JQ面向对象的封装步骤(实用写法)

电玩女神 2022-05-22 10:34 270阅读 0赞

一:函数自己调用自己

  1. /*CSS页面*/
  2. #box1{ width: 200px; height: 200px; background: red; margin :100px auto; }
  3. .box2{ width: 200px; height: 200px; background: blue; margin: 200px auto; }
  4. <!-- HTML页面 -->
  5. <div id="box1"></div>
  6. <div class="box2"></div>
  7. // JS 页面 (面向对象封装)
  8. // 1.创建构造函数
  9. var Silder = function(ele){
  10. this.$ele = $(ele)
  11. }
  12. //2.写面向对象的方法 ===> 函数
  13. Silder.prototype = {
  14. initialize:function(){
  15. this.$ele.click(function(){
  16. console.log(1)
  17. })
  18. }
  19. }
  20. // 调用自己函数
  21. var silder = new Silder(this)
  22. silder.initialize();
  23. // 打开页面后直接console 一个

二:使用JQ元素进行调用函数

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>元素的调用</title>
  6. <script type="text/javascript" src="jquery.min.js"></script>
  7. <style type="text/css"> #box1{ width: 200px; height: 200px; background: red; margin :100px auto; } .box2{ width: 200px; height: 200px; background: blue; margin: 200px auto; } </style>
  8. </head>
  9. <body>
  10. <div id="box1"></div>
  11. <div class="box2"></div>
  12. </body>
  13. </html>
  14. <script type="text/javascript"> // 1.创建构造函数 // ele 是元素 opt 是 文档(jq所有内容) var Silder = function (ele,opt) { //元素 属性 this.$ele = $(ele); } //2.方法 ===> 函数 Silder.prototype = { // 初始化函数 页面加载之前就要添加所有的事件 initialize:function() { this.$ele.click(function () { console.log(1) }) } } //3. 类方法返回对象 $.fn.playsee = function (option) { var silder = new Silder(this) return silder.initialize(); } //4.调用 $('#box1').playsee() </script>
  15. <!-- 页面点击 #box 后 console 一个 1 -->

三:自定义变量的改变与初始化

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>自定义变量的调用</title>
  6. <script type="text/javascript" src="jquery.min.js"></script>
  7. <style type="text/css"> #box1{ width: 200px; height: 200px; background: red; margin :100px auto; } .box2{ width: 200px; height: 200px; background: blue; margin: 200px auto; } </style>
  8. </head>
  9. <body>
  10. <div id="box1"></div>
  11. <div class="box2"></div>
  12. </body>
  13. </html>
  14. <script type="text/javascript"> // 1.创建构造函数 // ele 是元素 opt 是 文档(jq所有内容) var Silder = function (ele,opt,name,age) { //元素 属性 this.$ele = $(ele); //定义用户可更改的默认值 ===> 以后用的变量(不确定的值或者说是数据) this.defaults = { 'name': 'red', 'age':18 }; //定义并接受合成设置的值 this.settings = $.extend({}, this.defaults, opt); } //2.方法 ===> 函数 Silder.prototype = { // 初始化函数 页面加载之前就要添加所有的事件 initialize:function() { var _this = this; this.$ele.click(function () { console.log(1) console.log(_this.settings.name) console.log(_this.settings.age) }) } } //3. 类方法返回对象 $.fn.playsee = function (option) { var silder = new Silder(this,option) return silder.initialize(); } //4.调用 //第一个div $('#box1').playsee() //第二个div $('.box2').playsee({ 'name':'杨宝', 'age':18 }) </script>

