0

say you have this code:

    $("#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>

IS there a way were we can only do the hover jquery functionality once, instead of one per link?

Hope that made sense

Cheers

3
  • 1
    Can you elaborate on "only once"? Commented Nov 23, 2010 at 23:31
  • Agreed with @Brad. Are you looking for .one()? Commented Nov 23, 2010 at 23:34
  • Check my answer, i think i know what you're looking for, and i made it easiest implementation based on the DOM markup you have. Commented Nov 23, 2010 at 23:36

2 Answers 2

1

Err, quickest way i can think of is (this will be dynamic too):

    $("#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>

That will make the class homepage[the id number inside that's parent] i.e. homepage1, homepage2, etc

--UPDATE--

This should work for you (but i didnt test it! Just let me know if it doesnt work)

//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

P.S. if this works for you, make sure to give me an up vote ;)

Sign up to request clarification or add additional context in comments.

2 Comments

another thing... how would you add a class to each "li" that rotates. say, add a class "current" to the first "li" and after 3 seconds remove it and add it to the next "li" and then start again?
OK, i updated my post. Let me know if it doesn't work. I didnt test it, but it should.
0

add class to each 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","")); 
    }); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.