js轮播图,纯源码

分手后的思念是犯贱 2023-07-15 13:19 142阅读 0赞

最近也是在学js所以就做了个轮播图来玩玩

  1. CSS-------------------
  2. * {
  3. padding: 0;
  4. margin: 0;
  5. }
  6. .box img {
  7. width: 700px;
  8. height: 400px;
  9. }
  10. .box {
  11. position: absolute;
  12. top: 50%;
  13. left: 50%;
  14. transform: translate(-50%, -50%);
  15. -webkit-transform: translate(-50%, -50%);
  16. width: 700px;
  17. height: 400px;
  18. overflow: hidden;
  19. /* border: 2px solid red; */
  20. }
  21. .box1 {
  22. position: absolute;
  23. height: 400px;
  24. display: flex;
  25. }
  26. .left {
  27. position: absolute;
  28. background-color: black;
  29. width: 30px;
  30. height: 40px;
  31. opacity: .7;
  32. top: 190px;
  33. cursor: pointer;
  34. z-index: 22;
  35. }
  36. .right {
  37. position: absolute;
  38. background-color: black;
  39. width: 30px;
  40. height: 40px;
  41. opacity: .7;
  42. right: 0px;
  43. top: 190px;
  44. cursor: pointer;
  45. z-index: 22;
  46. }
  47. .circle {
  48. border-radius: 50%;
  49. position: absolute;
  50. left: 300px;
  51. bottom: 10px;
  52. display: flex;
  53. }
  54. .circle li {
  55. list-style: none;
  56. width: 15px;
  57. height: 15px;
  58. border-radius: 50%;
  59. background-color: pink;
  60. margin-left: 5px;
  61. cursor: pointer;
  62. }

JS代码

  1. window.onload = function() {
  2. //获取元素
  3. var box = document.querySelector('.box')
  4. var img = document.querySelectorAll('img')
  5. var left = document.querySelector('.left')
  6. var right = document.querySelector('.right')
  7. var box1 = document.querySelector('.box1')
  8. var circle = document.querySelector('.circle')
  9. //拿到图片的宽度
  10. var imgwdith = 700;
  11. // 动态设置box1长度
  12. box1.style.width = (img[0].offsetWidth * img.length) + 'px';
  13. //左右移动时使用的变量num
  14. var num = 0;
  15. //圆圈使用的变量o
  16. var o = 0;
  17. // 动态创建圆圈,与图片数量保持一致
  18. for (var i = 0; i < img.length - 1; i++) {
  19. var lis = document.createElement('li');
  20. //设置索引
  21. var index = lis.setAttribute('index', i);
  22. circle.appendChild(lis);
  23. // 给li添加点击事件
  24. circle.children[i].onclick = function() {
  25. for (var i = 0; i < circle.children.length; i++) {
  26. circle.children[i].style.backgroundColor = 'pink';
  27. }
  28. this.style.backgroundColor = 'white';
  29. //获取li的索引
  30. var index = this.getAttribute('index');
  31. //index控制
  32. num = index;
  33. o = index;
  34. animateW(box1, -index * imgwdith);
  35. }
  36. };
  37. circle.children[0].style.backgroundColor = 'white';
  38. //绑定右侧的点击事件
  39. right.onclick = function() {
  40. if (num == img.length - 1) {
  41. box1.style.left = 0 + 'px';
  42. num = 0;
  43. }
  44. num++;
  45. animateW(box1, -num * imgwdith);
  46. o++;
  47. //圆圈跟着动
  48. //如果到最后一个就回到0;
  49. if (o == circle.children.length) {
  50. o = 0;
  51. circle.children[o].style.backgroundColor = 'white'
  52. }
  53. circlecss();
  54. };
  55. //绑定左侧的点击事件
  56. left.onclick = function() {
  57. if (num == 0) {
  58. num = img.length - 1;
  59. box1.style.left = -num * img.length + 'px';
  60. }
  61. num--;
  62. animateW(box1, -num * imgwdith);
  63. o--;
  64. //圆圈跟着动
  65. //如果是第一个,点击之后变成最后一个;
  66. if (o < 0) {
  67. o = circle.children.length - 1;
  68. }
  69. circlecss();
  70. };
  71. //自动播放
  72. // 周期调用已经绑定事件的元素
  73. var int = setInterval(right.onclick, 2000);
  74. // 移入到容器中清除自动点击事件
  75. box.onmouseover = function() {
  76. clearInterval(int);
  77. }
  78. // 移出容器的时候继续调用自动点击的事件
  79. box.onmouseout = function() {
  80. int = setInterval(right.onclick, 2000);
  81. }
  82. //圆圈的样式变化函数
  83. function circlecss() {
  84. for (var i = 0; i < circle.children.length; i++) {
  85. circle.children[i].style.backgroundColor = "pink";
  86. }
  87. circle.children[o].style.backgroundColor = 'white';
  88. };
  89. // 动画函数
  90. function animateW(obj, target) {
  91. //先把原先的定时器清除,只保留一个.
  92. clearInterval(obj.time);
  93. obj.time = setInterval(function() {
  94. //步长 公式:(目标位置-现在的位置)/10
  95. var step = (target - obj.offsetLeft) / 10;
  96. step = step <= 0 ? Math.floor(step) : Math.ceil(step);
  97. if (obj.offsetLeft == target) {
  98. clearInterval(obj.time);
  99. }
  100. obj.style.left = obj.offsetLeft + step + 'px';
  101. }, 15);
  102. };
  103. }

HTML———

  1. </head>
  2. <body>
  3. <div class="box">
  4. <div class="left"></div>
  5. <div class="right"></div>
  6. <div class="box1">
  7. <img src="image/1.jpg" alt="">
  8. <img src="image/2.jpg" alt="">
  9. <img src="image/3.jpg" alt="">
  10. <img src="image/4.jpg" alt="">
  11. <img src="image/5.jpg" alt="">
  12. <img src="image/1.jpg" alt="">
  13. </div>
  14. <ol class="circle">
  15. </ol>
  16. </div>
  17. </body>

这个js和图片都是放在文件例来的,用的时候要引入
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
图片是上网找的,侵删;

发表评论

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

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

相关阅读

    相关 js实现淘宝商城

    需求:   循环无缝自动轮播3张图片,点击左右箭头可以手动切换图片,鼠标点击轮播图下面的小圆点会跳转到对应的第几张图片。鼠标放到轮播图的图片上时不再自动轮播,鼠标移开之后又