四:封装一个轮播(插件)

  1. // 1.创建构造函数
  2. // ele 是元素 opt 是 文档(jq所有内容)
  3. var Silder = function (ele,opt) {
  4. //1-1 元素 属性
  5. this.$ele = $(ele);
  6. //1-2 常量 不变的值
  7. this.config = {
  8. // 图片列表
  9. 'lis':this.$ele.find('ul li'),
  10. //左右按钮
  11. 'prev':this.$ele.find('.left'),
  12. 'next':this.$ele.find('.right'),
  13. // 下标
  14. 'icoNav':this.$ele.find('ol li span'),
  15. 'index':0,
  16. 'timer':null
  17. }
  18. //1-3 定义用户可更改的默认值 ===> 以后用的变量(不确定的值或者说是数据)
  19. this.defaults = {
  20. // 列表数据添加
  21. 'cArr': [],
  22. // 选中下标样式
  23. 'iconClass':'',
  24. // 列表长度
  25. 'lisNum':7,
  26. // 速度
  27. 'speed':1000,
  28. 'play':true,
  29. 'onbox':true,
  30. 'onimg':false
  31. };
  32. //1-4 定义并接受合成设置的值
  33. this.settings = $.extend({}, this.defaults, opt);
  34. }
  35. //2.方法 ===> 函数
  36. Silder.prototype = {
  37. // 初始化函数 页面加载之前就要添加所有的事件
  38. initialize:function() {
  39. var _this = this;
  40. // 判断是否 添加定时器
  41. if (this.settings.play) {
  42. this.play();
  43. };
  44. //判断是否移到box上停止定时器
  45. if (this.settings.onbox) {
  46. this.$ele.mouseover(function(){
  47. _this.clear();
  48. });
  49. this.$ele.mouseout(function(){
  50. _this.play();
  51. });
  52. }
  53. //判断是否给图片添加点击事件
  54. if (this.settings.onimg) {
  55. this.$ele.on("click","."+this.settings.cArr[3],function(){
  56. _this.left();
  57. });
  58. this.$ele.on("click","."+this.settings.cArr[5],function(){
  59. _this.right();
  60. });
  61. }
  62. // 左右按钮点击函数
  63. this.config.prev.click(function () {
  64. _this.left();
  65. })
  66. this.config.next.click(function () {
  67. _this.right();
  68. })
  69. // 下标点击函数
  70. this.config.icoNav.each(function (i,elem) {
  71. $(elem).click(function () {
  72. _this.byIndexInto(i)
  73. })
  74. })
  75. },
  76. left:function () {
  77. var _this = this;
  78. this.settings.cArr.unshift(this.settings.cArr[this.settings.lisNum-1]);
  79. this.settings.cArr.pop();
  80. this.config.lis.each(function (i,elem) {
  81. $(elem).removeClass().addClass(_this.settings.cArr[i]);
  82. })
  83. this.config.index -- ;
  84. if (this.config.index < 0 ) {
  85. this.config.index = this.settings.lisNum-1
  86. }
  87. this.show();
  88. },
  89. right:function () {
  90. var _this = this;
  91. this.settings.cArr.push(this.settings.cArr[0]);
  92. this.settings.cArr.shift();
  93. this.config.lis.each(function (i,elem) {
  94. $(elem).removeClass().addClass(_this.settings.cArr[i]);
  95. })
  96. this.config.index ++ ;
  97. if (this.config.index > this.settings.lisNum-1 ) {
  98. this.config.index = 0
  99. }
  100. this.show();
  101. },
  102. byIndexInto:function (index) {
  103. // 传递的index是 下标的 0 1 2 3 4 5 6
  104. var _this = this;
  105. var textindex = index;
  106. var num = textindex-this.config.index;
  107. if (num==0) {
  108. return;
  109. } else if (num>0) {
  110. var newarr = this.settings.cArr.splice(0,num);
  111. this.settings.cArr=$.merge(this.settings.cArr,newarr);
  112. this.config.lis.each(function (i,elem) {
  113. $(elem).removeClass().addClass(_this.settings.cArr[i]);
  114. })
  115. this.config.index=textindex;
  116. this.show();
  117. } else if (num<0) {
  118. this.settings.cArr.reverse();
  119. var oldarr=this.settings.cArr.splice(0,-num)
  120. this.settings.cArr=$.merge(this.settings.cArr,oldarr);
  121. this.settings.cArr.reverse();
  122. this.config.lis.each(function(i,elem){
  123. $(elem).removeClass().addClass(_this.settings.cArr[i]);
  124. })
  125. this.config.index=textindex;
  126. this.show();
  127. }
  128. },
  129. // 清除定时器
  130. clear:function () {
  131. clearInterval(this.config.timer)
  132. },
  133. play:function () {
  134. this.config.timer = setInterval(()=>{
  135. this.right()},this.settings.speed)
  136. },
  137. show:function () {
  138. this.config.icoNav.eq(this.config.index).addClass('blue').parent().siblings().children().removeClass('blue')
  139. }
  140. }
  141. //3. 类方法返回对象
  142. $.fn.LunBo = function (option) {
  143. var silder = new Silder(this,option)
  144. return silder.initialize();
  145. }
  146. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> *{ margin: 0;padding: 0; } #box{ width: 100%; height: 340px; margin-top: 100px; position: relative; background-color: #FFFFFF; } li{ list-style: none; transition: all 0.3s ease-out; opacity: 0; } #box>ul>li{ position: absolute; top: 0; left: 0; } #box>ul{ width: 1200px; height: 300px; overflow: hidden; position: absolute; left: 50%; margin-left: -600px; } #box>ol{ width: 1200px; height: 30px; position: absolute; bottom: 0; left: 50%; margin-left: -600px; text-align: center; padding-top: 8px; /*background: red;*/ } #box>a{ position: absolute; top: 50%; width: 60px; height: 100px; line-height: 100px; font-size: 30px; color: #fff; text-decoration: none; background: rgba(0,255,0,0.5); margin-top: -50px; text-align: center; } .right{ right: 0; } img{ width: 751px; height: 300px; border: none; float: left; } #box>ol>li{ display: inline-block; width: 35px; height: 5px; padding-top: 4px; /*background: red;*/ opacity: 1; } #box>ol>li>span{ display: block; width: 35px; height:10px; background: red; cursor: pointer; } .p1{ transform: :translate(1120px,0) scale(0.81); } .p2{ transform:translate(896px,0) scale(0.81); } .p3{ transform:translate(672px,0) scale(0.81); } .p4{ transform:translate(448px,0) scale(0.81); transform-origin:100% 50%; opacity: 0.8;z-index: 2;} .p5{ transform:translate(224px,0) scale(1); z-index: 3;opacity: 1; } .p6{ transform:translate(0px,0) scale(0.81); transform-origin:0 50%; opacity: 0.8;z-index: 2; } .p7{ transform:translate(-224px,0) scale(0.81); } #box>ol>li>span.blue{ background: blue; } </style> <script type="text/javascript" src="jquery.min.js"></script> <script type="text/javascript" src="FacingtheGiants.js"></script> </head> <body> <div id="box"> <ul> <li class="p1"><img src="img/11.png"></li> <li class="p2"><img src="img/22.png"></li> <li class="p3"><img src="img/33.png"></li> <li class="p4"><img src="img/44.jpg"></li> <li class="p5"><img src="img/55.jpg"></li> <li class="p6"><img src="img/66.jpg"></li> <li class="p7"><img src="img/77.jpg"></li> </ul> <a href="javascript:;"class="left" style="z-index: 99"><</a> <a href="javascript:;"class="right" style="z-index: 99">></a> <ol> <li><span class="blue"></span></li> <li><span></span></li> <li><span></span></li> <li><span></span></li> <li><span></span></li> <li><span></span></li> <li><span></span></li> </ol> </div> </body> </html> <script type="text/javascript"> $(function () { $('#box').LunBo({ //图片列表 'cArr':["p1","p2","p3","p4","p5","p6","p7"], // 是否自动播放(添加定时器) 'play':true, // 定时器的时间 'speed':5000, // 下标的颜色 'iconClass':'blue', // 是否移到整个div上停止定时器 'onbox':true, // 是否给图片添加点击事件 'onimg':false, // cArr的长度 'lisNum':7 }) }) </script>

