11484 sujets

JavaScript, DOM et API Web HTML5

Bonjour à tous,

Je suis occupé de modifier un menu javascript, le soucis étant que je m'y connais bien en HTML, CSS et PHP mais j'avoue que Javascript je n'ai pas encore pratiqué.

Donc d'après le code que je lis (apparemment) label permet de nommer une instruction.
Ensuite en dessous le addlinks permet d'y lier le lien, mais j'ai l'impression que le menu est créé via une boucle, ce qui fait que j'ai le même lien sur tous les "label". Donc ma question est, comment attribuer un lien différent pour chaque "label" ?

Merci.

var links = [{label: 'Accueil', bg: '#c0392b'}, 
             {label: 'link2', bg: '#16a085'}, 
             {label: 'link3', bg: '#8e44ad'}, 
             {label: 'link4', bg: '#27ae60'}, 
             {label: 'link5', bg: '#f39c12'}, 
             {label: 'link6', bg: '#2980b9'}];
var windowHeight = window.innerHeight;
if(windowHeight === 0) windowHeight = 238;
var radius = windowHeight*0.6,
    circle = document.createElement('div'),
    borderSize = radius*0.021;
    totalArea = 48, 
    increment = totalArea/(links.length-1),
    startPoint = 0-(totalArea/2),
    fontSize = radius*0.12,
    linkSize = radius*0.25;

styleCircle();
addCircle();
addLinks();
styleLinks();

function styleCircle() {
  circle.style.border= borderSize+'px solid #fff';
  circle.style.width = radius*2+'px';
  circle.style.height = radius*2+'px';
  circle.style.borderRadius = radius+'px';
  circle.style.position = 'absolute';
  circle.style.top = '-'+radius*0.2+'px';
  circle.style.left = radius*-1+'px';
}

function addCircle() {
  document.body.appendChild(circle);
}

function addLinks() {
  for (var i=0, l=links.length; i<l; i++) {
    link = document.createElement('a'),
    hover = document.createElement('span');
    link.href = "index.php";
    link.dataset.color = links[i].bg;
    link.style.display = 'inline-block';
    link.style.textDecoration = 'none';
    link.style.color = '#fff';
    link.style.position = 'absolute';
    link.style.zIndex = 100;
    link.innerHTML = links[i].label;
    hover.style.position = 'absolute';
    hover.style.display = 'inline-block';
    hover.style.zIndex = 50;
    hover.style.opacity = 0;
    document.body.appendChild(link);
    document.body.appendChild(hover);
    link.addEventListener('mouseover', linkOver);
    link.addEventListener('mouseout', linkOut);
    links[i].elem = link;
    links[i].hover = hover;
  }
}

function styleLinks() {
  for (var i=0, l=links.length; i<l; i++) {
    var link = links[i].elem, hover = links[i].hover, deg = startPoint+(i*increment);  
    link.style.paddingLeft = radius*1.2+'px';
    link.style.fontSize = fontSize+'px';
    link.style.height = linkSize+'px';
    link.style.lineHeight = linkSize+'px';
    setTransformOrigin(link, '0px '+linkSize*0.5+'px');
    setTransition(link, 'all 0.2s ease-out');
    setTransform(link, 'rotate('+deg+'deg)');
    link.style.left = borderSize+'px';
    link.style.top = (windowHeight/2) - (windowHeight*0.1)+borderSize+'px';

    hover.style.left = borderSize+'px';
    setTransformOrigin(hover, '0px '+linkSize*0.5+'px');
    setTransition(hover, 'all 0.2s ease-out');
    setTransform(hover, 'rotate('+deg+'deg)');
    hover.style.top = (windowHeight*0.4)+borderSize +'px';
    hover.style.width = radius+(borderSize/2)+'px';
    hover.style.height = linkSize+'px';
    hover.style.borderRight = borderSize*2+'px solid #fff';
  
  }
}

window.onresize = function() {
  windowHeight = window.innerHeight;
  radius = windowHeight*0.6,
  borderSize = radius*0.021;  
  fontSize = radius*0.12,
  linkSize = radius*0.25;
  styleCircle();
  styleLinks();
}

function linkOver(e) {
  var thisLink = e.target, thisHover = thisLink.nextSibling;
  thisLink.style.paddingLeft = radius*1.25+'px';
  thisHover.style.opacity = 1;
  document.body.style.backgroundColor = thisLink.dataset.color;
}

function linkOut(e) {
  var thisLink = e.target, thisHover = thisLink.nextSibling;
  thisLink.style.paddingLeft = radius*1.2+'px';
  thisHover.style.opacity = 0;
}

function setTransform(element, string) {
  element.style.webkitTransform = string;
  element.style.MozTransform = string;
  element.style.msTransform = string;
  element.style.OTransform = string;
  element.style.transform = string;
}

function setTransformOrigin(element, string) {
  element.style.webkitTransformOrigin = string;
  element.style.MozTransformOrigin = string;
  element.style.msTransformOrigin = string;
  element.style.OTransformOrigin = string;
  element.style.transformOrigin = string;
}

function setTransition(element, string) {
  element.style.webkitTransition = string;
  element.style.MozTransition = string;
  element.style.msTransition = string;
  element.style.OTransition = string;
  element.style.transition = string;
}
Hello Smiley smile

Ce n'est donc pas toi qui a fait ce code Smiley smile

sans chercher plus loin et pour ne pas t'embrouiller trop Smiley cligne
cette ligne dans la boucle
link.innerHTML = links[i].label;

en fait ici cela assigne en HTML (innerHTML) la valeur contenue dans l'array en haut Smiley cligne (label)
donc si tu veux un lien, il faut que tu complètes l'array en haut avec par exemple une valeur lien
comme ceci :

var links = [{label: 'Accueil', lien:'index.php', bg: '#c0392b'}, 
             {label: 'link2', lien:'index.php', bg: '#16a085'}, 
             {label: 'link3', lien:'index.php', bg: '#8e44ad'}, 
             {label: 'link4', lien:'index.php', bg: '#27ae60'}, 
             {label: 'link5', lien:'index.php', bg: '#f39c12'}, 
             {label: 'link6', lien:'index.php', bg: '#2980b9'}];


et dans la boucle tu remplaces :
link.href = "index.php";

par
link.href = links[i].lien;


voilà Smiley smile c'est la logique utilisée ici et ça marche pour les 2 boucles présentes Smiley smile

bon courage
Meilleure solution
Non non effectivement je n'ai pas écrit le code, c'est un menu opensource mais sans explication et comme je ne me suis pas encore mis au JS, je ne comprenais pas trop.

Un grand merci pour la réponse si rapide, cela fonctionne parfaitement.