0


前端学习--JS入门(5) 事件监听/事件类型/事件对象

一、事件监听

事件是系统运行时发生的动作或者发生的事情

1.1 事件监听

  1. 元素对象.addEventListener('事件类型',要执行的函数)

** 三要素:**

事件源--获取被触发的dom元素

事件类型--触发方式(比如click/mouseover)

事件调用函数--触发了做什么事

案例-随机点名

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>随机</title>
  8. <style>
  9. *{
  10. margin: 0;
  11. padding: 0;
  12. }
  13. .box{
  14. width: 500px;
  15. height: 300px;
  16. margin: 50px auto;
  17. background-color: rgba(255,192,203,0.4);
  18. border: 1px dotted #333;
  19. border-radius: 10px;
  20. }
  21. h2{
  22. margin-top: 20px;
  23. text-align: center;
  24. }
  25. .txt span{
  26. margin-top: 50px;
  27. display: inline-block;
  28. font-size: 18px;
  29. padding-left: 40px;
  30. }
  31. .btn{
  32. margin-top: 100px;
  33. margin-left: 250px;
  34. transform: translate(-50%,0);
  35. }
  36. .btn button{
  37. display: inline-block;
  38. width: 100px;
  39. height: 30px;
  40. margin-right: 20px;
  41. background-color: deepskyblue;
  42. font-weight: 600;
  43. text-align: center;
  44. line-height: 30px;
  45. font-size: 18px;
  46. color: #fff;
  47. border-radius: 3px;
  48. border: none;
  49. }
  50. .btn button:hover{
  51. background-color: dodgerblue;
  52. cursor: pointer;
  53. }
  54. </style>
  55. </head>
  56. <body>
  57. <div class="box">
  58. <h2>随机点名</h2>
  59. <div class="txt">
  60. <span>名字是:</span>
  61. <span class="name"></span>
  62. </div>
  63. <div class="btn">
  64. <button class="start">开始</button>
  65. <button class="end">停止</button>
  66. </div>
  67. </div>
  68. <script>
  69. const arr = ['马超','黄忠','赵云','关羽','张飞','曹操','项羽','程咬金']
  70. const start = document.querySelector('.start')
  71. const end = document.querySelector('.end')
  72. let timeId = 0
  73. // start.addEventListener('click',function(){
  74. // const random = parseInt(Math.random()*arr.length)
  75. // const name = document.querySelector('.name')
  76. // name.innerHTML = arr[random]
  77. // })
  78. start.addEventListener('click',function(){
  79. timeId = setInterval(function(){
  80. const random = parseInt(Math.random()*arr.length)
  81. const name = document.querySelector('.name')
  82. name.innerHTML = arr[random]
  83. },200)
  84. })
  85. end.addEventListener('click',function(){
  86. clearInterval(timeId)
  87. })
  88. </script>
  89. </body>
  90. </html>

1.2 事件监听版本

  1. //DOM L0
  2. 事件源.on事件 = function(){}
  3. //DOM L2
  4. 事件源.addEventListener(事件, 事件处理函数)
  5. //on的方式多次写入被覆盖,add可以绑定多次拥有更多特性

二、事件类型

2.1 鼠标事件

click 鼠标点击

mouseenter 鼠标经过

mouseleave 鼠标离开

