<html>
<head>
<title></title>
<script language="javascript" type="text/javascript">
function VScrollPanel() {
this.width = null; //滾動範圍寬度
this.height = null; //滾動範圍高度
this.delay = 100; //時間延遲
this.step = 1; //單步滾動像素
}
VScrollPanel.prototype = {
//滾動指定ID的內容
Bind: function (domID) {
this.dom = document.getElementById(domID);
if (this.width) {
this.dom.style.width = this.width + "px";
}
if (this.height) {
this.dom.style.height = this.height + "px";
}
this.dom.style.overflow = "hidden";
this.Scrolling();
},
//滾動方法
Scrolling: function () {
//如果內容太少,已經完全可以顯示,不需滾動
if (this.dom.scrollHeight <= this.dom.clientHeight) return;
//滾動到最後一項,切換位置
if (this.willSwap) {
this.dom.appendChild(this.lastOne);
this.willSwap = false;
}
//一個對象自身的引用
var _this = this;
if (this.dom.scrollTop+ this.step + this.dom.clientHeight
>= this.dom.scrollHeight) {
//滾動到達底部
//刪除臨時添加的首項
if (this.tmp != null) this.dom.removeChild(this.tmp);
//複製第一項,即將移至最後一項
this.lastOne = this.dom.firstChild;
this.tmp = this.lastOne.cloneNode(true);
//添加第一項
//this.lastOne.replaceNode(this.tmp);
//在此用replaceChild替代replaceNode即可兼容FF
this.dom.replaceChild(this.tmp,this.lastOne);
/*至於爲什麼要複製第一項,並臨時插入
是因爲如果在滾動內容剛好足夠滾動時,如果沒有這個臨時項,
直接移動第一項到最後一項的時候,會發生跳動現象。
也就是,比如:如果有4項,不會發生滾動,而如果有5項的時候,就發生這種現象
但6項以上的時候,正常*/
this.willSwap = true;
}
this.dom.scrollTop += this.step;
setTimeout(function () {
/*此處必須用 _this 代替 this 否則錯誤,原因在此不闡述*/
_this.Scrolling();
}, this.delay);
},
Pause: function () {
this.pauseStep = this.step;
this.step = 0;
},
Continue: function () {
this.step = this.pauseStep;
}
};
/*經過多次實驗,水平滾動通過新建一個表格來設置佈局
才能最有效的保證佈局不會亂,不會發生換行
所以只好水平、垂直滾動的分開做了*/
//水平滾動。以下代碼思路同上,不再註釋
function HScrollPanel() {
this.width = null;
this.height = null;
this.delay = 100;
this.step = 1;
}
HScrollPanel.prototype = {
Bind: function (domID) {
this.dom = document.getElementById(domID);
this.rootDom = document.createElement("table");
this.rootDom.border = "0";
this.rootDom.cellPadding = "0";
this.rootDom.cellSpacing = "0";
var tbody = document.createElement("tbody");
this.rootDom.appendChild(tbody);
this.row = document.createElement("tr");
tbody.appendChild(this.row);
var child = this.dom.firstChild;
while (child != null) {
this.dom.removeChild(child);
if (child.nodeType == 1) {
var td = document.createElement("td");
td.vAlign = "top";
td.appendChild(child);
this.row.appendChild(td);
}
child = this.dom.firstChild;
}
this.dom.appendChild(this.rootDom);
if (this.width) {
this.dom.style.width = this.width + "px";
}
if (this.height) {
this.dom.style.height = this.height + "px";
}
this.dom.style.overflow = "hidden";
this.Scrolling();
},
Scrolling: function () {
if (this.dom.scrollWidth <= this.dom.clientWidth) return;
if (this.willSwap) {
this.row.appendChild(this.lastOne);
this.willSwap = false;
}
var _this = this;
if (this.dom.scrollLeft + this.step + this.dom.clientWidth
>= this.dom.scrollWidth) {
if (this.tmp != null) {
this.row.removeChild(this.tmp);
}
this.lastOne = this.row.firstChild;
this.tmp = this.lastOne.cloneNode(true);
//this.lastOne.replaceNode(this.tmp);
this.row.replaceChild(this.tmp,this.lastOne);
this.willSwap = true;
}
this.dom.scrollLeft += this.step;
setTimeout(function () { _this.Scrolling(); }, this.delay);
},
Pause: function () {
this.pauseStep = this.step;
this.step = 0;
},
Continue: function () {
this.step = this.pauseStep;
}
};
</script>
<style type="text/css">
body,td
{
font-family:"宋體";
font-size:12px;
}
.item
{
width:150px;
background-color:#f0f0f0;
text-align:center;
margin:5px;
padding:3px;
}
</style>
</head>
<body>
垂直滾動示例<input type="text" value="100" οnchange="SetDelay(this);" />
<div id="divVScroll" οnmοuseοver="vsp.Pause();" οnmοuseοut="vsp.Continue()">
<div class="item">AAAAAA</div>
<div class="item">BBBBBB</div>
<div class="item">CCCCCC</div>
<div class="item">DDDDDD</div>
<div class="item">EEEEEE</div>
<div class="item">FFFFFF</div>
<div class="item">GGGGGG</div>
</div>
<script language="javascript" type="text/javascript">
var vsp = new VScrollPanel();
vsp.height = 100;
vsp.Bind("divVScroll");
function SetDelay(tb){
vsp.delay=tb.value
}
</script>
<br /><br />
水平滾動示例
<div id="divHScroll" οnmοuseοver="hsp.Pause();" οnmοuseοut="hsp.Continue()">
<div class="item">AAAAAA</div>
<div class="item">BBBBBB</div>
<div class="item">CCCCCC</div>
<div class="item">DDDDDD</div>
<div class="item">EEEEEE</div>
<div class="item">FFFFFF</div>
<div class="item">GGGGGG</div>
</div>
<script language="javascript" type="text/javascript">
var hsp = new HScrollPanel();
hsp.width = 400;
hsp.delay = 10;
hsp.Bind("divHScroll");
</script>
</body>
</html>