首頁的新聞快訊區常需要無間斷滾動,而且滑鼠移入時要能暫停。下面這個 jQuery.roll 插件模擬了百度新聞的滾動效果,同時支援文字與圖片、水平與垂直兩個方向。
/* * @module jQuery roll * @param contentCls 內容容器的 className * @param contentParentId 內容容器父元素的節點 ID * @param configs 設定參數 * * @config effect 滾動效果:'scrollX' 水平 / 'scrollY' 垂直 * @config duration 滾動 1 個像素的運行時間(毫秒) * @config delay 開始滾動的延遲時間(毫秒) */ jQuery.roll(contentCls, contentParentId, configs);
核心思路是:把內容整塊複製一份接在後面,兩塊輪流位移,當第一塊完全移出視野時立刻把它跳回尾端,視覺上就永遠沒有斷層。
jQuery.extend({
roll: function(contentCls, contentParentId, configs){
var setTimeID, totalWidth = 0, totalHeight = 0,
firstContent, secondContent, contents;
(function(){
var singleContent, cloneContent, nodeList;
singleContent = $(contentCls, contentParentId);
nodeList = singleContent.children();
if (configs.effect === 'scrollX') {
$.each(nodeList, function(idx, itm) { totalWidth += $(itm).outerWidth(true); });
singleContent.css({ 'width': totalWidth + 'px' });
}
else if (configs.effect === 'scrollY') {
$.each(nodeList, function(idx, itm) { totalHeight += $(itm).outerHeight(true); });
singleContent.css({ 'height': totalHeight + 'px' });
}
cloneContent = singleContent.clone();
cloneContent.appendTo(contentParentId);
contents = $(contentCls, contentParentId);
firstContent = contents[0];
secondContent = contents[1];
if (configs.effect === 'scrollX') {
$(firstContent).css({ 'left': 0 });
$(secondContent).css({ 'left': totalWidth + 'px' });
}
else if (configs.effect === 'scrollY') {
$(firstContent).css({ 'top': 0 });
$(secondContent).css({ 'top': totalHeight + 'px' });
}
})()
function cssAnimate(){
if (configs.effect === 'scrollX') {
$(firstContent).css({ left: parseInt(firstContent.style.left, 10) - 1 + 'px' });
$(secondContent).css({ left: parseInt(secondContent.style.left, 10) - 1 + 'px' });
$.each(contents, function(idx, itm) {
if (parseInt(itm.style.left, 10) === -totalWidth) {
$(itm).css({ left: totalWidth + 'px' }); // 移出視野後跳回尾端
}
});
}
else if (configs.effect === 'scrollY') {
$(firstContent).css({ top: parseInt(firstContent.style.top, 10) - 1 + 'px' });
$(secondContent).css({ top: parseInt(secondContent.style.top, 10) - 1 + 'px' });
$.each(contents, function(idx, itm) {
if (parseInt(itm.style.top, 10) === -totalHeight) {
$(itm).css({ top: totalHeight + 'px' });
}
});
}
setTimeId = setTimeout(cssAnimate, configs.duration);
}
function rollRun(){ setTimeId = setTimeout(cssAnimate, configs.delay); return jQuery; }
function rollStop(){ clearTimeout(setTimeId); return jQuery; }
return $.extend({ rollRun: rollRun, rollStop: rollStop });
}
});
<style>
body { font: 12px/1.5 tahoma,"microsoft yahei"; }
body, div, ul, li, h1 { margin: 0; padding: 0; }
.news { margin: 100px 0 0 100px; }
.news ul { list-style: none; }
.news-box {
width: 600px; margin-left: 20px; height: 24px;
background-color: #fcfcfd; overflow: hidden; position: relative; _zoom: 1;
}
.news h1 { margin-bottom: 15px; padding-left: 20px; color: #370188; }
.news-list { position: absolute; float: left; }
.news-list li {
float: left; _display: inline; margin-right: 15px;
height: 24px; line-height: 24px; overflow: hidden; word-wrap: normal;
}
.news-list li a { text-decoration: none; color: #000; }
.news-list li a:hover {
transition: color .2s linear, background-color .3s linear;
color: #FF4400; text-decoration: underline;
}
.news-list li a:visited { color: #290065; }
</style>
<div id="J_news" class="news">
<h1>文字列表滾動</h1>
<div id="J_Roll_Container" class="news-box">
<ul class="J_Roll_Content news-list">
<li><a href="#">第一條新聞標題</a></li>
<li><a href="#">第二條新聞標題</a></li>
<li><a href="#">第三條新聞標題</a></li>
</ul>
</div>
</div>
<script>
// 此處貼上 jQuery.roll 的程式碼
</script>
<script>
$(function(){
var roll_jQuery, contents;
roll_jQuery = $.roll('.J_Roll_Content', '#J_Roll_Container',
{ effect: 'scrollX', duration: 40, delay: 500 }).rollRun();
contents = $('.J_Roll_Content');
contents.bind('mouseenter', function(){ roll_jQuery.rollStop(); }); // 移入暫停
contents.bind('mouseleave', function(){ roll_jQuery.rollRun(); }); // 移出續跑
});
</script>
垂直版本(例 3.2)與水平版本幾乎相同,只要做兩處調整:
effect 改成 'scrollY'。.news-box { padding:20px; width:310px; height:192px; overflow:hidden; position:relative; },每個 li 佔滿一行(width:100%; height:24px; line-height:24px;)。滑鼠移入暫停、移出繼續的綁定方式與水平版完全相同,直接沿用即可。
這個插件以每次位移 1 像素搭配 setTimeout 達到平滑效果,好處是速度可以透過 duration 精細控制,而且天然不會有間隙;代價是會持續佔用計時器。若只是幾條靜態公告,用 CSS3 animation 會更省資源;需要動態增刪內容或與後端互動時,這類 JS 方案仍然比較有彈性。