js 面向对象选项卡

 

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
*{ margin:0; padding:0;}
#box{ width:500px; height:400px; margin:20px auto;  position:relative;}
#div1,#div2{  position:absolute; top:0; }
#div1 div,#div2 div{ width:200px; height:300px; border:1px #999 solid; display:none; text-align:center; color:#fff;}
#div1 input,#div2 input{ width:62px; height:30px; border:0; color:#fff; background:#333; margin-bottom:2px; }
.active{ background:red !important;}
.bk0{ background:url(../../images/TaylorSwift.jpg) no-repeat center center/contain;}
.bk2{ background:url(../../images/TaylorSwift3.jpg) no-repeat center center/contain;}
.bk3{ background:url(../../images/TaylorSwift4.jpg) no-repeat center center/contain;}
</style>
</head>

<body>
<script>

	  window.onload=function(){
		  var t1=new  Tab(‘div1‘);
		 // t1.init();  

		  var t2=new  Tab(‘div2‘);
		 // t2.init();
		  t2.autoplay();
		  } 

	  function Tab(id){
		  this.oParent = document.getElementById(id);
		  this.aInput=this.oParent.getElementsByTagName("input");
		  this.aDiv= this.oParent.getElementsByTagName("div");
		  this.iNow = 0;
		  this.init();  // init 可以直接写在这里 new时候自动初始化
		  }

	  Tab.prototype.init=function(){
		var that=this;
		 for(var i=0;i<this.aInput.length;i++){
			 this.aInput[i].index=i;
			 this.aInput[i].onclick=function(){
				 that.change(this);
				 };
			 }

	   }// init end

	    Tab.prototype.change=function(obj){
			for(var i=0;i<this.aInput.length;i++){
			  this.aInput[i].className=‘‘;
			  this.aDiv[i].style.display = ‘none‘;
			 }

			 obj.className=‘active‘;
			 this.aDiv[obj.index].style.display = ‘block‘;
		}//change

	     Tab.prototype.autoplay=function(){
			  var This = this;
               setInterval(function(){
              if(This.iNow == This.aInput.length-1){
               This.iNow = 0;
               }
              else{
                This.iNow++;
                }
			  for(var i=0;i<This.aInput.length;i++){
				  This.aInput[i].className = ‘‘;
				  This.aDiv[i].style.display = ‘none‘;
			  }
             This.aInput[This.iNow].className = ‘active‘;
             This.aDiv[This.iNow].style.display = ‘block‘;
			 },3000);
		 }
</script>

<div id="box">
    <div id="div1">
        <input class="active" type="button" value="1">
        <input type="button" value="2">
        <input type="button" value="3">
        <div style="display:block"  class="bk0">11111</div>
        <div class="bk3">22222</div>
        <div class="bk2">33333</div>
    </div>

    <div id="div2" style="right:0 !important">
        <input class="active" type="button" value="1">
        <input type="button" value="2">
        <input type="button" value="3">
        <div style="display:block" class="bk3">第二个的1111</div>
        <div class="bk0">第二 222</div>
        <div class="bk2">第二个33333</div>
    </div>

</div>

</body>
</html>
时间: 2024-08-29 16:33:00

js 面向对象选项卡的相关文章

手写js面向对象选项卡插件

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>无缝滚动</title>    <style type="text/css">        body,ul{margin: 0;padding: 0;}        li{list-style: none;}  

原生js面向对象编程-选项卡(点击)

<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>原生js面向对象选项卡(点击)</title> <style> #div1 div{ width:400px; height:300px; border:1px solid #ccc; overflow: hidden; display: non

js面向对象+一般方法的选项卡

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

原生js面向对象编程-选项卡(自动轮播)

<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>原生js面向对象编程-选项卡(自动轮播)</title> <style> #div1 div{ width:400px; height:300px; border:1px solid #ccc; overflow: hidden; display

js实现选项卡

通过JavaScript实现如上选项卡切换的效果. 实现思路: 一.HTML页面布局 选项卡标题使用ul..li 选项卡内容使用div 二.CSS样式制作 整个选项卡的样式设置 选项卡标题的样式设置 选项卡内容的样式设置 一开始只显示一个选项卡内容,其它选项卡内容隐藏. 三.JS实现选项卡切换 获取选项卡标题和选项卡内容 选项卡内容多个,需要循环遍历来操作,得知哪个选项卡和哪个选项内容匹配 通过改变DOM的css类名称,当前点击的选项卡显示,其它隐藏 <!--代码一--><!DOCTYP

js面向对象的系列

在面向对象语言中如java,C#如何定义一个对象,通常是定义一个类,然后在类中定义属性,然后通过new 关键字来实例化这个类,我们知道面向对象有三个特点,继承.多态和封装.那么问题来了,在javaScript中如何定义一个类?在javaScript中如何定义类的属性?如何继承?带着这些问题开始我们的js面向对象之旅吧. 在js中如何定义类? js中是没有类的概念的,但是我们通常会用一个函数定义成一个类.funtion class1(){ //类的成员定义 } 这里class1既是一个函数也是一个

简单粗暴地理解js原型链--js面向对象编程

简单粗暴地理解js原型链--js面向对象编程 原型链理解起来有点绕了,网上资料也是很多,每次晚上睡不着的时候总喜欢在网上找点原型链和闭包的文章看,效果极好. 不要纠结于那一堆术语了,那除了让你脑筋拧成麻花,真的不能帮你什么.简单粗暴点看原型链吧,想点与代码无关的事,比如人.妖以及人妖. 1)人是人他妈生的,妖是妖他妈生的.人和妖都是对象实例,而人他妈和妖他妈就是原型.原型也是对象,叫原型对象. 2)人他妈和人他爸啪啪啪能生出一堆人宝宝.妖他妈和妖他爸啪啪啪能生出一堆妖宝宝,啪啪啪就是构造函数,俗

js面向对象程序设置——创建对象

<script type="text/javascript">            //工厂方式        //1.原始方式        /* var objCar=new Object();        objCar.name="劳斯莱斯";        objCar.color="blue";        objCar.showColor = function() {          alert(this.colo

js面向对象编程:如何实现方法重载

js中如何实现方法重载?这涉及到三个问题 1同名函数的调用问题 2函数中特殊的参数arguments 3如何利用arguments实现方法重载 1同名函数的调用问题 都知道在js中如果存在多个名称相同的函数,则调用实际每次都只使用最后一个,js其实是没有重载的,也就是说,如果定义了多个同名的函数,单参数不一样,在调用时,js不管参数个数,只管前后顺序 例如: function test1(arg1) { alert("参数1:"+arg1); } function test1(arg1