這是一個相當於小框架的輪播實作,拿來就能用,供大家參考。

如上圖所示:點擊左右兩個按鈕,可實現圖片向左或向右滾動,也能設定每隔幾秒自動滾動。
檔名只要和 HTML 中引入的一致即可:
var sina = {
$: function (objName) {
if (document.getElementById) {
return eval('document.getElementById("' + objName + '")')
} else {
return eval('document.all.' + objName)
}
},
isIE: navigator.appVersion.indexOf("MSIE") != -1 ? true : false,
addEvent: function (l, i, I) {
if (l.attachEvent) { l.attachEvent("on" + i, I) }
else { l.addEventListener(i, I, false) }
},
delEvent: function (l, i, I) {
if (l.detachEvent) { l.detachEvent("on" + i, I) }
else { l.removeEventListener(i, I, false) }
}
};
function ScrollPic(scrollContId, arrLeftId, arrRightId, dotListId) {
this.scrollContId = scrollContId;
this.arrLeftId = arrLeftId;
this.arrRightId = arrRightId;
this.dotListId = dotListId;
this.dotClassName = "dotItem";
this.dotOnClassName = "dotItemOn";
this.dotObjArr = [];
this.pageWidth = 0;
this.frameWidth = 0;
this.speed = 10;
this.space = 10;
this.pageIndex = 0;
this.autoPlay = true;
this.autoPlayTime = 5;
var _autoTimeObj, _scrollTimeObj, _state = "ready";
this.stripDiv = document.createElement("DIV");
this.listDiv01 = document.createElement("DIV");
this.listDiv02 = document.createElement("DIV");
if (!ScrollPic.childs) { ScrollPic.childs = [] }
this.ID = ScrollPic.childs.length;
ScrollPic.childs.push(this);
this.initialize = function () {
if (!this.scrollContId) { throw new Error("必須指定 scrollContId."); return };
this.scrollContDiv = sina.$(this.scrollContId);
this.scrollContDiv.style.width = this.frameWidth + "px";
this.scrollContDiv.style.overflow = "hidden";
this.listDiv01.innerHTML = this.listDiv02.innerHTML = this.scrollContDiv.innerHTML;
this.scrollContDiv.innerHTML = "";
this.scrollContDiv.appendChild(this.stripDiv);
this.stripDiv.appendChild(this.listDiv01);
this.stripDiv.appendChild(this.listDiv02);
this.stripDiv.style.overflow = "hidden";
this.stripDiv.style.zoom = "1";
this.stripDiv.style.width = "32766px";
this.listDiv01.style.cssFloat = "left";
this.listDiv02.style.cssFloat = "left";
sina.addEvent(this.scrollContDiv, "mouseover", Function("ScrollPic.childs[" + this.ID + "].stop()"));
sina.addEvent(this.scrollContDiv, "mouseout", Function("ScrollPic.childs[" + this.ID + "].play()"));
if (this.arrLeftId) {
this.arrLeftObj = sina.$(this.arrLeftId);
if (this.arrLeftObj) {
sina.addEvent(this.arrLeftObj, "mousedown", Function("ScrollPic.childs[" + this.ID + "].rightMouseDown()"));
sina.addEvent(this.arrLeftObj, "mouseup", Function("ScrollPic.childs[" + this.ID + "].rightEnd()"));
sina.addEvent(this.arrLeftObj, "mouseout", Function("ScrollPic.childs[" + this.ID + "].rightEnd()"))
}
};
if (this.arrRightId) {
this.arrRightObj = sina.$(this.arrRightId);
if (this.arrRightObj) {
sina.addEvent(this.arrRightObj, "mousedown", Function("ScrollPic.childs[" + this.ID + "].leftMouseDown()"));
sina.addEvent(this.arrRightObj, "mouseup", Function("ScrollPic.childs[" + this.ID + "].leftEnd()"));
sina.addEvent(this.arrRightObj, "mouseout", Function("ScrollPic.childs[" + this.ID + "].leftEnd()"))
}
};
if (this.autoPlay) { this.play() }
};
this.leftMouseDown = function () {
if (_state != "ready") { return };
_state = "floating";
_scrollTimeObj = setInterval("ScrollPic.childs[" + this.ID + "].moveLeft()", this.speed)
};
this.rightMouseDown = function () {
if (_state != "ready") { return };
_state = "floating";
_scrollTimeObj = setInterval("ScrollPic.childs[" + this.ID + "].moveRight()", this.speed)
};
this.moveLeft = function () {
if (this.scrollContDiv.scrollLeft + this.space >= this.listDiv01.scrollWidth) {
this.scrollContDiv.scrollLeft = this.scrollContDiv.scrollLeft + this.space - this.listDiv01.scrollWidth
} else { this.scrollContDiv.scrollLeft += this.space }
};
this.moveRight = function () {
if (this.scrollContDiv.scrollLeft - this.space <= 0) {
this.scrollContDiv.scrollLeft = this.listDiv01.scrollWidth + this.scrollContDiv.scrollLeft - this.space
} else { this.scrollContDiv.scrollLeft -= this.space }
};
this.next = function () {
if (_state != "ready") { return };
_state = "stoping";
this.move(this.pageWidth, true)
};
this.play = function () {
if (!this.autoPlay) { return };
clearInterval(_autoTimeObj);
_autoTimeObj = setInterval("ScrollPic.childs[" + this.ID + "].next()", this.autoPlayTime * 1000)
};
this.stop = function () { clearInterval(_autoTimeObj) };
this.pageTo = function (num) {
if (_state != "ready") { return };
_state = "stoping";
var fill = num * this.frameWidth - this.scrollContDiv.scrollLeft;
this.move(fill, true)
}
};<div class="nav" id="nav"> <div id="nav-all"> <div class="nav-i"><div class="nav-name"><a>BI系統1</a></div></div> <div class="nav-i"><div class="nav-name"><a>BI系統2</a></div></div> <div class="nav-i"><div class="nav-name"><a>BI系統3</a></div></div> </div> <a class="last-next" id="last"><img src="img/last.png" /></a> <a class="last-next" id="next"><img src="img/next.png" /></a> </div> <script src="play.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> var scrollPic_02 = new ScrollPic(); scrollPic_02.scrollContId = "nav-all"; // 存放所有輪播元素的 div scrollPic_02.arrLeftId = "last"; // 左箭頭 ID scrollPic_02.arrRightId = "next"; // 右箭頭 ID scrollPic_02.frameWidth = 1102; // 顯示框寬 scrollPic_02.pageWidth = 226; // 翻頁寬度 scrollPic_02.speed = 10; // 移動速度(毫秒,越小越快) scrollPic_02.space = 20; // 每次移動像素(px,越大越快) scrollPic_02.autoPlay = true; // 自動播放 true|false scrollPic_02.autoPlayTime = 2; // 自動播放間隔時間(秒) scrollPic_02.initialize(); // 初始化 </script>
其中樣式可自行調整,最關鍵的是最下方的 script 設定:只要設定好 scrollContId、左右箭頭 ID、frameWidth、pageWidth 等參數並呼叫 initialize(),就能達到輪播效果。下次要寫輪播時,只要改變樣式與這些參數即可,無需改動核心邏輯。以上就是本文的完整內容,希望對大家有所幫助。