终极版轮播图

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>轮播图点击切换</title>
  8. <style>
  9. * {
  10. box-sizing: border-box;
  11. }
  12. .slider {
  13. width: 560px;
  14. height: 400px;
  15. overflow: hidden;
  16. }
  17. .slider-wrapper {
  18. width: 100%;
  19. height: 320px;
  20. }
  21. .slider-wrapper img {
  22. width: 100%;
  23. height: 100%;
  24. display: block;
  25. /* transition: all 1s; */
  26. }
  27. .slider-footer {
  28. height: 80px;
  29. background-color: rgb(100, 67, 68);
  30. padding: 12px 12px 0 12px;
  31. position: relative;
  32. }
  33. .slider-footer .toggle {
  34. position: absolute;
  35. right: 0;
  36. top: 12px;
  37. display: flex;
  38. }
  39. .slider-footer .toggle button {
  40. margin-right: 12px;
  41. width: 28px;
  42. height: 28px;
  43. appearance: none;
  44. border: none;
  45. background: rgba(255, 255, 255, 0.1);
  46. color: #fff;
  47. border-radius: 4px;
  48. cursor: pointer;
  49. }
  50. .slider-footer .toggle button:hover {
  51. background: rgba(255, 255, 255, 0.2);
  52. }
  53. .slider-footer p {
  54. margin: 0;
  55. color: #fff;
  56. font-size: 18px;
  57. margin-bottom: 10px;
  58. }
  59. .slider-indicator {
  60. margin: 0;
  61. padding: 0;
  62. list-style: none;
  63. display: flex;
  64. align-items: center;
  65. }
  66. .slider-indicator li {
  67. width: 8px;
  68. height: 8px;
  69. margin: 4px;
  70. border-radius: 50%;
  71. background: #fff;
  72. opacity: 0.4;
  73. cursor: pointer;
  74. }
  75. .slider-indicator li.active {
  76. width: 12px;
  77. height: 12px;
  78. opacity: 1;
  79. }
  80. </style>
  81. </head>
  82. <body>
  83. <div class="slider">
  84. <div class="slider-wrapper">
  85. <img src="./image/slider01.jpg" alt="" />
  86. </div>
  87. <div class="slider-footer">
  88. <p>对人类来说会不会太超前了?</p>
  89. <ul class="slider-indicator">
  90. <li class="active"></li>
  91. <li></li>
  92. <li></li>
  93. <li></li>
  94. <li></li>
  95. <li></li>
  96. <li></li>
  97. <li></li>
  98. </ul>
  99. <div class="toggle">
  100. <button class="prev">&lt;</button>
  101. <button class="next">&gt;</button>
  102. </div>
  103. </div>
  104. </div>
  105. <script>
  106. // 1. 初始数据
  107. const sliderData = [
  108. { url: './image/slider01.jpg', title: '对人类来说会不会太超前了?', color: 'rgb(100, 67, 68)' },
  109. { url: './image/slider02.jpg', title: '开启剑与雪的黑暗传说!', color: 'rgb(43, 35, 26)' },
  110. { url: './image/slider03.jpg', title: '真正的jo厨出现了!', color: 'rgb(36, 31, 33)' },
  111. { url: './image/slider04.jpg', title: '李玉刚:让世界通过B站看到东方大国文化', color: 'rgb(139, 98, 66)' },
  112. { url: './image/slider05.jpg', title: '快来分享你的寒假日常吧~', color: 'rgb(67, 90, 92)' },
  113. { url: './image/slider06.jpg', title: '哔哩哔哩小年YEAH', color: 'rgb(166, 131, 143)' },
  114. { url: './image/slider07.jpg', title: '一站式解决你的电脑配置问题!!!', color: 'rgb(53, 29, 25)' },
  115. { url: './image/slider08.jpg', title: '谁不想和小猫咪贴贴呢!', color: 'rgb(99, 72, 114)' },
  116. ]
  117. const img = document.querySelector('.slider-wrapper img')
  118. const p = document.querySelector('.slider-footer p')
  119. const footer = document.querySelector('.slider-footer')
  120. let i = 0 //控制播放的图片顺序
  121. //1 右侧按钮业务
  122. const next = document.querySelector('.next')
  123. next.addEventListener('click',function(){
  124. i++
  125. if(i >= 8)i = 0
  126. toggle()
  127. })
  128. //2 左侧按钮业务
  129. const prev = document.querySelector('.prev')
  130. prev.addEventListener('click',function(){
  131. i--
  132. if(i < 0)i = i + sliderData.length
  133. toggle()
  134. })
  135. function toggle(){
  136. img.src = sliderData[i].url
  137. p.innerHTML = sliderData[i].title
  138. footer.style.backgoundColor = sliderData[i].color
  139. document.querySelector('.slider-indicator .active').classList.remove('active')
  140. document.querySelector(`.slider-indicator li:nth-child(${i+1})`).classList.add('active')
  141. }
  142. //3 自动播放
  143. let timeId = setInterval(function(){
  144. //利用js自动调用点击事件
  145. next.click()
  146. },2000)
  147. //4 鼠标经过大盒子 停止定时器
  148. const slider = document.querySelector('.slider')
  149. slider.addEventListener('mouseenter',function(){
  150. clearInterval(timeId)
  151. })
  152. //5 鼠标离开 继续播放
  153. slider.addEventListener('mouseleave',function(){
  154. clearInterval(timeId)
  155. timeId = setInterval(function(){
  156. //利用js自动调用点击事件
  157. next.click()
  158. },2000)
  159. })
  160. </script>
  161. </body>
  162. </html>

