我在这里有一个非常基本的滑块:http://codepen.io/Abbogaming1353/pen/CxgIE
问题是,如果幻灯片少于三张,动画就会混乱,我可以在动画期间看到背景,而不是下一张幻灯片顺畅地推动第一张幻灯片。如果我只使用两张幻灯片,即使在原始滑块中也会发生这种情况我会附上我编辑过的代码,这样你就可以看到我发生了什么:
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script src='http://codepen.io/assets/libs/fullpage/jquery.js'></script>
<script src="index.js"></script></head>
<style type="text/css">
#sliderBackground
{
width: 968px;
height: 370px;
margin: 0 auto;
padding: 0;
background: #0CC;
}
#slider
{
position: relative;
overflow: hidden;
width: 940px;
height: 340px;
margin: 5px auto 0;
}
#slider ul
{
position: relative;
margin: 0;
padding: 0;
height: 200px;
list-style: none;
}
#slider ul li
{
position: relative;
display: block;
float: left;
margin: 0;
padding: 0;
width: 940px;
height: 340px;
background: #ccc;
text-align: center;
line-height: 340px;
}
a.control_prev, a.control_next
{
position: absolute;
top: 40%;
z-index: 999;
display: block;
padding: 4% 3%;
width: auto;
height: auto;
background: #2a2a2a;
color: #fff;
text-decoration: none;
font-weight: 600;
font-size: 18px;
opacity: 0.8;
cursor: pointer;
}
</style>
<body>
<div id="sliderBackground">
<div id="slider">
<ul>
<li>SLIDE 1</li>
<li>SLIDE 2</li>
</ul>
</div>
</div>
</body>
</html>
使用Javascript:
jQuery(document).ready(function ($) {
setInterval(function () {
moveRight();
}, 3000);
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();
});
});
我认为问题出在JavaScript上,但我不知道从哪里开始。
答案 0 :(得分:0)
替换此行
$('#slider ul').css({ width: sliderUlWidth, marginLeft: - slideWidth });
有了它,它将起作用
$('#slider ul').css({ width: sliderUlWidth});
修改2
替换此行
$('#slider').css({ width: slideWidth, height: slideHeight });
$('#slider ul').css({ width: sliderUlWidth});
$('#slider ul li:last-child').prependTo('#slider ul'); // this line to be changed
到此
$('#slider').css({ width: slideWidth, height: slideHeight });
$('#slider ul').css({ width: sliderUlWidth});
$('#slider ul li:first-child').prependTo('#slider ul'); // from :last-child to :first-child
答案 1 :(得分:0)
Vitorino的解决方案确实适用于Moveright,但如果向左移动则会遇到同样的问题。我建议你打开检查员并搜索“ul”然后你会看到你的ul中只有2个元素,你的ul总是在左侧附加了li。 所以你需要把.css放在每个按钮的.click事件中。
jQuery(document).ready(function ($) {
$('#checkbox').change(function(){
setInterval(function () {
moveRight();
}, 3000);
});
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 });
if(slideCount>2){
$('#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();
});
}else
{
$('a.control_prev').hide();
$('#slider ul').css({ width: sliderUlWidth});
function moveLeft2() {
$('#slider ul').animate({
left: 0
}, 200);
$('a.control_prev').hide();
$('a.control_next').show();
};
function moveRight2() {
$('#slider ul').animate({
left: -slideWidth
}, 200);
$('a.control_prev').show();
$('a.control_next').hide();
};
$('a.control_prev').click(function () {
moveLeft2();
});
$('a.control_next').click(function () {
moveRight2();
});
}
});
这样当你点击右边时,ul将处于正常位置(2. li将位于右侧)。 我希望你理解