JavaScript原生普通自动轮播图之函数封装

释放双眼,带上耳机,听听看~!
00:00
00:00
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style type="text/css">
  7. * {
  8. padding: 0;
  9. margin: 0;
  10. list-style: none;
  11. border: 0;
  12. }
  13. .all {
  14. width: 500px;
  15. height: 200px;
  16. padding: 7px;
  17. border: 1px solid #ccc;
  18. margin: 100px auto;
  19. position: relative;
  20. }
  21. .screen {
  22. width: 500px;
  23. height: 200px;
  24. overflow: hidden;
  25. position: relative;
  26. }
  27. .screen li {
  28. width: 500px;
  29. height: 200px;
  30. overflow: hidden;
  31. float: left;
  32. }
  33. .screen ul {
  34. position: absolute;
  35. left: 0;
  36. top: 0px;
  37. width: 5000px;
  38. }
  39. .all ol {
  40. position: absolute;
  41. right: 10px;
  42. bottom: 10px;
  43. line-height: 20px;
  44. text-align: center;
  45. }
  46. .all ol li {
  47. float: left;
  48. width: 20px;
  49. height: 20px;
  50. background: #fff;
  51. border: 1px solid #ccc;
  52. margin-left: 10px;
  53. cursor: pointer;
  54. }
  55. .all ol li.current {
  56. background: yellow;
  57. }
  58. #arr {
  59. display: none;
  60. }
  61. #arr span {
  62. width: 40px;
  63. height: 40px;
  64. position: absolute;
  65. left: 5px;
  66. top: 50%;
  67. margin-top: -20px;
  68. background: #000;
  69. cursor: pointer;
  70. line-height: 40px;
  71. text-align: center;
  72. font-weight: bold;
  73. font-family: '黑体';
  74. font-size: 30px;
  75. color: #fff;
  76. opacity: 0.3;
  77. border: 1px solid #fff;
  78. }
  79. #arr #right {
  80. right: 5px;
  81. left: auto;
  82. }
  83. </style>
  84. </head>
  85. <body>
  86. <div class="all" id='box'>
  87. <div class="screen">
  88. <ul>
  89. <li><img src="images/1.jpg" width="500" height="200" /></li>
  90. <li><img src="images/2.jpg" width="500" height="200" /></li>
  91. <li><img src="images/3.jpg" width="500" height="200" /></li>
  92. <li><img src="images/4.jpg" width="500" height="200" /></li>
  93. <li><img src="images/5.jpg" width="500" height="200" /></li>
  94. <li><img src="images/5.jpg" width="500" height="200" /></li>
  95. </ul>
  96. <ol>
  97. <!-- 动态创建的小方块,添加在这里,样式已经给写好了-->
  98. </ol>
  99. </div>
  100. <div id="arr">
  101. <span id="left">&lt;</span>
  102. <span id="right">&gt;</span>
  103. </div>
  104. </div>
  105. <script>
  106. //思路
  107. //1.获取对应的元素
  108. // 获取移动的单位宽
  109. //2.根据轮播图片的个数,来动态的生成右下角的小方块们.
  110. // 右下角的小方块其实就是ol中的li标签
  111. // ol中的第一个li标签要有颜色(添加一个current类)
  112. //3.简单轮播
  113. // 遍历ol中的每一个li标签
  114. // 给每一个li标签添加索引
  115. // 给每一个li标签设置单击事件.
  116. // 颜色排他
  117. // 获取当前点击的小方块的索引
  118. // 计算要移动的目标位置
  119. // 调用animate来做动画
  120. // 注意: ******
  121. //4.左右焦点轮播
  122. // 显示隐藏左右焦点
  123. // 右边焦点设置点击事件.
  124. // 移动出去的图片张数++
  125. // 计算移动的目标位置
  126. // 调用animate来移动
  127. // 右下角小方块的颜色要对应上
  128. // 无限无缝轮播
  129. // 左边焦点设置点击事件.
  130. //1.获取对应的元素.
  131. let box = document.getElementById('box'); //大盒子
  132. let screenBox = document.querySelector('.screen');
  133. let unitWidth = screenBox.offsetWidth; //移动的单位宽
  134. let ul1 = screenBox.children[0]; //要动画移动的ul标签.
  135. let lisUL = ul1.children; //ul中的所有li标签
  136. let ol1 = screenBox.children[1]; //ol标签,装右下角所有的小方块的.
  137. let arrDiv = document.getElementById('arr'); //左右焦点盒子
  138. let leftSpan = document.getElementById('left'); //左焦点
  139. let rightSpan = document.getElementById('right'); //右焦点
  140. //2.根据轮播图图片的个数,来生成右下角的小方块.
  141. for (let i = 0; i < lisUL.length; i++) {
  142. //创建li标签
  143. let liNew = document.createElement('li');
  144. //给li标签设置内容
  145. liNew.innerText = i + 1;
  146. //把生成的li添加到ol中
  147. ol1.appendChild(liNew);
  148. }
  149. // 默认ol中的第一个li标签有一个current类.
  150. let lisOl = ol1.children; //ol中的所有li标签,也就是右下角的所有小方块.
  151. lisOl[0].setAttribute('class', 'current');
  152. //3.简单轮播
  153. // 遍历右下角的每一个小方块(ol中的li标签们)
  154. for (let i = 0; i < lisOl.length; i++) {
  155. //给每一个小方块li添加索引
  156. lisOl[i].setAttribute('index', i);
  157. //给每一个小方块li注册点击事件.
  158. lisOl[i].onclick = function () {
  159. //颜色排他.
  160. for (let j = 0; j < lisOl.length; j++) {
  161. lisOl[j].removeAttribute('class');
  162. }
  163. this.setAttribute('class', 'current');
  164. //获取当前点击的小方块的索引
  165. let idx = this.getAttribute('index');
  166. //计算要运动的目标位置
  167. let target = -idx * unitWidth;
  168. //调用animate来做动画
  169. animate(ul1, target);
  170. //注意: *******索引要统一
  171. picIndex = liIndex = idx;
  172. }
  173. }
  174. //4.左右焦点轮播.
  175. //4.0 声明一个变量,用来记录图片滚出去的个数.
  176. let picIndex = 0;
  177. //4.0 给ul中再添加一张轮播图片(其实添加的这一张就是第一张一模一样的.)
  178. ul1.appendChild(lisUL[0].cloneNode(true));
  179. //4.0 声明一个变量,用来记录右下角的小方块滚出去的个数.
  180. let liIndex = 0;
  181. //4.1 显示隐藏左右焦点.
  182. box.onmouseover = function () {
  183. arrDiv.style.display = 'block';
  184. //停止自动轮播
  185. clearInterval(timerId);
  186. }
  187. box.onmouseout = function () {
  188. arrDiv.style.display = 'none';
  189. //重新自动轮播
  190. timerId = setInterval(nextPic, 2000);
  191. }
  192. //4.2 右边焦点设置点击事件.
  193. rightSpan.onclick = function () {
  194. nextPic();
  195. }
  196. //把右边焦点点击事件代码封装成一个函数
  197. function nextPic() {
  198. //无限无缝轮播.
  199. //如果当前索引是5,显示的就是克隆的哪一张(普通用户看到的是第一张)
  200. //我们就应该马上把索引改成0,位置马上改成真正的第一张.
  201. if (picIndex == lisUL.length - 1) {
  202. picIndex = 0;
  203. ul1.style.left = '0px';
  204. }
  205. // console.log(lisUL.length);
  206. //移动出去的图片张数++
  207. picIndex++;
  208. //计算目标位置
  209. let target = -picIndex * unitWidth;
  210. //调用animate函数来做动画
  211. animate(ul1, target);
  212. //右下角小方块们的颜色要对应上.
  213. liIndex++;
  214. if (liIndex == lisUL.length - 1) {
  215. liIndex = 0;
  216. }
  217. console.log(liIndex);
  218. for (let i = 0; i < lisOl.length; i++) {
  219. lisOl[i].removeAttribute('class');
  220. }
  221. lisOl[liIndex].setAttribute('class', 'current');
  222. }
  223. //4.3 左边焦点设置点击事件.
  224. leftSpan.onclick = function () {
  225. //无限无缝轮播.
  226. //如果当前索引是5,显示的就是克隆的哪一张(普通用户看到的是第一张)
  227. //我们就应该马上把索引改成0,位置马上改成真正的第一张.
  228. if (picIndex == 0) {
  229. picIndex = lisUL.length - 1;
  230. ul1.style.left = -(lisUL.length - 1) * unitWidth + 'px';
  231. }
  232. //移动出去的图片张数--
  233. picIndex--;
  234. //计算目标位置
  235. let target = -picIndex * unitWidth;
  236. //调用animate函数来做动画
  237. animate(ul1, target);
  238. //右下角小方块们的颜色要对应上.
  239. liIndex--;
  240. if (liIndex < 0) {
  241. liIndex = lisUL.length - 2; // 索引比图片少 1 再加上克隆的一张图片 所以 需要减去 2
  242. }
  243. console.log(liIndex);
  244. for (let i = 0; i < lisOl.length; i++) {
  245. lisOl[i].removeAttribute('class');
  246. }
  247. lisOl[liIndex].setAttribute('class', 'current');
  248. }
  249. //5.自动轮播
  250. //设置计时器,间隔时间去调用nextPic
  251. let timerId = setInterval(nextPic, 2000);
  252. </script>
  253. <script>
  254. // 1、 匀速轮播
  255. // 封装一个animate函数
  256. // 01 - 运动的目标位置不写死.
  257. // 02 - 做运动的元素不写死
  258. // 03 - 运动的方向不写死
  259. // function animate(ele, target) {
  260. // //设置新计时器之前要清空老的计时器
  261. // clearInterval(ele.timerId);
  262. // //设置计时器
  263. // ele.timerId = setInterval(function () {
  264. // //获取元素当前的定位的left值
  265. // let currentLeft = ele.offsetLeft;
  266. // //设置步长
  267. // let step = target > currentLeft ? 30 : -30;
  268. // //计算运动后的位置.
  269. // currentLeft += step;
  270. // // console.log(currentLeft);
  271. // //判断赋值
  272. // if (Math.abs(target - currentLeft) > Math.abs(step)) {
  273. // ele.style.left = currentLeft + "px";
  274. // } else {
  275. // ele.style.left = target + "px";
  276. // clearInterval(ele.timerId);
  277. // }
  278. // }, 50);
  279. // }
  280. // 2、缓动轮播
  281. //封装一个animateSlow函数
  282. //01-运动的目标位置不写死.
  283. //02-做运动的元素不写死
  284. //03-运动的方向不写死
  285. function animate(ele, target) {
  286. //设置新计时器之前要清空老的计时器
  287. clearInterval(ele.timerId);
  288. //设置计时器
  289. ele.timerId = setInterval(function () {
  290. //获取元素当前的定位的left值
  291. let currentLeft = ele.offsetLeft;
  292. //设置步长
  293. let step = (target - currentLeft) / 5;
  294. step = step > 0 ? Math.ceil(step) : Math.floor(step);
  295. //计算运动后的位置.
  296. currentLeft += step;
  297. // console.log(currentLeft);
  298. //判断赋值
  299. if (Math.abs(target - currentLeft) > Math.abs(step)) {
  300. ele.style.left = currentLeft + "px";
  301. } else {
  302. ele.style.left = target + "px";
  303. clearInterval(ele.timerId);
  304. }
  305. }, 50);
  306. }
  307. </script>
  308. </body>
  309. </html>
内容投诉
JavaScript

JavaScript缓动动画的封装-添加多属性

2020-7-17 11:47:01

JavaScript

Js中伪数组转真数组的五种方法

2020-7-27 17:45:38

0 条回复 A文章作者 M管理员
欢迎您,新朋友,感谢参与互动!
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
私信列表
搜索