2.2 焦点事件-表单获得光标

focus 获得焦点

blur 失去焦点

小米搜索框

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. ul {
  15. list-style: none;
  16. }
  17. .mi {
  18. position: relative;
  19. width: 223px;
  20. margin: 100px auto;
  21. }
  22. .mi input {
  23. width: 223px;
  24. height: 48px;
  25. padding: 0 10px;
  26. font-size: 14px;
  27. line-height: 48px;
  28. border: 1px solid #e0e0e0;
  29. outline: none;
  30. }
  31. .mi .search {
  32. border: 1px solid #ff6700;
  33. }
  34. .result-list {
  35. display: none;
  36. position: absolute;
  37. left: 0;
  38. top: 48px;
  39. width: 223px;
  40. border: 1px solid #ff6700;
  41. border-top: 0;
  42. background: #fff;
  43. }
  44. .result-list a {
  45. display: block;
  46. padding: 6px 15px;
  47. font-size: 12px;
  48. color: #424242;
  49. text-decoration: none;
  50. }
  51. .result-list a:hover {
  52. background-color: #eee;
  53. }
  54. </style>
  55. </head>
  56. <body>
  57. <div class="mi">
  58. <input type="search" placeholder="小米笔记本">
  59. <ul class="result-list">
  60. <li><a href="#">全部商品</a></li>
  61. <li><a href="#">小米11</a></li>
  62. <li><a href="#">小米10S</a></li>
  63. <li><a href="#">小米笔记本</a></li>
  64. <li><a href="#">小米手机</a></li>
  65. <li><a href="#">黑鲨4</a></li>
  66. <li><a href="#">空调</a></li>
  67. </ul>
  68. </div>
  69. <script>
  70. const input = document.querySelector('[type=search]')
  71. const ul = document.querySelector('.result-list')
  72. input.addEventListener('focus',function(){
  73. ul.style.display = 'block'
  74. input.classList.add('search')
  75. })
  76. input.addEventListener('blur',function(){
  77. ul.style.display = 'none'
  78. input.classList.remove('search')
  79. })
  80. </script>
  81. </body>
  82. </html>

2.3 键盘事件

keydown键盘按下触发

keyup 键盘抬起触发

2.4 文本事件

input 用户输入触发

  1. const tx = document.querySelector('#tx')
  2. const total = document.querySelector('.total')
  3. tx.addEventListener('focus',function(){
  4. total.style.opacity = 1
  5. })
  6. tx.addEventListener('blur',function(){
  7. total.style.opacity = 0
  8. })
  9. tx,addEventListener('input',function(){
  10. total.innerHTML = `${tx.value.length}/200字`
  11. })

三、事件对象

包含事件触发时相关信息的对象,比如用户按下哪个键、鼠标点击哪个元素

