jQuery 轮播不重复

发布于 2024-12-26 02:22:41 字数 2068 浏览 5 评论 0原文

我正在尝试为我的网站创建一个轮播,但无法将幻灯片移动到列表的后面,因此它是一个连续的流,没有空白。代码如下:

<div class="maskleft"></div>
<div class="maskright"></div>

<div class="slideshow">
    <div class="views-row views-row-1 views-row-odd views-row-first">
        <div class="panel-text">text</div>  
        <div class="views-field views-field-field-image-feature">
            <img typeof="foaf:Image" src="" alt="" />
        </div> 
        <img typeof="foaf:Image" src="" alt="" />
    </div>

    <div class="views-row views-row-2 views-row-even">
        <div class="panel-text">text</div>  
        <div class="views-field views-field-field-image-feature">
            <img typeof="foaf:Image" src="" alt="" />
        </div> 
        <img typeof="foaf:Image" src="" alt="" />
    </div>

    <div class="views-row views-row-3 views-row-odd">
        <div class="panel-text">text</div>  
        <div class="views-field views-field-field-image-feature">
            <img typeof="foaf:Image" src="" alt="" />
        </div> 
        <img typeof="foaf:Image" src="" alt="" />
    </div>

    <div class="views-row views-row-4 views-row-even views-row-last">
        <div class="panel-text">text</div>  
        <div class="views-field views-field-field-image-feature">
            <img typeof="foaf:Image" src="" alt="" />
        </div> 
        <img typeof="foaf:Image" src="" alt="" />
    </div>
</div>

jQuery 是:

function slideChange() {
    var $slider = $('.slideshow');

    var $next = $slider.next().length ? $slider.next() : $('.slideshow'); 

    $next.animate({marginLeft: '-=1024'}, 1000, function() {
    });
}

$(document).ready(function() {
    setInterval( "slideChange()", 5000 );
});

实时开发站点在这里:http://dev.shoeboxdesign.co。英国/

I am trying to create a carousel for my site and am having trouble getting the slides to move to the back of the list, so it is a continuous stream with no white space. Here is the code:

<div class="maskleft"></div>
<div class="maskright"></div>

<div class="slideshow">
    <div class="views-row views-row-1 views-row-odd views-row-first">
        <div class="panel-text">text</div>  
        <div class="views-field views-field-field-image-feature">
            <img typeof="foaf:Image" src="" alt="" />
        </div> 
        <img typeof="foaf:Image" src="" alt="" />
    </div>

    <div class="views-row views-row-2 views-row-even">
        <div class="panel-text">text</div>  
        <div class="views-field views-field-field-image-feature">
            <img typeof="foaf:Image" src="" alt="" />
        </div> 
        <img typeof="foaf:Image" src="" alt="" />
    </div>

    <div class="views-row views-row-3 views-row-odd">
        <div class="panel-text">text</div>  
        <div class="views-field views-field-field-image-feature">
            <img typeof="foaf:Image" src="" alt="" />
        </div> 
        <img typeof="foaf:Image" src="" alt="" />
    </div>

    <div class="views-row views-row-4 views-row-even views-row-last">
        <div class="panel-text">text</div>  
        <div class="views-field views-field-field-image-feature">
            <img typeof="foaf:Image" src="" alt="" />
        </div> 
        <img typeof="foaf:Image" src="" alt="" />
    </div>
</div>

The jQuery is:

function slideChange() {
    var $slider = $('.slideshow');

    var $next = $slider.next().length ? $slider.next() : $('.slideshow'); 

    $next.animate({marginLeft: '-=1024'}, 1000, function() {
    });
}

$(document).ready(function() {
    setInterval( "slideChange()", 5000 );
});

The live dev site is here: http://dev.shoeboxdesign.co.uk/

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

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

发布评论

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

评论(2

污味仙女 2025-01-02 02:22:42
  1. 您只有一个正在移动的 div,为什么需要此代码:

    var $next = $slider.next().length ? $slider.next() : $('.slideshow'); 
    
  2. 当你到达幻灯片结尾:

    if (... check the end of Slideshow ...) { // 可以计算宽度、幻灯片数量等。
        $slider.css({marginLeft: '0'});
    }
    
  1. You have only one div whitch is moving, why do you need this code:

    var $next = $slider.next().length ? $slider.next() : $('.slideshow'); 
    
  2. You need to reset your marginLeft style when you reach end of your slideshow:

    if (... check the end of slideshow ...) { // could be calculation of the widht, count of slides, etc.
        $slider.css({marginLeft: '0'});
    }
    
瑾兮 2025-01-02 02:22:42

如果我的理解是对的,您希望将第一张幻灯片附加到幻灯片的末尾,这样您就有一个恒定的流,而不是跳回到开头。
假设这是正确的,我们发现了一个重要的关键字:append

您必须虚拟地拍摄第一张幻灯片并将其附加到最后一张幻灯片。通过每个动画步骤执行此操作。我们最终得到这样的结果。

function slideChange() {
    var $slider = $('.slideshow');
    var $first = $slider.first();

    $slider.append($first); // Append a copy of the first slide to the last one
    $slider.animate({marginLeft: '-=1024'}, 1000, function() {
        $first.remove(); // Remove the first slide we just copied. So we virtually swapped the slide from the first to the last place
        $slider.css({marginLeft: '+=1024'}); // Reset the margin
    });

}

该解决方案未经测试,但应该可以解决问题。
但我仍然发现您的代码存在一些问题。

您想仅将此功能用于此滑块吗?如果您的答案是“是”,您应该明确为滑块提供 #id 而不是 .class 选择器。而且由于您不会再在任何地方使用您的函数,因此不需要可公开访问的函数。您可以匿名进行,这可以节省一些资源。

setInterval(function() {
    /* Your stuff goes here */
}, 5000);

If I get you right, you want to append the first slide to the end of the slideshow so you have a constant stream instead of a jump cut back to the start.
Assuming this is correct, we found an important keyword: append.

You have to virtually take the first slide and append it to the last slide. Do this by every animation step. We end up with something like this.

function slideChange() {
    var $slider = $('.slideshow');
    var $first = $slider.first();

    $slider.append($first); // Append a copy of the first slide to the last one
    $slider.animate({marginLeft: '-=1024'}, 1000, function() {
        $first.remove(); // Remove the first slide we just copied. So we virtually swapped the slide from the first to the last place
        $slider.css({marginLeft: '+=1024'}); // Reset the margin
    });

}

This solution is untested but should do the trick.
But I still see some problems in your code.

Do you want to use this function only for this slider? If your answer is 'yes' you should definetly give your slider an #id and not a .class selector. And because you won't use your function anywhere again, there's no need for a publicly accessible function. You could make it anonymously, which saves some ressources.

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