如果我理解正确,您正在寻找这样的效果:
http://jsfiddle.net/2RRWS/
我的代码采用 html 结构,如:
<div id="scrollContainer">
<p>Some text</p>
<p>More text</p>
...
</div>
和一些 CSS 来适当地设置包含 div 的宽度/高度。它还为“变暗”和“突出显示”段落假设了一些类。
可能有一种更清洁的方法可以做到这一点,但这只是我拼凑起来的,它似乎有效,所以......
var $container = $("#scrollContainer"),
$ps = $container.find("p"),
containerHeight = $container.height(),
contentHeight = 0,
scrollTop = 0;
// figure out the height of the content
$ps.each(function() {
contentHeight += $(this).outerHeight();
});
// add some blank space at the beginning and end of the content so that it can
// scroll in from the bottom
$("<div></div>").css("height", 400).appendTo($container).clone().prependTo($container);
setInterval(function() {
if (paused)
return;
// if we've gone off the end start again
if (scrollTop > contentHeight + containerHeight)
scrollTop = 0;
// scroll up slightly
$container.scrollTop(scrollTop++);
$ps.removeClass("highlighted") // for each paragraph...
.addClass("dimmed") // dim it
.each(function() { // unless it is in view
var $this = $(this),
top = $this.position().top,
height = $this.height();
if (top > 0 && top + height < containerHeight)
$(this).addClass("highlighted").removeClass("dimmed");
});
}, 20);
$container.hover(function() {
paused = true;
}, function() {
paused = false;
});
编辑:更新以根据评论实现“暂停”功能。http://jsfiddle.net/2RRWS/8/