3.1 获取事件对象

  1. //事件绑定的回调函数的第一个参数就是事件对象
  2. //e就是事件对象
  3. 元素.addEventListener('click',function(e){
  4. })

3.2 事件对象常用属性

type获取当前的事件类型clientX/clientY获取光标相对于浏览器左上角的位置offsetX/offsetY获取光标相对于当前DOM元素左上角的位置key用户按下键盘键的值

案例-评论发布

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>评论回车发布</title>
  8. <style>
  9. .wrapper {
  10. min-width: 400px;
  11. max-width: 800px;
  12. display: flex;
  13. justify-content: flex-end;
  14. }
  15. .avatar {
  16. width: 48px;
  17. height: 48px;
  18. border-radius: 50%;
  19. overflow: hidden;
  20. background: url(./images/avatar.jpg) no-repeat center / cover;
  21. margin-right: 20px;
  22. }
  23. .wrapper textarea {
  24. outline: none;
  25. border-color: transparent;
  26. resize: none;
  27. background: #f5f5f5;
  28. border-radius: 4px;
  29. flex: 1;
  30. padding: 10px;
  31. transition: all 0.5s;
  32. height: 30px;
  33. }
  34. .wrapper textarea:focus {
  35. border-color: #e4e4e4;
  36. background: #fff;
  37. height: 50px;
  38. }
  39. .wrapper button {
  40. background: #00aeec;
  41. color: #fff;
  42. border: none;
  43. border-radius: 4px;
  44. margin-left: 10px;
  45. width: 70px;
  46. cursor: pointer;
  47. }
  48. .wrapper .total {
  49. margin-right: 80px;
  50. color: #999;
  51. margin-top: 5px;
  52. opacity: 0;
  53. transition: all 0.5s;
  54. }
  55. .list {
  56. min-width: 400px;
  57. max-width: 800px;
  58. display: flex;
  59. }
  60. .list .item {
  61. width: 100%;
  62. display: flex;
  63. }
  64. .list .item .info {
  65. flex: 1;
  66. border-bottom: 1px dashed #e4e4e4;
  67. padding-bottom: 10px;
  68. }
  69. .list .item p {
  70. margin: 0;
  71. }
  72. .list .item .name {
  73. color: #FB7299;
  74. font-size: 14px;
  75. font-weight: bold;
  76. }
  77. .list .item .text {
  78. color: #333;
  79. padding: 10px 0;
  80. }
  81. .list .item .time {
  82. color: #999;
  83. font-size: 12px;
  84. }
  85. </style>
  86. </head>
  87. <body>
  88. <div class="wrapper">
  89. <i class="avatar"></i>
  90. <textarea id="tx" placeholder="发一条友善的评论" rows="2" maxlength="200"></textarea>
  91. <button>发布</button>
  92. </div>
  93. <div class="wrapper">
  94. <span class="total">0/200字</span>
  95. </div>
  96. <div class="list">
  97. <div class="item" style="display: none;">
  98. <i class="avatar"></i>
  99. <div class="info">
  100. <p class="name">清风徐来</p>
  101. <p class="text">大家都辛苦啦,感谢各位大大的努力,能圆满完成真是太好了[笑哭][支持]</p>
  102. <p class="time">2022-10-10 20:29:21</p>
  103. </div>
  104. </div>
  105. </div>
  106. <script>
  107. const tx = document.querySelector('#tx')
  108. const total = document.querySelector('.total')
  109. const item = document.querySelector('.item')
  110. const text = document.querySelector('.text')
  111. const bt = document.querySelector('.wrapper button')
  112. tx.addEventListener('focus',function(){
  113. total.style.opacity = 1
  114. })
  115. tx.addEventListener('blur',function(){
  116. total.style.opacity = 0
  117. })
  118. tx,addEventListener('input',function(){
  119. total.innerHTML = `${tx.value.length}/200字`
  120. })
  121. function addComment(){
  122. //如果用户输入的不是纯空格
  123. //trim()去除字符串左右的空格
  124. if(tx.value.trim()!==''){
  125. item.style.display = 'block'
  126. text.innerHTML = tx.value
  127. }
  128. //清空文本域
  129. tx.value = ''
  130. total.innerHTML = `0/200字`
  131. }
  132. //按下回车发布评论
  133. tx.addEventListener('keyup',function(e){
  134. if(e.key === 'Enter'){
  135. addComment()
  136. }
  137. })
  138. bt.addEventListener('click',function(){
  139. addComment()
  140. })
  141. </script>
  142. </body>
  143. </html>

