zoukankan      html  css  js  c++  java
  • 面向对象版Tab栏切换

    <div class="wrapper" id="wrapper">
        <ul class="tab" id="tab-menu">
            <li class="tab-item active">国际大牌<span></span></li>
            <li class="tab-item">国妆名牌<span></span></li>
            <li class="tab-item">清洁用品<span></span></li>
            <li class="tab-item">男士精品</li>
        </ul>
        <div class="products" id="tab-main">
            <div class="main selected">
                <a href="###"><img src="imgs/guojidapai.jpg" alt=""/></a>
            </div>
            <div class="main">
                <a href="###"><img src="imgs/guozhuangmingpin.jpg" alt=""/></a>
            </div>
            <div class="main">
                <a href="###"><img src="imgs/qingjieyongpin.jpg" alt=""/></a>
            </div>
            <div class="main">
                <a href="###"><img src="imgs/nanshijingpin.jpg" alt=""/></a>
            </div>
        </div>
    </div>
    var tb = new Tab({
            tabMenu: "tab-menu",    // 指定tab栏菜单id
            tabMain: "tab-main",    // 指定tab栏内容id
            auto: true           // 是否自动播放
        });

    面向对象封装

    function Tab(obj) {
        this.tabMenus = null;
        this.tabMains = null;
        if(obj) {
            this._init(obj);
        }
    }
    Tab.prototype = {
        constructor: Tab,
        timer : null,
        _init:function(obj){
            this.initEle(obj);
            this.click();
            if(obj.auto){
                this.autoPlay();
            }
    
        },
        initEle:function(obj){
            var tabMenu = document.getElementById(obj.tabMenu);
            var tabMain = document.getElementById(obj.tabMain);
            this.tabMenus = tabMenu.children;
            this.tabMains = tabMain.children;
        },
        click: function() {
            var that = this;
            for(var i=0,len=this.tabMenus.length;i<len;i++) {
                this.tabMenus[i].onclick = function(){
                    that.show(this);
                    that.autoPlay(this.index);
                }
            }
        },
        show: function(currentLi) {
            for(var i=0,len=this.tabMenus.length;i<len;i++) {
                this.tabMenus[i].index = i;
                this.tabMenus[i].className = "tab-item";
                this.tabMains[i].style.display = "none";
            }
            currentLi.className += " active";
            this.tabMains[currentLi.index].style.display = "block";
        },
        autoPlay: function(currentIndex){
            var that = this;
            var index = 0;
            if(currentIndex !== undefined && currentIndex !== ""){
                index = currentIndex;
            }
            clearInterval(this.timer);
            this.timer = setInterval(function(){
                index++;
                if(index == that.tabMenus.length){
                    index = 0;
                }
                that.show(that.tabMenus[index]);
            },2000);
        }
    }

    注意事项:

    这里要注意timer的位置,如果将timer放在构造函数里如this.timer=null,则_init()方法要用新创建出的对象来调用timer才生效(tb._init())
    如果在构造函数里使用 this._init(obj);来初始化,则timer要放到原型对象里(timer:null)才生效
    这里的生效是指清除定时器时clearInterval(this.timer); this.timer为同一个对象 正常情况下this.timer值为null,未生效的情况下值为undefined
    由于一些经验不足上面的一段解释是错误的,造成timer值为Undefined的原因为this.timer=null应该在调用方法_init()之前定义


  • 相关阅读:
    struts2 ajax传值
    s:iterator遍历
    JavaScript闭包
    组合继承与寄生组合式继承
    JSP EL表达式详细介绍
    js判断字符串是否包含中文或英文
    jQuery 中 jQuery(function(){})与(function(){})(jQuery) 的区别
    HTML5--新增结构元素(2)
    HTML5--新增全局属性(1)
    nodejs的安装配置(nvm-windows)
  • 原文地址:https://www.cnblogs.com/zmshare/p/6644990.html
Copyright © 2011-2022 走看看