jquery停止/启动滑块按钮
我正在寻找一个简单任务的解决方案。如何编程按钮以停止/启动幻灯片放映。 我制作了startSlider和stopSlider功能,当我保存了这些内容时,按钮就消失了jquery停止/启动滑块按钮,jquery,button,slider,Jquery,Button,Slider,我正在寻找一个简单任务的解决方案。如何编程按钮以停止/启动幻灯片放映。 我制作了startSlider和stopSlider功能,当我保存了这些内容时,按钮就消失了 $('#stopStartBtn').toggle( function(e){ e.preventDefault(); stopSlider()}, function(e){ e.preventDefault(); startSlider();
$('#stopStartBtn').toggle(
function(e){ e.preventDefault();
stopSlider()},
function(e){ e.preventDefault();
startSlider();}
);
除此之外,滑块在悬停时起作用(停止/启动),在文档准备就绪时开始
HTML:
给你:
var started=true;
$('#stopStartBtn').click(function(){
if(started){
stopSlider();
started=false;
}
else{
startSlider();
started=true;
}
});
说明:
toggle()
另外,pause
变量必须等于animationSpeed
变量。mouseenter
和mouseleave
事件是否按预期工作?是的,mouseenter和mousleave在没有切换的情况下工作正常,这是我的代码,很棒!Thx,告诉我为什么变量pause和animationSpeed应该相等?因为如果pause
小于animationSpeed
,幻灯片动画将排队,因此停止操作不会立即生效!亲自看看=>
$(function(){
//configuration
var width = 720,
animationSpeed = 1000,
pause = 500,
currentImage = 1,
interval;
//cache DOM
var $slider = $('#slider'),
$slideContainer = $slider.find('.slides'),
$image = $slideContainer.find('.image');
//setInterval
function startSlider() {
interval = setInterval(function(){
$slideContainer.animate({'margin-left': '-='+width},animationSpeed, function(){
currentImage++;
//if it's last slide go to position 1 (0px)
if(currentImage === $image.length) {
currentImage = 1;
$slideContainer.css('margin-left',0);
}
});
},pause);//setInterval
}//startSlider
function stopSlider() {
clearInterval(interval);
}//stopSlider
//hover stop/start
$slider.on('mouseenter',stopSlider).on('mouseleave',startSlider);
$('#stopStartBtn').toggle(
function(e){ e.preventDefault();
stopSlider();},
function(e){ e.preventDefault();
startSlider();}
);
startSlider();
});//ready
var started=true;
$('#stopStartBtn').click(function(){
if(started){
stopSlider();
started=false;
}
else{
startSlider();
started=true;
}
});