四、环境对象

this--当前函数运行时所处的环境

普通函数中this指向window

谁调用,this指向谁

五、回调函数

当一个函数做参数传递给另一个函数时,这个函数是回调函数

综合案例 -- tab栏切换

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>tab栏切换</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. .tab {
  14. width: 590px;
  15. height: 340px;
  16. margin: 20px;
  17. border: 1px solid #e4e4e4;
  18. }
  19. .tab-nav {
  20. width: 100%;
  21. height: 60px;
  22. line-height: 60px;
  23. display: flex;
  24. justify-content: space-between;
  25. }
  26. .tab-nav h3 {
  27. font-size: 24px;
  28. font-weight: normal;
  29. margin-left: 20px;
  30. }
  31. .tab-nav ul {
  32. list-style: none;
  33. display: flex;
  34. justify-content: flex-end;
  35. }
  36. .tab-nav ul li {
  37. margin: 0 20px;
  38. font-size: 14px;
  39. }
  40. .tab-nav ul li a {
  41. text-decoration: none;
  42. border-bottom: 2px solid transparent;
  43. color: #333;
  44. }
  45. .tab-nav ul li a.active {
  46. border-color: #e1251b;
  47. color: #e1251b;
  48. }
  49. .tab-content {
  50. padding: 0 16px;
  51. }
  52. .tab-content .item {
  53. display: none;
  54. }
  55. .tab-content .item.active {
  56. display: block;
  57. }
  58. </style>
  59. </head>
  60. <body>
  61. <div class="tab">
  62. <div class="tab-nav">
  63. <h3>每日特价</h3>
  64. <ul>
  65. <li><a class="active" href="javascript:;">精选</a></li>
  66. <li><a href="javascript:;">美食</a></li>
  67. <li><a href="javascript:;">百货</a></li>
  68. <li><a href="javascript:;">个护</a></li>
  69. <li><a href="javascript:;">预告</a></li>
  70. </ul>
  71. </div>
  72. <div class="tab-content">
  73. <div class="item active"><img src="./image/tab00.png" alt="" /></div>
  74. <div class="item"><img src="./image/tab01.png" alt="" /></div>
  75. <div class="item"><img src="./image/tab02.png" alt="" /></div>
  76. <div class="item"><img src="./image/tab03.png" alt="" /></div>
  77. <div class="item"><img src="./image/tab04.png" alt="" /></div>
  78. </div>
  79. </div>
  80. <script>
  81. const as = document.querySelectorAll('.tab-nav a')
  82. for(let i = 0; i < as.length; i++){
  83. as[i].addEventListener('mouseenter',function() {
  84. document.querySelector('.tab-nav .active').classList.remove('active')
  85. this.classList.add('active')
  86. document.querySelector('.tab-content .active').classList.remove('active')
  87. document.querySelector(`.tab-content .item:nth-child(${i+1})`).classList.add('active')
  88. })
  89. }
  90. </script>
  91. </body>
  92. </html>

本文转载自: https://blog.csdn.net/Michelle209/article/details/129986696
版权归原作者 Michelle209 所有, 如有侵权,请联系我们删除。

“前端学习--JS入门(5) 事件监听/事件类型/事件对象”的评论:

还没有评论