ASCII码 ASCII码

轮播图,xhr

发布于:2022-04-14 13:39:13  栏目:技术文档

标悬停时自动停止播放, 离开时又自动播放

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>轮播图</title>
  7. </head>
  8. <style>
  9. .slider {
  10. width: 500px;
  11. margin: auto;
  12. padding: 0 10px;
  13. margin-top:20px;
  14. }
  15. .slider .imgs {
  16. /* 图片容器必须要有高度,否则下面图片不能正常显示 */
  17. height: 300px;
  18. }
  19. .slider .imgs img {
  20. /* 图片完全充满父级空间显示 */
  21. height: 100%;
  22. width: 100%;
  23. /* 默认图片全部隐藏,只有有active的图片才显示 */
  24. display: none;
  25. }
  26. .slider .imgs img:hover {
  27. cursor: pointer;
  28. }
  29. /* 默认显示第一张 */
  30. .slider .imgs img.active {
  31. display: block;
  32. }
  33. /* 轮播图按钮组 */
  34. .slider .btns {
  35. /* 按钮水平一排显示,用flex,且水平居中 */
  36. display: flex;
  37. place-content: center;
  38. }
  39. .slider .btns span {
  40. /* 按钮宽高相同,确定显示成一个正圆 */
  41. width: 8px;
  42. height: 8px;
  43. /* 加上红色背景和数字是为了布局时可以看到,一会更去掉 */
  44. background-color: rgba(255, 255, 255, 255);
  45. /* 50%可确保显示为正圆 */
  46. border-radius: 50%;
  47. /* 按钮上外边距负值,可将它上移,可移动到图片中下方 */
  48. margin: -12px 3px 5px;
  49. /* z-index: 99; */
  50. }
  51. .slider .btns span.active {
  52. background-color: red;
  53. }
  54. </style>
  55. <body>
  56. <!-- 1.写个轮播图 -->
  57. <!-- 2.写个图片盒子 -->
  58. <!-- 3.写个按钮盒子 -->
  59. <div class="slider">
  60. <div class="imgs">
  61. <img src="images/1.jpg" alt="" data-index="1">
  62. <img src="images/2.jpg" alt="" data-index="2" class="active">
  63. <img src="images/3.jpg" alt="" data-index="3">
  64. </div>
  65. <div class="btns">
  66. <span data-index="1" onclick="setActive()" class="active"></span>
  67. <span data-index="2" onclick="setActive()"></span>
  68. <span data-index="3" onclick="setActive()"></span>
  69. </div>
  70. </div>
  71. </body>
  72. <script>
  73. //获取图片按钮
  74. const imgs = document.querySelectorAll('.slider .imgs img');
  75. const btns = document.querySelectorAll('.slider .btns span');
  76. //设置激活状态
  77. function setActive() {
  78. //1.获取到当前点击的按钮
  79. console.log(event.target);
  80. //2.把图片和按钮的active都清空
  81. imgs.forEach((img) => img.classList.remove("active"))
  82. btns.forEach(function (btn) {
  83. btn.classList.remove('active');
  84. })
  85. //3.把active显示到当前点击的按钮上,和对应的data-index上
  86. event.target.classList.add('active')
  87. imgs.forEach(function (key) {
  88. if (key.dataset.index === event.target.dataset.index) {
  89. key.classList.add('active')
  90. }
  91. })
  92. }
  93. //实现自动播放
  94. //需要定时器 setInterval()除了回调和时间外,还可传入第三个参数,做为回调的参数
  95. let sj = setInterval(function (arr) {
  96. //1.头部删除
  97. let a = arr.shift();
  98. //2.尾部相加
  99. arr.push(a);
  100. //console.log(arr);
  101. //.事件派发
  102. btns[a].dispatchEvent(new Event('click'))
  103. }, 2000, Object.keys(btns))
  104. // console.log('这是按钮',Object.keys(btns))
  105. // console.log('这是图片',Object.keys(imgs))
  106. //当鼠标移动到图片时候停止轮播
  107. //给定时器个变量
  108. //获取当前图片盒子
  109. const himg = document.querySelector('.slider .imgs');
  110. console.log(himg);
  111. //鼠标移动到当前盒子是清除定时器 mouseover(移入)
  112. himg.addEventListener("mouseover", function () {
  113. clearInterval(sj)
  114. console.log('停止滚动')
  115. });
  116. //当鼠标离开是继续轮播
  117. //获取当前图片盒子 上面以及获取到了我们直接拿来使用
  118. //鼠标离开到当前盒子是继续轮播 mouseout(移除)
  119. himg.addEventListener("mouseout", function () {
  120. sj = setInterval(function (arr) {
  121. let a = arr.shift();
  122. arr.push(a);
  123. btns[a].dispatchEvent(new Event('click'))
  124. }, 2000, Object.keys(btns))
  125. console.log('继续滚动')
  126. })
  127. //向左点击
  128. //获取想左点击按钮
  129. //
  130. </script>
  131. </html>

xhr演示

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>xhr.传参</title>
  7. </head>
  8. <body>
  9. <button onclick="getUser1(this)">用户信息: xhr</button>
  10. </body>
  11. <script>
  12. // 1. 传统 XHR
  13. // 1. 创建对象: new XMLHttpRequest();
  14. // 2. 响应类型: xhr.responseType = "json";
  15. // 3. 配置参数: xhr.open("GET", url, true);
  16. // 4. 请求回调: xhr.onload = () => console.log(xhr.response);
  17. // 5. 失败回调: xhr.onerror = () => console.log("Error");
  18. // 6. 发起请求: xhr.send(null);
  19. // xhr 至少监听2个事件: load,error, 调用2个函数: open,send
  20. // post请求,需要设置一下请求头与请求的数据,其它与get请求完全相同
  21. function getuser1() {
  22. //1.创建对象:
  23. const xhr = new XMLHttpRequest();
  24. // 2. 响应类型
  25. xhr.responseType = "json";
  26. // 3. 配置参数
  27. let url = "hhttp://xhr411.cc/users.php?id=2";
  28. xhr.open("GET",url,)
  29. // 4. 请求回调:
  30. xhr.onload = function() {
  31. console.log(xhr.response)
  32. }
  33. //5.失败回调
  34. xhr.onerror = function () {
  35. console.log('请求失败')
  36. }
  37. //6. 发起请求
  38. xhr.send(null);
  39. }
  40. </script>
  41. </html>
相关推荐
阅读 +