转自:https://blog.csdn.net/Facing_the_Giants/article/details/75206260

发表评论

表情:
评论列表 (有 0 条评论,270人围观)

还没有评论,来说两句吧...

相关阅读

    相关 面向对象——封装

           在面向对象程序中,就是把对象的属性和操作结合为一个独立的整体,并尽可能隐藏对象的内部实现细节。在java中主要体现为类对属性和方法的封装和方法对方法具体的实现细节

    相关 面向对象封装

    封装 该露的露,该藏的藏 我们程序设计要追求“高内聚,低耦合”。 高内聚就是类的内部数据操作细节自己完成,不允许外部干涉; 低耦合:仅暴露少量的方法给外部使用。

    相关 面向对象——封装

    封装的通俗介绍 我们日常使用的电脑主机,把cpu,内存,主板等等都封装到机箱里面去。假如没有机箱的话就会出现主机,主板全部都散落在一处,然后开机没有开机按钮,那么需要我们

    相关 面向对象封装

    封装 隐藏细节 - 抽离共有 封装: 对外隐藏类中一些属性与方法的实现细节 优点:外界不能直接访问,让内部的属性与方法具有安全保障 clas

    相关 面向对象——封装

    面向对象——封装 面向对象 面向过程:当需要实现一个功能的时候,每一个具体的步骤都要亲力亲为,详细处理每一个细节。 面向对象:当需要实现一个功能的时候,

    相关 面向对象封装

    一、什么是封装 什么是封装,就是将复杂的丑陋的,隐私的细节隐藏到内部,对外提供简单的使用接口,对外隐藏内部实现细节,并提供访问的接口 二、为什么需要封装 两个目的:1.为