我想使用JQuery实现垂直幻灯片放映。我正在使用一个垂直卷轴(div),它依次包含图像我每隔2秒按图像尺寸递减卷轴顶部位置,这是正常工作但当我到达最后一个图像时,我无法实现从第一张图像开始卷轴的逻辑。如果我的解释不清楚,我将附加我的代码:
<script type="text/javascript">
$(function() {
setInterval(SlideImage, 2000);
});
function SlideImage() {
$("#imgReel").animate({ "marginTop": "-=300px" }, 1000, function() { });
}
</script>
<style type="text/css">
img
{
width: 600px;
height: 300px;
}
.imgContainer
{
overflow: hidden;
width: 620px !important;
height: 300px !important;
}
div
{
width: 620px;
}
.current
{
z-index: 0;
}
.next
{
z-index: 1;
}
.imgReel
{
width: 620px !important;
}
.
</style>
<body>
<div id="imgContainer" class="imgContainer">
<div id="imgReel" class="imgReel">
<div class="current">
<img src="Images/Summerwave.jpg" />
</div>
<div>
<img src="Images/Sunlight_and_the_Wild_Forest_Floor.jpg" />
</div>
<div>
<img src="Images/Sunset.jpg" />
</div>
<div>
<img src="Images/Swimming_with_the_fishys.jpg" />
</div>
<div>
<img src="Images/Tearing_Apart.jpg" />
</div>
<div>
<img src="Images/Teaser.jpg" />
</div>
<div>
<img src="Images/Terra_Nova.jpg" />
</div>
</div>
</div>
非常感谢任何帮助
答案 0 :(得分:1)
您可以使用其他方法:修复#imgReel
,并调整元素大小:
将imgReel移动到底部,如果你对顶部的元素执行slideUp
,在最后一个元素(最后一个元素之后)的第一个元素之前追加,那么你将拥有卷轴移动的效果,首先消失在当前图像的顶部。
在代码中,使用插件scrollTo:
var $reel = null;
function init() {
$reel = $("#imgReel")
// Keep the first element as first shown, now that we're going to take the reel to the bottom
$reel.children("div:first").insertAfter($reel.children("div:last"))
// Move to the bottom using scrollTo or whatever you want:
$reel.scrollTo("max")
}
function next() {
// Clone the first element (not visible) at the end
var $topElement = $reel.children("div:first")
var $bottomElement = $topElement.clone().appendTo($reel)
// Hide the top element (cloned now at the bottom) and remove it when it's done
$topElement.slideUp(1000, function() { $(this).remove() })
// Mark the next 'current'
$reel.children(".current").removeClass("current");
$bottomElement.addClass("current");
}
另一方面,如果你不想进行无限循环,并保持你的方法,使用scrollTo就像这样简单:
funnction next() {
var $current = $reel.find("div.current").removeClass("current")
var $next = $current.next()
if(!$next.length) $next = $reel.children("div:first")
$reel.scrollTo($next.addClass("current"), 1000)
}
function prev() {
var $current = $reel.find("div.current").removeClass("current")
var $next = $current.prev()
if(!$next.length) $next = $reel.children("div:last")
$reel.scrollTo($next.addClass("current"), 1000)
})
希望它有所帮助: - )
答案 1 :(得分:0)
您可以移回卷轴顶部,也可以将查看过的div
移动到卷轴的末端。
回到开始你可以通过将盒子褪色到某种颜色,移动到开头(用户看不到这种情况),并淡出颜色。这样,用户就知道他正在再次查看相同的图片。