標籤輪換,切換之精簡方法

來源:互聯網
上載者:User

標籤輪換,標籤切換 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>

最終如下:
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.