2017-01-11 90 views
0

你好我有一個jquery插件,我想以特定的時間間隔運行,問題我有它它只運行一次然後停止,但如果我交換函數被調用一個簡單的警報()然後它運行正常,setinerval只調用一個函數時運行一次

任何幫助將非常感激。

感謝

大衛

在你的代碼
(function ($) { 
$.fn.gtSlider = function (options) { 

    /*****Defualt options****/ 
    var settings = $.extend({ 
     slideInterval: 5000, 
     slideSpeed: 10000, 
     activeSlideClass: "active" 
    }, options); 


    return this.each(function() { 

     /*****Set repsonsive widths/heights ****/ 

     /*Slider*/ 
     var slider = $(this); 
     /*Slider parent*/ 
     var parent = $(slider).parent(); 
     /*Slides*/ 
     var slide = $(parent).find("li"); 
     /*Slider outter height*/ 
     var sliderHeight = $(parent).outerHeight(); 
     /*Slider outter width*/ 
     var sliderWidth = $(parent).outerWidth(); 
     /*Count slides with */ 
     var slideCount = $(slide).length; 
     /*gtSlider width*/ 
     slider.width(sliderWidth * slideCount); 
     /*Set slider height based on parent*/ 
     slider.height(sliderHeight); 
     /*Set slider width based on parent*/ 
     slide.width(sliderWidth); 

     //Set active slide to 1 to match slide count 
     var activeSlideIndex = slide.index(); 

     function showNextSlide() {     
      /*Left slide animation*/ 
      slider.animate({ 
       right: sliderWidth 
      }, settings.slideSpeed, function() { 
      }); 
     } 


     setInterval(function() { 
      showNextSlide() 
     }, settings.slideInterval); 

    }); 
}; 
}(jQuery)); 



$(document).ready(function() { 
$(".gtslider").gtSlider({ 
    slideInterval: 2000, 
    slideSpeed: 1000, 
    activeSlideClass: "active" 
}); 
}); 




<div class="container"> 
    <div class="gtslider-wrapper"> 
     <ul class="gtslider"> 
      <li> 

      </li> 
      <li> 

      </li> 
      <li> 

      </li> 
     </ul> 
     <div style="clear:both"></div> 
    </div> 
</div> 



<style> 
.container{margin:0px auto;max-width:900px;width:100%} 
.gtslider-wrapper{overflow:hidden;width:100%;padding- bottom:40%;height:0px;display:inline-block;} 
.gtslider{position:relative;width:100%;height:auto;margin:0;text-  align:center;padding:0;overflow:hidden} 
.gtslider li{width:100%;list-style:none;text-align:center;float:left} 
.gtslider li *{margin:0px auto} 
.gtslider li img{max-width:100%;height:auto;display:block} 
</style> 
+1

是否有可能該函數實際上運行,但只是沒有做你期望的事情? http://jsbin.com/zizunikime/1/edit?js,console –

+0

嗯,我們爲什麼它不會左滑兩次? –

+0

你是動畫正確的屬性...檢查HTML,也許答案在那裏,只是猜測...你可以顯示示例,與HTML/CSS? – sinisake

回答

0

一些問題。你必須增量正確的財產,你必須重置動畫,當所有幻燈片顯示。這是固定的:

function showNextSlide() {     
      /*Left slide animation*/ 

      slider.animate({ 
       right:'+='+sliderWidth 
      }, settings.slideSpeed, function() { 

      if($(slider).css('right')==$(slider).css('width')) 
      $(slider).css('right','0') 
      }); 
     } 

檢查片段:

(function ($) { 
 
$.fn.gtSlider = function (options) { 
 

 
    /*****Defualt options****/ 
 
    var settings = $.extend({ 
 
     slideInterval: 5000, 
 
     slideSpeed: 10000, 
 
     activeSlideClass: "active" 
 
    }, options); 
 

 

 
    return this.each(function() { 
 

 
     /*****Set repsonsive widths/heights ****/ 
 

 
     /*Slider*/ 
 
     var slider = $(this); 
 
     /*Slider parent*/ 
 
     var parent = $(slider).parent(); 
 
     /*Slides*/ 
 
     var slide = $(parent).find("li"); 
 
     /*Slider outter height*/ 
 
     var sliderHeight = $(parent).outerHeight(); 
 
     /*Slider outter width*/ 
 
     var sliderWidth = $(parent).outerWidth(); 
 
     /*Count slides with */ 
 
     var slideCount = $(slide).length; 
 
     /*gtSlider width*/ 
 
     slider.width(sliderWidth * slideCount); 
 
     /*Set slider height based on parent*/ 
 
     slider.height(sliderHeight); 
 
     /*Set slider width based on parent*/ 
 
     slide.width(sliderWidth); 
 

 
     //Set active slide to 1 to match slide count 
 
     var activeSlideIndex = slide.index(); 
 
     
 
     
 

 
     function showNextSlide() {     
 
      /*Left slide animation*/ 
 
      
 
      slider.animate({ 
 
       right:'+='+sliderWidth 
 
      }, settings.slideSpeed, function() { 
 
      
 
      if($(slider).css('right')==$(slider).css('width')) 
 
      $(slider).css('right','0') 
 
      }); 
 
     } 
 

 

 
     setInterval(function() { 
 
     
 
      showNextSlide() 
 
     }, settings.slideInterval); 
 

 
    }); 
 
}; 
 
}(jQuery)); 
 

 

 
$(".gtslider").gtSlider({ 
 
    slideInterval: 2000, 
 
    slideSpeed: 1000, 
 
    activeSlideClass: "active" 
 
});
.container{margin:0px auto;max-width:900px;width:100%} 
 
.gtslider-wrapper{overflow:hidden;width:100%;padding-bottom:40%;height:0px;display:inline-block;} 
 
.gtslider{position:relative;width:100%;height:auto;margin:0;text-align:center;padding:0;overflow:hidden} 
 
.gtslider li{width:100%;list-style:none;text-align:center;float:left} 
 
.gtslider li *{margin:0px auto} 
 
.gtslider li img{max-width:100%;height:auto;display:block}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="container"> 
 
    <div class="gtslider-wrapper"> 
 
     <ul class="gtslider"> 
 
      <li> 
 
11111111111111111 
 
      </li> 
 
      <li> 
 
222222222222222222222222222 
 
      </li> 
 
      <li> 
 
33333333333333333333333333333333333333333333 
 
      </li> 
 
     </ul> 
 
     <div style="clear:both"></div> 
 
    </div> 
 
</div>

附:我想你打算添加活動類到活動幻燈片(你當前的活動幻燈片變種不應該做它應該做的),但是,我想你會想出它......

+0

非常感謝您的幫助 –