1. <!--
    2. * @Description: Tab使用继承实现 模块的显示和隐藏 以及类型变换
    3. * @Author: Harry
    4. * @Date: 2021-10-15 09:53:14
    5. * @Url: https://u.mr90.top
    6. * @github: https://github.com/rr210
    7. * @LastEditTime: 2021-10-15 11:03:20
    8. * @LastEditors: Harry
    9. -->
    10. <!DOCTYPE html>
    11. <html lang="en">
    12. <head>
    13. <meta charset="UTF-8">
    14. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    15. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    16. <title>继承实现状态的改变</title>
    17. </head>
    18. <style>
    19. * {
    20. padding: 0;
    21. margin: 0;
    22. box-sizing: border-box;
    23. }
    24. a,span {
    25. text-decoration: none;
    26. color: #fff;
    27. width: 100px;
    28. height: 50px;
    29. line-height: 50px;
    30. text-align: center;
    31. font-size: 18px;
    32. font-weight: 500;
    33. }
    34. body {
    35. background-color: #ccc;
    36. }
    37. main {
    38. display: inline-block;
    39. width: 40%;
    40. margin: 20px;
    41. }
    42. nav {
    43. display: flex;
    44. justify-content: center;
    45. align-items: center;
    46. }
    47. main nav a:nth-child(1) {
    48. background-color: orange;
    49. }
    50. main nav a:nth-child(2) {
    51. background-color: #888;
    52. }
    53. section {
    54. width: 100%;
    55. height: 200px;
    56. font-size: 40px;
    57. font-weight: 500;
    58. color: #000;
    59. border-radius: 20px;
    60. padding: 20px;
    61. box-shadow: 0 0 10px #999;
    62. }
    63. section {
    64. margin: 20px 0;
    65. background-color: rgb(228, 238, 83);
    66. }
    67. section+section {
    68. display: none;
    69. background-color: rgb(20, 172, 28);
    70. }
    71. </style>
    72. <body>
    73. <main class="tab1">
    74. <nav>
    75. <span href="javascript:;">点击按钮1</span>
    76. <span href="javascript:;">点击按钮2</span>
    77. </nav>
    78. <section>1</section>
    79. <section>2</section>
    80. </main>
    81. <main class="tab2">
    82. <nav>
    83. <a href="javascript:;">点击按钮1</a>
    84. <a href="javascript:;">点击按钮2</a>
    85. </nav>
    86. <section>1</section>
    87. <section>2</section>
    88. </main>
    89. </body>
    90. <script>
    91. // 使用继承实现类型的转化
    92. // 先写一个继承的方法
    93. function _extend(sub, sup) {
    94. sub.prototype = Object.create(sup.prototype)
    95. Object.defineProperty(sub, 'constructor', {
    96. value: sub,
    97. enumerable: false
    98. })
    99. }
    100. // 定义一个变换的函数
    101. function Animation() { }
    102. // 实现元素的显示和隐藏
    103. Animation.prototype.show = function () {
    104. this.style.display = 'block'
    105. }
    106. Animation.prototype.hide = function () {
    107. this.style.display = 'none'
    108. }
    109. // 实现元素背景的替换
    110. Animation.prototype.backgroundColor = function (color) {
    111. this.style.backgroundColor = color
    112. }
    113. _extend(Tab, Animation)
    114. function Tab(params) {
    115. let args = Object.assign({ el: null, section: 'section', link: 'a', callback: null }, params)
    116. this.tab = document.querySelector(args['el'])
    117. this.links = this.tab.querySelectorAll(args['link'])
    118. this.sections = this.tab.querySelectorAll(args['section'])
    119. this.callback = args['callback']
    120. }
    121. // 当实例创建之后需要开始启动 并且获取
    122. Tab.prototype.run = function (i) {
    123. this.reset()
    124. this.action(i)
    125. this.bindEvent()
    126. }
    127. // 绑定一个事件
    128. Tab.prototype.bindEvent = function () {
    129. this.links.forEach((el, i) => {
    130. el.addEventListener('click', () => {
    131. // 调用action函数之前先进行重置
    132. this.reset()
    133. this.action(i)
    134. if (this.callback) this.callback()
    135. })
    136. });
    137. }
    138. Tab.prototype.reset = function () {
    139. this.links.forEach((el, i) => {
    140. this.backgroundColor.call(el, '#888')
    141. this.hide.call(this.sections[i])
    142. })
    143. }
    144. // 绑定一个事件执行函数
    145. Tab.prototype.action = function (i) {
    146. this.backgroundColor.call(this.links[i], '#ffa500')
    147. this.show.call(this.sections[i])
    148. }
    149. new Tab({ el: ".tab1", link: 'span', callback: () => { console.log('测试回调') } }).run(0)
    150. new Tab({ el: ".tab2", callback: () => { console.log('测试回调') }, link: "a" }).run(1)
    151. </script>
    152. </html>