我使用Jquery制作了一个滑块,它可以正常运行,但是一旦它到达最后一张幻灯片,它就不会滑到第一张幻灯片,而只是瞬间显示第一张幻灯片。它也不会停留在最后一张幻灯片上,即使是一秒钟。
HTML:
<body>
<div id="container">
<div id="header">
<h3>J-Slider</h3>
</div>
<div class="slider">
<ul>
<li>
<img width="750px" height="400px" src="http://cdn.wonderfulengineering.com/wp-content/uploads/2014/04/space-wallpapers-1.jpg">
</li>
<li>
<img width="750px" height="400px" src="http://cdn.wonderfulengineering.com/wp-content/uploads/2014/04/space-wallpapers-13.jpg">
</li>
<li>
<img width="750px" height="400px" src="http://th08.deviantart.net/fs70/PRE/f/2014/071/5/5/blue_space_by_whendell-d79zabi.jpg">
</li>
</ul>
</div>
</div>
</body>
CSS:
body {
font-family:Gerogia;
font-size:15px;
}
#container {
width:930px;
margin:50px auto 10px auto;
border-left:#666 solid 3px;
border-right:#666 solid 3px;
background:#f5f5f5;
padding:20px 30px;
}
#header {
padding:10px 0;
border-bottom:#ccc solid 1px;
overflow:hidden;
text-align:center;
}
h3 {
font-size: 30px;
text-transform: uppercase;
letter-spacing: 2px;
}
.slider {
width: 750px;
height: 400px;
padding-top: 10px;
margin-left: 75px;
overflow: hidden;
}
.slider ul {
width:8000px;
list-style-type:none;
}
.slider li {
float: left;
}
JQuery的:
$('document').ready(function () {
var width = 750;
var animationSpeed = 1000;
var pause = 3000;
var currentSlide = 1;
var $slider = $('.slider');
var $slideContainer = $slider.find('ul');
var $slides = $slideContainer.find('li');
setInterval(function () {
$slideContainer.animate({
'margin-left': '-=' + width + 'px'
}, animationSpeed, function () {
currentSlide++;
if (currentSlide === $slides.length) {
currentSlide = 1;
$slideContainer.css('margin-left', 0);
}
});
}, pause);
});
答案 0 :(得分:2)
我稍微更改了你的代码,我把第一个元素的副本放在最后一个位置,并稍微修改了你的代码
$('document').ready(function () {
var width = 750;
var animationSpeed = 1000;
var pause = 3000;
var currentSlide = 1;
var $slider = $('.slider');
var $slideContainer = $slider.find('ul');
var $slides = $slideContainer.find('li');
setInterval(function () {
$slideContainer.animate({
'margin-left': '-=' + width + 'px'
}, animationSpeed, function () {
currentSlide++;
if (currentSlide >= $slides.length) {
currentSlide = 1;
$slideContainer.css('margin-left', 0);
}
});
}, pause);
});
更改
if (currentSlide === $slides.length)
要:
if (currentSlide >= $slides.length)
示例:jsFiddle
更新:左侧图像边框问题
为了完成所有工作,我修改了你的css(将 UL 的左边距放到0px)
.slider ul {
width:8000px;
list-style-type:none;
padding-left: 0px
}
更新了jsFiddle:jsFiddle
答案 1 :(得分:0)
尝试更改幻灯片的增量方式和时间,以及将其重置为一个时。 在你当前的js中,你增加到最后一张幻灯片,然后告诉它立即回到一张
$('document').ready(function () {
var width = 750;
var animationSpeed = 1000;
var pause = 3000;
var currentSlide = 1;
var $slider = $('.slider');
var $slideContainer = $slider.find('ul');
var $slides = $slideContainer.find('li');
setInterval(function () {
$slideContainer.animate({
'margin-left': '-=' + width + 'px'
}, animationSpeed, function () {
if (currentSlide === $slides.length) {
currentSlide = 1;
$slideContainer.css('margin-left', 0);
} else {
currentSlide++; // move this down here
}
});
}, pause);
});
答案 2 :(得分:0)
我不是Javascript的专家,但我认为我认为你的问题。
当你制作动画时,你会执行currentSlide ++,并且在你进行检查之后(currentSlide == $ slides),所以你迭代你的currentSlide并在你告诉他改为第一个之后。就像之前说的mmseasor一样。
因为你没有返回问题是因为在if循环中的动作你忘记为slideContainer设置动画,所以它只是显示。
答案 3 :(得分:0)
请检查jsfiddle代码。或低于代码
也许是你的解决方案。
$('document').ready(function () {
var width = 750;
var animationSpeed = 1000;
var pause = 3000;
var currentSlide = 1;
var $slider = $('.slider');
var $slideContainer = $slider.find('ul');
var $slides = $slideContainer.find('li');
setInterval(function () {
$slideContainer.animate({
'margin-left': '-=' + width + 'px'
}, animationSpeed, function () {
currentSlide++;
if (currentSlide === $slides.length) {
currentSlide = 1;
//$slideContainer.css('margin-left', 0);
$slideContainer.animate({ 'margin-left': 0
}, pause );
}
});
}, pause);
});