Home  >  Article  >  Web Front-end  >  js namespace writing example code

js namespace writing example code

怪我咯
怪我咯Original
2017-07-07 15:09:111309browse

This article mainly introduces the writing method of js namespace, and analyzes the writing method of JavaScript namespace in the form of a complete example. It has certain reference value. Friends in need can refer to it.

This article analyzes the writing method of js namespace with examples. I share it with you for your reference. The details are as follows:

I have known this writing method for a long time. I have been avoiding it because the foundation Object-oriented is not solid enough, but this method is still necessary for the entire site. Will

html part:

111

现实

  • 1
  • 2
  • 3

内容1

内容2

内容3

css style:

#p1{width: 100px;height: 100px;background: #ccc;}
#p2{width:100px;height: 20px;background: red;}
#p3{width: 300px;height: 200px;border: 1px solid #ccc;position: absolute;;margin-left: -150px;margin-top:-100px;left:50%;top: 50%;display: none;}
li{width: 100px;float: left;background: #ccc;}
.active{background: red;}
.tab_main{display: none;}
.clearfix:after{clear: both;display: table;content:'';}
.cleafix{zoom:1;}

js code:

var namespace={
 int:function(){
  this.hide.hideFun();
  this.show.showFun();
  this.tab.tabFun();
 }
};
namespace.hide={
 hideBtn:$('#p1'),
 hideFun:function() {
  var that=this;
  var a=this.hideBtn;
  a.click(function() {
   $(this).hide();
  });
 }
};
namespace.show={
 showBtn:$('#p2'),
 showBox:$('#p3'),
 showFun:function(){
  var that=this;
  var a=this.showBtn;
  var b=this.showBox;
  a.click(function(event) {
   b.show();
  });
 }
}
namespace.tab={
 tabBtn:$('.tab_nav li'),
 tabCon:$('.tab_main p'),
 tabFun:function(){
  var that=this;
  var a=this.tabBtn;
  var b=this.tabCon;
  a.click(function() {
   $(this).addClass('active').siblings().removeClass('active');
   b.eq($(this).index()).show().siblings().hide();
  });
 }
}
namespace.int();

The above is the detailed content of js namespace writing example code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn