在哪里放置 live() 以使 jquery crossfade 工作

发布于 2024-12-22 18:17:03 字数 2620 浏览 1 评论 0原文

我有这个jquery crossfade,它在很大程度上起作用,

除了似乎我需要使用 live() 函数,因为“active”类通过代码添加到列表项元素中。

我不确定将 live() 函数放在我的代码中的哪里,所以这是可行的。

这是我的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');

});

这是我的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>

这是我的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;
}

似乎有一半的时间旧LI不会淡出,有时它会切换到错误的LI。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

双手揣兜 2024-12-29 18:17:03

无需实时。您只需在类名前面使用额外的 .

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

去掉 .,您的代码应该可以正常工作。

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.

颜漓半夏 2024-12-29 18:17:03

在这里很难判断您要解决什么问题,但是此代码中的活动类不需要 .live() 因为您没有在之前运行涉及 .active 的选择器查询时间 - 您仅在需要时才运行它们。此处提前运行的唯一选择器是分配点击处理程序且未指定 .active 类的选择器。

如果您需要更具体的帮助,则必须澄清问题所在。

另外,不再推荐 .live() 。应使用 .on() (v1.7+) 或 .delegate() (v1.7 之前)。

根据您问题的澄清,我认为您只需要删除 addClass()removeClass() 中类名前面的句点,因此代码如下所示这:

$('.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');

});

It's not easy to tell what problem you're trying to solve here, but .live() is not needed for the active class in this code because you're not running selector queries involving .active ahead of time - you're only running them when you need them. The only selector that is being run ahead of time here is the one that assigns the click handler and that doesn't specify the .active class.

If you want more specific help, you'll have to clarify what the problem is.

Also, .live() is no longer recommended. One should use .on() (v1.7+) or .delegate() (before v1.7).

Based on the clarification of your question, I think you just need to remove the periods in front of the class name in addClass() and removeClass() so the code looks like this:

$('.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');

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文