打破我在这里有一个非常基本的滑块,我得到了来自: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,但我不知道从哪里开始。
这工作,除了现在幻灯片2显示第一。不只是幻灯片2,但永远幻灯片是最后一个。如果有4张幻灯片,它会进入幻灯片4,然后进入幻灯片1(这应该发生,我只是希望它从幻灯片1开始)。感谢您的帮助,了解幻灯片现在失灵的原因吗? – crazyflower432 2014-09-19 17:36:58
我已编辑我的答案 – 2014-09-21 08:15:58
谢谢,这很容易。 – crazyflower432 2014-09-22 13:14:37