面向对象-简易选项卡(简易轮播图)

html代码

<body>
		<div id="div1">
			<input class="active" type="button" value="1" />
			<input type="button" value="2" />
			<input type="button" value="3" />
			<div style="display: block;">1111</div>
			<div>2222</div>
			<div>3333</div>
		</div>
		<div id="div2">
			<input class="active" type="button" value="1" />
			<input type="button" value="2" />
			<input type="button" value="3" />
			<div style="display: block;">1111</div>
			<div>2222</div>
			<div>3333</div>
		</div>
	</body>

css代码

<style>
			#div1 div ,#div2 div{
				width: 200px;
				height: 200px;
				border: 1px solid red;
				display: none;
			}
			.active{
				background: red;
			}
		</style>

js代码

要注意 点击事件 定时器 等 函数中this的指向

<script>
			var div1 = null;
			var aIpt = null;
			var aDiv = null;
			window.onload = function () {
			    var t1 = new Tab("div1");
                t1.init();
                var t2 = new Tab("div2");
                t2.aotoPlay(2000);
			};
			function Tab(id) {
                this.div1 = document.getElementById(id);
                this.aIpt = this.div1.getElementsByTagName("input");
                this.aDiv = this.div1.getElementsByTagName("div");
            	this.iNow = 0; //自动轮播用
			}
			Tab.prototype.init = function() {
			    //this ==> t1
				var This = this;
                for(var i = 0;i < this.aIpt.length;i++){
                    this.aIpt[i].index = i;
                    this.aIpt[i].onclick = function () {
						This.change(this);
                    };
                }
            };
            Tab.prototype.change = function(obj){
                for(var j = 0;j < this.aDiv.length;j++){
                    this.aDiv[j].style.display = "none";
                    this.aDiv[obj.index].style.display = "block";
                    this.aIpt[j].className = "";
                    obj.className = "active"
                }
            };
            //自动轮播
            Tab.prototype.aotoPlay = function (time) {
                var This = this;
				setInterval(function () {
                    This.iNow++;
                    if(This.iNow >=This.aDiv.length){
                        This.iNow = 0;
                    }
                    for(var j = 0;j < This.aDiv.length;j++){
                        This.aDiv[j].style.display = "none";
                        This.aDiv[This.iNow].style.display = "block";
                        This.aIpt[j].className = "";
                        This.aIpt[This.iNow].className = "active"
                    }
                },time)
            }
		</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值