標籤輪換,標籤切換 JS實現
JavaScript語言: CRM<script type="text/javascript">
//boxId 容器ID boxHeight 容器高度,標籤內容高度
//tabsId 標籤按鈕容器ID 無為false
//tabsOn 當前標籤項
//times 自動輪換時間
//e 事件 true:onmouseover false:onclick
var ztShow = function (boxId, boxHeight, tabsId, tabsOn, times, e) {
var getNodeList = function (nodeList) {//擷取標籤下結點數
var New = []; var n = 0;
for (var i = 0; i < nodeList.length; ++i) { if (nodeList[i].nodeType == 1) { n++; New.push(nodeList[i]); nodeList[i].setAttribute("num", n); } }
return New;
}
var $ = function (id) { return document.getElementById(id); } //定義ID查詢器;
if (tabsId) { var as = getNodeList($(tabsId).childNodes); as[0].className = tabsOn; }
if (boxId) { var imgs = getNodeList($(boxId).childNodes); } //取得輪換圖片數組;
var tabsKey = true; //定義定時輪換KEY開關;
var tabsNum = 0; //定義當前輪換數組位置座標數;
if (tabsId) {
for (var i = 0; i < as.length; i++) {
as[i][e ? "onmouseover" : "onclick"] = function (e) {
tabsKey = false;
if (boxId) { imgs[0].style.marginTop = "-" + ((this.attributes["num"].nodeValue - 1) * boxHeight) + "px"; }
for (var ii = 0; ii < as.length; ii++) { as[ii].className = ""; }; this.className = tabsOn;
tabsNum = this.attributes["num"].nodeValue - 1;
}
}
}
if (boxId) {
for (var i = 0; i < imgs.length; i++) {
imgs[i].onmouseover = function () { tabsKey = false; }
imgs[i].onmouseout = as[i].onmouseout = function () { tabsKey = true; }
}
}
if (times != 0) {
this.setInterval(function () {
if (tabsKey) {
if (tabsId) { for (var i = 0; i < as.length; i++) { as[i].className = ""; }; as[tabsNum].className = tabsOn };
if (boxId) { imgs[0].style.marginTop = "-" + ((tabsNum) * boxHeight) + "px"; }
tabsNum++;
if (tabsNum == imgs.length) { tabsNum = 0; }
}
}, times);
}
}
window.onload = function () {
ztShow("imgBox", 308, "tabBox", "tabsOn", 2000, true);
ztShow(false, 0, "onOn", "on", 0, true);
ztShow(false, 0, "theGood", "on", 0, true);
}
</script>