单页上具有多个实例的 jQuery 图像旋转器

发布于 2024-12-11 06:41:46 字数 435 浏览 0 评论 0原文

美好的一天,

我已经搜索和研究了这个问题 12 个小时,但找不到解决我的问题的方法。因为我不太了解 Javascript 和 jQuery,所以我不太确定要寻找和搜索什么。

我已经在我的网站上实现了这个 jQuery 图像旋转器成功并且它与在页面上工作的旋转器实例一起出色地工作。现在,我想知道是否可以让多个旋转器同时在单个页面上工作。

到目前为止我的努力还没有成功。两个旋转器将正确加载,但第一个旋转器的第一个图像将开始播放。然后,将切换到第二个旋转器并继续旋转该旋转器,而第一个旋转器留空。

对此的任何帮助和指导将不胜感激。

Good day,

I've searched and researched this for the past 12 hours and can't find a solution to my problem. And because I'm not too clued up on Javascript and jQuery I'm not exactly sure what to look for and search for.

I've implemented this jQuery Image Rotator on my site successfully and its working brilliantly with one instance of the rotator working on a page. Now, I want to know if its possible to get more than one rotator working on a single page, at the same time.

My efforts so far haven't been successful. The two rotators will load properly, but the first image of the first rotator will start playing. And then, will switch over to the second rotator and continue rotating that one, while the first one is left blank.

Any help and direction on this will be much appreciated.

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

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

发布评论

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

评论(1

痴者 2024-12-18 06:41:46

从目前的实施来看,没有。旋转器代码显式地对与 div#rotator 匹配的内容进行操作,其中应该最多是一个元素。

执行类似以下操作即可解决问题 - 传递包含 ul 的 div 的 id

function theRotator( id ) {
    //Set the opacity of all images to 0
    var jqElem = $( '#' + id );
    jqElem.find('ul li').css({opacity: 0.0});

    //Get the first image and display it (gets set to full opacity)
    jqElem.find('ul li:first').css({opacity: 1.0});

    //Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
    setInterval(rotate,6000);

    function rotate() { 
        //Get the first image
        var current = (jqElem.find('ul li.show')?  jqElem.find('ul li.show') : jqElem.find('ul li:first'));

        //Get next image, when it reaches the end, rotate it back to the first image
        var next = ((current.next().length) ? ((current.next().hasClass('show')) ? jqElem.find('ul li:first') :current.next()) : jqElem.find('ul li:first'));   

        //Set the fade in effect for the next image, the show class has higher z-index
        next.css({opacity: 0.0})
        .addClass('show')
        .animate({opacity: 1.0}, 1000);

        //Hide the current image
        current.animate({opacity: 0.0}, 1000)
        .removeClass('show');

    };

}


$(document).ready(function() {      
    //Load the slideshow
    theRotator( <id of rotator container 1> );
    theRotator( <id of rotator container 2> );
    ...
});

As it is currently implemented, no. The rotator code explicitly operates on the contents of whatever matches div#rotator, which should be at most one element.

Doing something like the following would do the trick - pass in the id of the div containing the ul.

function theRotator( id ) {
    //Set the opacity of all images to 0
    var jqElem = $( '#' + id );
    jqElem.find('ul li').css({opacity: 0.0});

    //Get the first image and display it (gets set to full opacity)
    jqElem.find('ul li:first').css({opacity: 1.0});

    //Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
    setInterval(rotate,6000);

    function rotate() { 
        //Get the first image
        var current = (jqElem.find('ul li.show')?  jqElem.find('ul li.show') : jqElem.find('ul li:first'));

        //Get next image, when it reaches the end, rotate it back to the first image
        var next = ((current.next().length) ? ((current.next().hasClass('show')) ? jqElem.find('ul li:first') :current.next()) : jqElem.find('ul li:first'));   

        //Set the fade in effect for the next image, the show class has higher z-index
        next.css({opacity: 0.0})
        .addClass('show')
        .animate({opacity: 1.0}, 1000);

        //Hide the current image
        current.animate({opacity: 0.0}, 1000)
        .removeClass('show');

    };

}


$(document).ready(function() {      
    //Load the slideshow
    theRotator( <id of rotator container 1> );
    theRotator( <id of rotator container 2> );
    ...
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文