jQueryReplacing images at time intervals

后端 未结 2 597
孤街浪徒
孤街浪徒 2020-12-22 01:21

I\'m trying to add christmas lights to my logo. I was going to do this in flash but I\'m trying to move away from flash so I decided to try it with jQuery.

A quick g

2条回答
  •  星月不相逢
    2020-12-22 01:31

    Try this;

    function swapImages() {
        var $current = $('#myGallery img:visible');
        var $next = $current.next();
        if($next.length === 0) {
            $next = $('#myGallery img:first');
        }
        $current.hide();
        $next.show();
    }
    
    $(document).ready(function() {
        // Run our swapImages() function every 0.5 secs
        setInterval(swapImages, 500);
    });
    

    Working example

    Bonus (Random change)

    function swapImages() {
        var random = Math.floor(Math.random()*3),
            $current = $('#myGallery img:visible');
        $current.hide();
        if($current.index() == random) {
            random = ++random % 4;
        }
        $('#myGallery img').eq(random).show();
    }
    
    $(document).ready(function() {
        // Run our swapImages() function every 0.5 secs
        setInterval(swapImages, 500);
    });
    

提交回复
热议问题