2010-11-23 8 views
0

que vous avez ce code:jquery vol stationnaire articles multiples

$("#sub_nav_home1").hover(function() { 
     $("#homepage").removeClass(); 
     $("#homepage").addClass("homepage1"); 
    }); 
    $("#sub_nav_home2").hover(function() { 
     $("#homepage").removeClass(); 
     $("#homepage").addClass("homepage2"); 
    }); 
    $("#sub_nav_home3").hover(function() { 
     $("#homepage").removeClass(); 
     $("#bhomepage").addClass("bhomepage3"); 
    }); 

    <ul class="nav sub_nav_home"> 
     <li id="sub_nav_home1"><a href="#"><span>LINK1</span></a></li> 
     <li id="sub_nav_home2"><a href="#"><span>LINK2</span></a></li> 
     <li id="sub_nav_home3"><a href="#"><span>LINK3</span></a></li> 
    </ul> 

Est-il possible que nous étions ne pouvons le faire la fonctionnalité jquery vol stationnaire une fois, au lieu d'un par lien?

espoir qui fait sens

Vive

+1

Pouvez-vous élaborer sur "une seule fois"? –

+0

D'accord avec @Brad. Cherchez-vous ['.one()'] (http://api.jquery.com/one)? –

+0

Vérifiez ma réponse, je pense que je sais ce que vous cherchez, et j'ai fait l'implémentation la plus simple basée sur le balisage DOM que vous avez. –

Répondre

1

Err, plus rapide que je peux penser est (ce sera dynamique aussi):

$("#sub_nav_home a").hover(function() { 
     $("#homepage").removeClass().addClass("homepage"+$(this).parent().attr('id').split('sub_nav_home')[1]); 
    }); 

    <ul class="nav sub_nav_home"> 
     <li id="sub_nav_home1"><a href="#"><span>LINK1</span></a></li> 
     <li id="sub_nav_home2"><a href="#"><span>LINK2</span></a></li> 
     <li id="sub_nav_home3"><a href="#"><span>LINK3</span></a></li> 
    </ul> 

Cela fera de la page d'accueil de la classe [le numéro d'identification à l'intérieur de ce parent] ie homepage1, home page2, etc

--UPDATE--

Cela devrait fonctionner pour vous (mais je na pas le tester! Laissez-moi savoir si cela ne fonctionne pas)

//find the first li and add a class of current to start the loop 
$('.sub_nav_home li:first').addClass('current'); 
//Here we set the loop 
setInterval(function(){ 
    //Here we are checking if there IS a next item. If there IS it'll return 1 
    //which will make this if() true (1 > 0) 
    if($('.current').next().length > 0){ 
     //Here we grab the current .current, remove the class, get the next item 
     //and then add .current to that 
     $('.current').removeClass('current').next().addClass('current'); 
    } 
    else{ 
     //If the if() fails (returns 0 [no next() item]) we'll get the current 
     //.current, remove the class, get the parent, find the first item in the 
     //parent (first <li> of the <ul>) and add a class of current 
     $('.current').removeClass('current').parent().find(':first').addClass('current'); 
    } 
},3000) //3000 = 3 seconds 

post-scriptum si cela fonctionne pour vous, assurez-vous de me donner un vote up;)

+0

cool - qui a fonctionné. – user

+0

une autre chose ... comment ajouteriez-vous une classe à chaque "li" qui tourne. disons, ajoutez une classe "current" au premier "li" et après 3 secondes, supprimez-le et ajoutez-le au "li" suivant, puis recommencez? – user

+0

OK, j'ai mis à jour mon message. Faites-moi savoir si cela ne fonctionne pas. Je n'ai pas testé, mais il devrait le faire. –

0

classe ajouter à chaque li:

<li class="c1" id="sub_nav_home1"><a href="#"><span>LINK1</span></a></li> 

$('ul.nav sub_nav_home li').hover(function() { 
     $("#homepage").removeClass(); 
     $("#bhomepage").addClass("bhomepage" + this.className.replace("c","")); 
    }); 
Questions connexes