我正在编写一个简单的jquery滑块,目前我无法在单击播放/暂停按钮时启动它,使用if和stop = false状态启动滑块但无法使其工作,代码相当简单,但我在js中有点新手。
swapslides函数完成它的工作,它是代码中断的条件检查器。
您可以在此处查看代码:
<script>
$(document).ready(function(){
var stop = true;
$('.play').click(function(){
if (stop == true) {
stop = false;
alert(stop);
}
else {
stop = true;
alert(stop);
}
});
if(stop == false){
setInterval(swapSlides, 3000);
}
function swapSlides(){
var cs = $('div.currentslide:first');
var ns = cs.next();
if(ns.hasClass('mySlides1')){
cs.removeClass('currentslide');
ns.addClass('currentslide');
}
else{
ns = $('#polaroid1').children('div.mySlides1:first');
cs.removeClass('currentslide');
ns.addClass('currentslide');
}
}
});
</script>
&#13;
.play{
position:absolute;
left:5px;
bottom:5px;
cursor:pointer;
z-index:10000;
}
.polaroid1{
box-shadow: 10px 10px 7px -7px rgba(0, 0, 0, 0.5);
-webkit-backface-visibility: hidden;
transform: rotate(-8deg);
margin-bottom:30px;
width:380px;
height:320px;
background-color:rgba(255, 255, 255, 1.0);
text-align:right;
padding-top:10px;
padding-right:10px;
padding-left:10px;
padding-bottom:15px;
top:15px;
left:25px;
position:relative;
}
.photo1{
width:100%;
height:85%;
position:relative;
padding:5px;
}
.date1{
margin:0;
padding-right:10px;
font-family: 'Covered By Your Grace', cursive;
transform: rotate(-5deg);
font-size:28px;
}
.mySlides1{
display:none;
width:380px;
height:320px;
position:absolute;
top:0px;
left:0px;
}
.currentslide{display:block;}
&#13;
<div id="polaroid1" class="polaroid1">
<img class="play" src="images/heart.png">
<div class="mySlides1 fade currentslide">
<img class="photo1" src="images/IMG-20170610-WA0028.jpg">
<h3 class="date1">22-05-2017</h3>
</div>
<div class="mySlides1 fade">
<img class="photo1" src="images/20170812_181516.jpg">
<h3 class="date1">12-08-2017</h3>
</div>
<div class="mySlides1 fade">
<img class="photo1" src="images/20170522_112958.jpg">
<h3 class="date1">22-05-2017</h3>
</div>
<div class="mySlides1 fade">
<img class="photo1" src="images/IMG-20170610-WA0017.jpg">
<h3 class="date1">10-06-2017</h3>
</div>
<div class="mySlides1 fade">
<img class="photo1" src="images/20170819_194526.jpg">
<h3 class="date1">19-08-2017</h3>
</div>
<div class="mySlides1 fade">
<img class="photo1" src="images/20170811_182103.jpg">
<h3 class="date1">11-08-2017</h3>
</div>
<div class="mySlides1 fade">
<img class="photo1" src="images/20170522_124602.jpg">
<h3 class="date1">22-05-2017</h3>
</div>
<div class="mySlides1 fade">
<img class="photo1" src="images/20170419_020725.jpg">
<h3 class="date1">19-04-2017</h3>
</div>
<div class="mySlides1 fade">
<img class="photo1" src="images/20170520_115819.jpg">
<h3 class="date1">20-05-2017</h3>
</div>
<div class="mySlides1 fade">
<img class="photo1" src="images/20170822_011703.jpg">
<h3 class="date1">22-08-2017</h3>
</div>
<div class="mySlides1 fade">
<img class="photo1" src="images/20170705_184344.jpg">
<h3 class="date1">05-07-2017</h3>
</div>
</div>
&#13;
答案 0 :(得分:0)
setInterval()你没有停止间隔
添加:
var slideInterval;
并改变:
$('.play').click(function(){
if (stop == true) {
stop = false;
alert(stop);
clearInterval(slideInterval);
}
else {
stop = true;
alert(stop);
slideInterval = setInterval(swapSlides, 3000);
}
});
并且不需要:
if(stop == false){
setInterval(swapSlides, 3000);
}