Search code examples
jqueryliveaddclassremoveclass

where to put live() to make this jquery crossfade work


i have this jquery crossfade that is working for the most part

except it seems i need to use the live() function since the class "active" is being added to a list item element via the code.

i am not sure where to put the live() function in my code so this works.

heres my jquery:

$('.photo-thumbs ul li a').click(function(e) {

    e.preventDefault();

    var next = $(this).parent('li').index();

    $('.photo-main ul .active').fadeOut(1000).removeClass('.active');
    $('.photo-main ul li:eq('+next+')').fadeIn(1000).addClass('.active');

});

heres my html:

    <div class="photo-main">

        <ul>
        <li style="background-image: url(images/dummy1-l.jpg);"></li>
        <li class="active" style="background-image: url(images/dummy2-l.jpg);"></li>
        <li style="background-image: url(images/dummy3-l.jpg);"></li>
        <li style="background-image: url(images/dummy4-l.jpg);"></li>
        <li style="background-image: url(images/dummy5-l.jpg);"></li>
        </ul>

    </div>

    <div class="photo-thumbs">

        <ul>
        <li><a href="#" style="background-image: url(images/dummy1-s.jpg);"></a></li>
        <li><a href="#" style="background-image: url(images/dummy2-s.jpg);"></a></li>
        <li><a href="#" style="background-image: url(images/dummy3-s.jpg);"></a></li>
        <li><a href="#" style="background-image: url(images/dummy4-s.jpg);"></a></li>
        <li style="margin-right: 0px;"><a href="#" style="background-image: url(images/dummy5-s.jpg);"></a></li>
        </ul>

        <div style="clear: both;"></div>

    </div>

heres my css:

.listing-page .left .photo-main {
    width: 630px;
    height: 350px;
    margin-bottom: 10px;
    position: relative;
}
.listing-page .left .photo-main li {
    width: 630px;
    height: 350px;  
    position: absolute;
    top: 0px;
    left: 0px;
    z-index: 1;
    background-repeat: no-repeat;
    background-position: center center;
    display: none;
}
.listing-page .left .photo-main .active {
    z-index: 2;
    display: block;
}
.listing-page .left .photo-thumbs li {
    margin-right: 10px;
    float: left;
}
.listing-page .left .photo-thumbs li a {
    display: block;
    width: 118px;
    height: 118px;
    -webkit-opacity: 0.75;
    background-repeat: no-repeat;
    background-position: center center;
}
.listing-page .left .photo-thumbs li a:hover {
    -webkit-opacity: 1.0;
}

it seems that half the time the old LI does not fade out, and sometimes it switches to the wrong LI.


Solution

  • No need for live. You are just using an extra . in front of the class name:

    $('.photo-main ul .active').fadeOut(1000).removeClass('.active');
                                                           ^
    $('.photo-main ul li:eq('+next+')').fadeIn(1000).addClass('.active');
                                                               ^
    

    Get rid of the .s and your code should work just fine.