【发布时间】:2010-04-09 17:35:10
【问题描述】:
瞄准
目标是拥有一个具有固定高度和宽度的容器 DIV,并让该 DIV 中的 HTML 内容自动连续垂直滚动。
问题 基本上我已经使用jQuery创建了下面的代码来垂直向上滚动(移动)子DIV,直到它在动画完成的边界父框之外触发事件处理程序,该事件处理程序重置子DIV的位置并再次启动该过程.
这很好,所以内容向上滚动留下一个空白区域,然后再次从底部开始向上滚动。
我的问题是这个要求是让内容看起来好像在不断重复,看下图更好地解释这一点,有没有办法做到这一点? (我不想使用 jQuery 以外的第三方插件或库):
(来源:cameroncooke.com)
我目前所拥有的
HTML:
<div id="scrollingContainer">
<div class="scroller">
<h1>This is a title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse at orci mi, id gravida tellus. Integer malesuada ante sit amet enim pulvinar congue. Donec pulvinar dolor et arcu posuere feugiat id et felis.</p>
<p>More content....</p>
</div>
</div>
CSS:
#scrollingContainer{
height: 300px;
width: 300px;
overflow: hidden;
}
#scrollingContainer DIV.scroller{
position: relative;
}
JavaScript:
/**
* Scrolls the content DIV
*/
function scroll() {
if($('DIV.scroller').height() > $('#scrollingContainer').height()) {
var t = $('DIV.scroller').position().top + $('DIV.scroller').height();
/* Animate */
$('DIV.scroller').animate(
{
top: '-=' + t + 'px'
}
, 4000, 'linear', animationComplete);
}
}
function animationComplete() {
$(this).css('top', $('#scrollingContainer').height());
scroll();
}
【问题讨论】:
标签: javascript jquery jquery-ui animation scroll