【发布时间】:2015-01-13 15:17:19
【问题描述】:
我有 3 个 div 可以制作幻灯片。我想要的是能够点击一个 div 并让它循环两次。即,当单击 div #1 时,它会滚动 div #2,继续,然后在 div#3 处停止 .... 或单击 div #2 并让它平滑滚动两次并在 div #1 处停止
现在我只能让它滚动一次到下一个 div。
这里有 2 个 jsfiddle 示例,虽然我无法让它在点击时滚动 2 个 div,但只有 1 个。
http://jsfiddle.net/jtbowden/ykbgT/2/ http://jsfiddle.net/jtbowden/ykbgT/
这是我的 HTML:
<div id="box1" class="box">Div #1</div>
<div id="box2" class="box">Div #2</div>
<div id="box3" class="box">Div #3</div>
</div>
和 CSS
body {
padding: 0px;
}
#container {
position: absolute;
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
overflow: hidden;
}
.box {
position: absolute;
width: 50%;
height: 300px;
line-height: 300px;
font-size: 50px;
text-align: center;
border: 2px solid black;
left: 50%;
top: 100px;
margin-left: -25%;
}
#box1 {
background-color: green;
left: -150%;
}
#box2 {
background-color: yellow;
}
#box3 {
background-color: red;
left: 150%;
}
和 JS:
$('.box').click(function() {
$('.box').each(function() {
if ($(this).offset().left < 0) {
$(this).css("left", "150%");
} else if ($(this).offset().left > $('#container').width()) {
$(this).animate({
left: '50%',
}, 500 );
} else {
$(this).animate({
left: '-150%',
}, 500 );
}
});
});
【问题讨论】:
-
我花时间用 css 来做这件事 :)
标签: jquery scroll jquery-animate slideshow