DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> jquery 多行滾動代碼(附詳細解釋)
jquery 多行滾動代碼(附詳細解釋)
編輯:JQuery特效代碼     
代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
<style type="text/css">
ul,li{margin:0;padding:0}
#scrollDiv{width:300px;height:100px;min-height:25px;line-height:25px;border:#ccc 1px solid;overflow:hidden}
#scrollDiv li{height:25px;padding-left:10px;}
</style>
<script type="text/javascript" src="jquery-1.1.3.pack.js"></script>
<script type="text/javascript">
(function($){
$.fn.extend({
Scroll:function(opt,callback){
//參數初始化
if(!opt) var opt={};
var _btnUp = $("#"+ opt.up);//Shawphy:向上按鈕
var _btnDown = $("#"+ opt.down);//Shawphy:向下按鈕
var timerID;
var _this=this.eq(0).find("ul:first");
var lineH=_this.find("li:first").height(), //獲取行高
line=opt.line?parseInt(opt.line,10):parseInt(this.height()/lineH,10), //每次滾動的行數,默認為一屏,
即父容器高度
speed=opt.speed?parseInt(opt.speed,10):500; //卷動速度,數值越大,速度越慢(毫秒)
timer=opt.timer //?parseInt(opt.timer,10):3000; //滾動的時間間隔(毫秒)
if(line==0) line=1;
var upHeight=0-line*lineH;
//滾動函數
var scrollUp=function(){
_btnUp.unbind("click",scrollUp); //Shawphy:取消向上按鈕的函數綁定
_this.animate({
marginTop:upHeight
},speed,function(){
for(i=1;i<=line;i++){
_this.find("li:first").appendTo(_this);
}
_this.css({marginTop:0});
_btnUp.bind("click",scrollUp); //Shawphy:綁定向上按鈕的點擊事件
});
}
//Shawphy:向下翻頁函數
var scrollDown=function(){
_btnDown.unbind("click",scrollDown);
for(i=1;i<=line;i++){
_this.find("li:last").show().prependTo(_this);
}
_this.css({marginTop:upHeight});
_this.animate({
marginTop:0
},speed,function(){
_btnDown.bind("click",scrollDown);
});
}
//Shawphy:自動播放
var autoPlay = function(){
if(timer)timerID = window.setInterval(scrollUp,timer);
};
var autoStop = function(){
if(timer)window.clearInterval(timerID);
};
//鼠標事件綁定
_this.hover(autoStop,autoPlay).mouseout();
_btnUp.css("cursor","pointer").click( scrollUp ).hover(autoStop,autoPlay);//Shawphy:向上向下鼠標事件綁定
_btnDown.css("cursor","pointer").click( scrollDown ).hover(autoStop,autoPlay);
}
})
})(jQuery);
$(document).ready(function(){
$("#scrollDiv").Scroll({line:4,speed:500,timer:3000,up:"btn2",down:"btn1"});
});
</script>
</head>
<body>
<p>多行滾動演示:</p>
<div id="scrollDiv">
<ul>
<li>這是公告標題的第一行</li>
<li>這是公告標題的第二行</li>
<li>這是公告標題的第三行</li>
<li>這是公告標題的第四行</li>
<li>這是公告標題的第五行</li>
<li>這是公告標題的第六行</li>
<li>這是公告標題的第七行</li>
<li>這是公告標題的第八行</li>
</ul>
</div>
<span id="btn1">down</span>
<br/>
<span id="btn2">up</span>

</body>
</html>

這段代碼只是多行滾動。

1。首先一個<div id="scrollDiv">高度限制是100px,而每行是25px,總共只能顯示4行,但div中有八行。那怎麼顯示呢?

scrollDiv屬性中有個overflow:hidden表示不能顯示的則隱藏。

2。第二個問題是 哪四行顯示在<div id="scrollDiv">中,由於_this.find("li:first").appendTo(_this);將前面的四行剪切插入到後面,並且設置高度是從0開始,這樣就只能顯示前四行。_this.find("li:last").show().prependTo(_this);同理將後面四行插入到前面去。

3。第三個問題是animate,它是jquery的一個函數,起到動畫效應。但_btnDown.unbind("click",scrollDown);為何綁定了又要解開呢。這是因為當點擊down時,將執行scrollDown函數,當執行到裡面去時要解開綁定,以免此時再點擊down時又執行scrollDown函數,導致混亂,只有執行完滾動後再綁定。

4。當不點擊時,也會自動滾動。那時因為有個_this.hover(autoStop,autoPlay).mouseout();

jQuery中的hover方法是個非常常用的方法,接受二個參數,第一個參數為鼠標移入對象時觸發的事件,第二個參數為鼠標移出對象時觸發的事
5。$("#scrollDiv").Scroll({line:4,speed:500,timer:3000,up:"btn2",down:"btn1"});調用了Scroll 執行了:function(opt,callback),這裡面卻只傳了opt,這裡面沒有callback函數。而且傳參數也非常奇怪。不過就當它能這樣傳,相當於一個object,傳過來也。

希望懂得更多的拍磚。
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved