2014-10-02 131 views
1

我想從我的滑塊jquery部分添加一個自動播放。我能做什麼?jquery圖像滑塊自動播放

我有一個下一個& prev按鈕,但我想添加一個自動播放,並且當鼠標懸停在幻燈片自動停止的圖像上。

任何人都可以幫助我嗎?

這是我DEMO頁從codepen

jQuery(document).ready(function ($) { 

    var slideCount = $('#slider ul li').length; 
    var slideWidth = $('#slider ul li').width(); 
    var slideHeight = $('#slider ul li').height(); 
    var sliderUlWidth = slideCount * slideWidth; 

    $('#slider').css({ width: slideWidth, height: slideHeight }); 

    $('#slider ul').css({ width: sliderUlWidth, marginLeft: - slideWidth }); 

    $('#slider ul li:last-child').prependTo('#slider ul'); 

    function moveLeft() { 
     $('#slider ul').animate({ 
      left: + slideWidth 
     }, 200, function() { 
      $('#slider ul li:last-child').prependTo('#slider ul'); 
      $('#slider ul').css('left', ''); 
     }); 
    }; 

    function moveRight() { 
     $('#slider ul').animate({ 
      left: - slideWidth 
     }, 200, function() { 
      $('#slider ul li:first-child').appendTo('#slider ul'); 
      $('#slider ul').css('left', ''); 
     }); 
    }; 

    $('a.control_prev').click(function() { 
     moveLeft(); 
    }); 

    $('a.control_next').click(function() { 
     moveRight(); 
    }); 

}); 
+0

你想要什麼樣的滑塊?繼續移動滑塊或逐個滑動圖像?檢查[this](http://slideshow.hohli.com/docs/demo06.html)out – 2014-10-02 06:58:20

+0

@HendryTanaka我想自動播放只moveLeft。 – innovation 2014-10-02 07:01:30

回答

4

修改你的這樣的腳本:

jQuery(document).ready(function ($) { 

    var slideCount = $('#slider ul li').length; 
    var slideWidth = $('#slider ul li').width(); 
    var slideHeight = $('#slider ul li').height(); 
    var sliderUlWidth = slideCount * slideWidth; 

    $('#slider').css({ width: slideWidth, height: slideHeight }); 

    $('#slider ul').css({ width: sliderUlWidth, marginLeft: - slideWidth }); 

    $('#slider ul li:last-child').prependTo('#slider ul'); 

    function moveLeft() { 
     $('#slider ul').animate({ 
      left: + slideWidth 
     }, 1000, function() { 
      $('#slider ul li:last-child').prependTo('#slider ul'); 
      $('#slider ul').css('left', ''); 
     }); 
    }; 
    function do_slide(){ 
    interval = setInterval(function(){ 
     moveLeft(); 
    }, 1000); 
    } 
    do_slide(); 


    $('ul li').hover(function(){ 
     clearInterval(interval); 
    }); 
     $('ul li').mouseleave(function(){ 
     do_slide(); 
    }); 
}); 
+0

運行良好!相信我!對不起,亂碼。 – 2014-10-02 07:35:21

1

我發現這個代碼的解決方案:

$(function(){ 
    setInterval(function() { 
     moveRight(); 
    }, 3000); 
    }); 
+0

停止滑動懸停? – 2014-10-02 07:19:31

+0

@HendryTanaka我無法做到。 – innovation 2014-10-02 07:25:20

+0

要停止動畫,請使用stop()函數。在你的代碼情況下,添加這個代碼'$('ul li')。hover(function(){('#slider ul')。stop(); });' – 2014-10-02 07:26:25

0

這增加的決賽之前你的JavaScript代碼的結束「}) ;「

 var myparam1 = true; 

     (function(){ 
     if (myparam1){ 
     moveLeft();}; 
     setTimeout(arguments.callee, 1000); 
     })(); 

     $("#slider") 
     .mouseenter(function(){myparam1 = false;}) 
     .mouseleave(function(){myparam1 = true;});