Contohnya:
程序代码
<!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 ">
<kepala>
<meta http-equiv="Jenis Konten" content="teks/html; charset=gb2312" />
<title>JavaScript 无缝上下左右滚动加定高定宽停顿效果(兼容ie/ff)</title>
</kepala>
<tubuh>
<Jenis SKRIP=teks/javascript>
var $ = fungsi (id) {
kembalikan "string" == typeof id ? dokumen.getElementById(id) : id;
};
var Kelas = {
buat: fungsi() {
fungsi kembali() {
this.initialize.apply(ini, argumen);
}
}
}
Objek.extend = function(tujuan, sumber) {
for (properti var di sumber) {
tujuan[properti] = sumber[properti];
}
tujuan kembali;
}
fungsi addEventHandler(oTarget, sEventType, fnHandler) {
if (oTarget.addEventListener) {
oTarget.addEventListener(sEventType, fnHandler, false);
} else if (oTarget.attachEvent) {
oTarget.attachEvent("aktif" + sEventType, fnHandler);
} kalau tidak {
oTarget["aktif" + sEventType] = fnHandler;
}
};
var Scroller = Kelas.buat();
Scroller.prototipe = {
inisialisasi: function(idScroller, idScrollMid, options) {
var oIni = ini, oScroller = $(idScroller), oScrollMid = $(idScrollMid);
this.SetOptions(pilihan);
ini.Sisi = ini.pilihan.Sisi || ["naik"];//方向
this.scroller = oScroller; //对象
this.speed = this.options.Speed; //速度
this.timer = null; //时间
this.pauseHeight = 0; //定高
this.pauseWidth = 0; //定宽
ini.jeda = 0; //定高(宽)
ini.sisi = 0; //参数
//用于上下滚动
this.heightScroller = parseInt(oScroller.style.height) || oScroller.offsetHeight;
this.heightList = oScrollMid.offsetHeight;
//用于左右滚动
this.widthScroller = parseInt(oScroller.style.width) || oScroller.offsetWidth;
this.widthList = oScrollMid.offsetWidth;
//js取不到css设置的height和width
oScroller.style.overflow = "tersembunyi";
oScrollMid.appendChild(oScrollMid.cloneNode(benar));
oScrollMid.appendChild(oScrollMid.cloneNode(benar));
addEventHandler(oScroller, "mouseover", function() { oThis.Stop(); });
addEventHandler(oScroller, "mouseout", function() { oThis.Start(); });
ini.Mulai();
},
//设置默认属性
SetOptions: fungsi(opsi) {
this.options = {//默认值
Langkah: 1,//每次变化的px量
Kecepatan: 20,//速度(越大越慢)
Samping: ["atas"],//滚动方向:"naik"是上,"bawah"是下,"kiri"是左,"kanan"是右
PauseHeight: 0,//隔多高停一次
Lebar Jeda: 0,//隔多宽停一次
// Bagaimana cara menggunakan PauseHeight dan PauseWidth?
PauseStep: 1000//停顿时间(PauseHeight或PauseWidth大于0该参数才有效)
};
Objek.memperluas(ini.pilihan, pilihan || {});
},
//转向
Putar: fungsi() {
//通过设置方向数组的排列来转向
this.Side.push(ini.Side.shift().toLowerCase());
},
//上下滚动
Gulir ke Atas: fungsi() {
this.pause = this.pauseHeight;
this.scroller.scrollTop = this.GetScroll(ini.scroller.scrollTop, this.heightScroller, this.heightList, this.options.PauseHeight);
this.pauseHeight = ini.jeda;
var oIni = ini;
this.timer = window.setTimeout(function(){ oThis.Start(); }, this.speed);
},
//左右滚动
GulirKiriKanan: fungsi() {
this.pause = this.pauseWidth;
//注意:scrollLeft超过1400会自动变回1400 注意长度
this.scroller.scrollLeft = ini.GetScroll(ini.scroller.scrollLeft, this.widthScroller, this.widthList, this.options.PauseWidth);
this.pauseWidth = ini.jeda;
var oIni = ini;
this.timer = window.setTimeout(function(){ oThis.Start(); }, this.speed);
},
//获取设置滚动数据
GetScroll: fungsi(iScroll, iScroller, iList, iPause) {
var iStep = ini.pilihan.Langkah * ini.sisi;
if(sisi ini > 0){
if(iScroll >= (iList * 2 - iScroller)){ iScroll -= iList; }
} kalau tidak {
jika(iGulir <= 0){ iGulir += iList; }
}
ini.kecepatan = ini.pilihan.Kecepatan;
jika(iJeda > 0){
if(Matematika.abs(ini.jeda) >= iPause){
this.speed = this.options.PauseStep; this.pause = iStep = 0; ini.Putar();
} kalau tidak {
ini.jeda += iStep;
}
}
kembali (iScroll + iStep);
},
//开始
Mulai: function() {
//document.getElementById("test").innerHTML+=sTurn+",";
//方向设置
beralih (ini.Sisi[0].toLowerCase()) {
kasus "benar":
if(this.widthList < this.widthScroller) kembali;
ini.sisi = -1;
ini.ScrollLeftRight();
merusak;
kasus "kiri" :
if(this.widthList < this.widthScroller) kembali;
ini.sisi = 1;
ini.ScrollLeftRight();
merusak;
kasus "turun":
if(this.heightList < this.heightScroller) kembali;
ini.sisi = -1;
ini.ScrollUpDown();
merusak;
kasus "naik":
bawaan :
if(this.heightList < this.heightScroller) kembali;
ini.sisi = 1;
ini.ScrollUpDown();
}
},
//停止
Berhenti: fungsi() {
clearTimeout(ini.timer);
}
};
</SKRIP>
<GAYA>
.Scroller {tinggi garis:50px; batas:1 piksel padat #000000; bantalan:0px 10px; tinggi:50 piksel; lebar:400 piksel;}
.Penggulung *{margin:0px; bantalan:0 piksel;}
.ScrollMid {float:kiri;}
.ScrollMid ul{lebar:800px;float:kiri;}
.ScrollMid li{list-style:none; mengapung:kiri; lebar:390 piksel; padding-kiri:10px;tinggi garis:50px; }
</STYLE>
<DIV class=Penggulung id=idPenggulung>
<DIV style="LEBAR: 1600 piksel">
<kelas DIV=ScrollMid id=idScrollMid>
<UL>
<LI>顺德于1993年被批准为广东省综合改革试点。 </LI>
<LI>2006年顺德成为首个PDB 1000亿的县级行政单位。 </LI>
<LI>2000至2003年顺德均在中国百强县排名中位居榜首。 </LI>
<LI>2005年顺德实现国内生产总值856.11亿元。 </LI></UL></DIV></DIV></DIV>
<SKRIP>
Scroller baru("idScroller", "idScrollMid",{ Sisi:["atas","kiri"], PauseHeight:50, PauseWidth:400 });
</SKRIP>
</tubuh>
</html>