【问题标题】:restart animation jquery and javascript重新启动动画 jquery 和 javascript
【发布时间】:2015-11-30 18:42:33
【问题描述】:

您好,我正在创建一个 jquery 滑块,当时间(在 setInterval 中)结束时,我无法重新启动滑块。当 3 张图片滑动并且最后一张显示动画时,动画停留在那里,但我想重新开始到第一张图片并再次开始循环。有人可以帮助我吗? 谢谢!

HTML

                        <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Slider Jquery Test</title>
    <link href="slider.css" rel="stylesheet" type="text/css">
    <script src="js/jquery-2.1.4.min.js"></script>
    <script src="js/slidertest.js"></script>
    </head>
    <body>
        <div id="container">
            <ul id="buttons" class="buttons">
                <li><a href="#"><img id="left_b" class="left_b" src="imagens/flechae.png" alt="Anterior" onMouseOver="this.src='imagens/flechaehover.png'" onMouseOut="this.src='imagens/flechae.png'"></a></li>
                <li><a href="#"><img id="right_b" class="right_b" src="imagens/flechad.png" alt="Proximo" onMouseOver="this.src='imagens/flechadhover.png'" onMouseOut="this.src='imagens/flechad.png'"></a></li>
            </ul>
            <ul id="gallery" class="gallery">
                <li><img id="1" class="images" src="imagens/imagem1.jpg" alt="Imagem 1"></li>
                <li><img id="2" class="images" src="imagens/imagem2.jpg" alt="Imagem 2"></li>
                <li><img id="3" class="images" src="imagens/imagem3.jpg" alt="Imagem 3"></li>      
            </ul>
        </div>
    </body>
    </html>

CSS

    @charset "utf-8";

    body{
        width: 100%;
        height: auto;   
    }

    #container {
        width: 100%;
        height: auto;
        margin: 0;
        padding: 0;
        /*overflow: hidden;*/
    }

    .buttons {
        width: 100%;
        height: auto;
        list-style: none;
        margin: 0;
        padding: 0;
        position: absolute;
        z-index: 1;
        display: none;
    }

    .buttons li {
        width: 100%;
        height: auto;
        position: relative;
        margin: 0;
        padding: 0;
    }
    .left_b {
        width: 20px;
        height: 80px;
        position: relative;
        float: left;
        margin-left: 30px;
        margin-top: 250px;
    }

    .right_b {
        width: 20px;
        height: 80px;
        position: relative;
        float: right;
        margin-right: 30px;
        margin-top: 250px;
    }

    .gallery {
        width: 100%;
        height: auto;
        list-style: none;
        margin: 0;
        padding: 0;
        position: relative;
        overflow: hidden;
    }

    .gallery li {
        width: auto;
        height: auto;
        position: relative;
        float: left;
    }

    .images {
        width: 1200px;
        height: 720px;
        position: relative;
        float: left;
    }

JavaScript

$(function(){

slide();

// Slider

function slide()
{
    // Variables
    var interval = 0;
    var time = 3000;
    var seconds = 1000;
    var current_image = 1;
    var num_images = 0;
    var total_width = 0;
    var slide_left = "+=1200px";
    var slide_right = "-=1200px";
    var right_b = $(".right_b");
    var left_b = $(".left_b");
    var left = "margin-left";
    var width = "width";
    var gallery = $(".gallery");
    var galleryLi = $(".gallery li");
    var images = $(".images");
    var buttons_class = $(".buttons");
    var container = $("#container");

    // Calling functions
    imagesSize();
    hideButtons();
    animation();


    // Functions
    function animation()
    {
        if(current_image < num_images)
        {
            loop();
            clickRight();
            clickLeft();
        }
    }

    function hideButtons()
    {   
        container.hover(function(){
            buttons_class.fadeIn(); 
        }, function(){
            buttons_class.fadeOut();    
        });
    }

    function imagesSize()
    {
        galleryLi.each(function(){
            num_images++;
            total_width += 1200;    
        });
        gallery.css(width, total_width + "px");
    }

    function loop()
    {
        if(current_image >= 1){
            interval = setInterval(moveLeft, time);
        }
        else if(current_image === num_images){
            clearLoop();
            moveLeft();
        }
    }

    function clearLoop()
    {
        clearInterval(interval);
    }

    function moveLeft()
    {
        if(current_image < num_images){
            gallery.animate({left: slide_right}, seconds);
            current_image++;
        }
    }

    function clickRight()
    {
        right_b.click(function(){
            moveLeft();
            clearLoop();
            loop();
        }); 
    }

    function moveRight()
    {
        if(current_image > 1){
            gallery.animate({"margin-left": slide_left}, seconds);
            current_image--;
        }   
    }

    function clickLeft()
    {
        left_b.click(function(){
            moveRight();
            clearLoop();
            loop();
        }); 
    }


} // end of function slide

}); // End of main function

【问题讨论】:

    标签: javascript jquery animation slider restart


    【解决方案1】:
    function loop()
        {
            if(current_image >= 1){
                interval = setInterval(moveLeft, time);
            }
            else if(current_image === num_images){
                clearLoop(); // Clear the previous interval
                moveLeft();
                // Add these two lines after setTimeout to finish the animation.
                setTimeout(function(){
                    current_image = 1;
                    loop();
                }, seconds);
            }
    
        }
    

    【讨论】:

    • 对不起,它仍然不起作用,你的逻辑是有道理的,但我现在不明白为什么它不起作用,还有其他方法吗?谢谢!
    猜你喜欢
    • 2016-08-20
    • 1970-01-01
    • 2017-04-28
    • 2021-11-13
    • 1970-01-01
    • 2022-10-19
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    相关资源
    最近更新 更多