crm开发定制JS实战——轮播图

目录


一、crm开发定制轮播图介绍

       crm开发定制现在我们在很多网站上crm开发定制都能看到轮播图,像某东、某宝、crm开发定制某猫等等大小型网站上都有应用。下面就是某宝上的轮播图样式。

 二、原理

  将一些图片在一行中平铺,然后计算偏移量再利用定时器实现定时轮播。

三、轮播图基本htm布局

       主要就是创建一个大盒子里面存放轮播图的图片标签,cicle标签下的div主要是为后期存放实现图片轮播下面的小圆点。

  1. <div class="banner">
  2. <ul class="imgList">
  3. <li><img src="../1.png" alt=""></li>
  4. <li><img src="../2.jpg" alt=""></li>
  5. <li><img src="../3.jpg" alt=""></li>
  6. <li><img src="../4.jpg" alt=""></li>
  7. </ul>
  8. <div class="circle"> </div>
  9. </div>

四、轮播图CSS布局

        定义全局,消除页面对我们创建CSS布局时的影响。布局全局样式,消除自带小黑点使用定位消除高度塌陷。

  1. * {
  2. margin: 0px;
  3. padding: 0px;
  4. }
  5. .banner {
  6. width: 600px;
  7. margin: auto;
  8. border: 10px solid greenyellow;
  9. height: 350px;
  10. position: relative;
  11. overflow: hidden;
  12. }
  13. .imgList {
  14. list-style: none;
  15. /* width: 2480px; */
  16. position: absolute;
  17. /* left:-620px; */
  18. }

           设置图片大小,让图片在我们之前设置的边框里。

  1. .imgList img {
  2. width: 600px;
  3. height: 350px;
  4. }
  5. .imgList li {
  6. float: left;
  7. margin-right: 20px;
  8. }

        定义a标签,生成点击小圆点,产生点击前后效果对比样式。

  1. .circle {
  2. position: absolute;
  3. bottom: 15px;
  4. left: 50%;
  5. transform: translateX(-50%);
  6. }
  7. .circle a {
  8. width: 15px;
  9. height: 15px;
  10. background: yellow;
  11. display: block;
  12. border-radius: 50%;
  13. opacity: .5;
  14. float: left;
  15. margin-right: 5px;
  16. cursor: pointer;
  17. }
  18. .circle a.hover {
  19. background: black;
  20. opacity: .8;
  21. }

五、轮播图JS布局

         首先先获取元素,给父类设置整个图片的宽度,并利用设置好的CSS样式创建底部小圆点。

  1. window.onload = function () {
  2. var imgList = document.querySelector('.imgList');
  3. var circle = document.querySelector('.circle');
  4. var thisIndex = 0;
  5. var imgListLi = imgList.children;
  6. var circleA = circle.children;
  7. var flag = true;
  8. imgList.style.width = imgList.children.length * 620 + 'px';
  9. for (var i = 0; i < imgList.children.length; i++) {
  10. var aNode = document.createElement('a');
  11. aNode.setAttribute('index', i); //设置自定义属性
  12. if (i == 0) {
  13. aNode.className = 'hover';
  14. }
  15. circle.appendChild(aNode);
  16. }

         设置监听器为点击按钮实现图片的移动,并用获取元素的方法解决点击圆点附近区域跳转的bug事件。

  1. circle.addEventListener('click', function (e) {
  2. if (flag) {
  3. flag = false;
  4. // console.log(e.target);
  5. if (e.target.nodeName != 'A') {
  6. return false;
  7. }
  8. thisIndex = e.target.getAttribute('index');
  9. // imgList.style.left = -thisIndex * 620 + 'px';
  10. slow(imgList, -thisIndex * 620, function () {
  11. flag = true;
  12. });
  13. circleChange();
  14. }
  15. })

         利用函数和节流阀解决图片在跳转到最后一张图片无法返回第一张图片的问题,节流阀用来解决图片移动出现左右抖动横跳现象,并设置了图片自动播放功能。

  1. function antoChange() {
  2. setInterval(function () {
  3. if (flag) {
  4. flag = false;
  5. if (thisIndex >= circleA.length) {
  6. thisIndex = 0;
  7. }
  8. slow(imgList, -thisIndex * 620, function () {
  9. flag = true;
  10. });
  11. circleChange();
  12. thisIndex++;
  13. }
  14. }, 3000);
  15. }

        剩下主要用来解决图片左右移动速度和图片移动同时图片下方小圆点同步移动,以及移动过程中小圆点样式的变化。

  1. function circleChange() {
  2. for (var i = 0; i < circleA.length; i++) {
  3. circleA[i].className = '';
  4. }
  5. circleA[thisIndex].className = 'hover';
  6. }
  7. function slow(obj, target, callback) {
  8. obj.myInter = setInterval(function () {
  9. var offsetLeft = obj.offsetLeft;
  10. var num = (target - offsetLeft) / 10;
  11. num > 0 ? num = Math.ceil(num) : num = Math.floor(num);
  12. if (offsetLeft == target) {
  13. clearInterval(obj.myInter);
  14. callback && callback();
  15. } else {
  16. obj.style.left = offsetLeft + num + 'px';
  17. }
  18. }, 10)
  19. }
  20. antoChange();
  21. }

六、轮播图效果

七、完整代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>轮播图</title>
  6. <!-- <link rel="stylesheet" href="style.css"> -->
  7. <style>
  8. * {
  9. margin: 0px;
  10. padding: 0px;
  11. }
  12. .banner {
  13. width: 600px;
  14. margin: auto;
  15. border: 10px solid greenyellow;
  16. height: 350px;
  17. position: relative;
  18. overflow: hidden;
  19. }
  20. .imgList {
  21. list-style: none;
  22. /* width: 2480px; */
  23. position: absolute;
  24. /* left:-620px; */
  25. }
  26. .imgList img {
  27. width: 600px;
  28. height: 350px;
  29. }
  30. .imgList li {
  31. float: left;
  32. margin-right: 20px;
  33. }
  34. .circle {
  35. position: absolute;
  36. bottom: 15px;
  37. left: 50%;
  38. transform: translateX(-50%);
  39. }
  40. .circle a {
  41. width: 15px;
  42. height: 15px;
  43. background: yellow;
  44. display: block;
  45. border-radius: 50%;
  46. opacity: .5;
  47. float: left;
  48. margin-right: 5px;
  49. cursor: pointer;
  50. }
  51. .circle a.hover {
  52. background: black;
  53. opacity: .8;
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <div class="banner">
  59. <ul class="imgList">
  60. <li><img src="../1.png" alt=""></li>
  61. <li><img src="../2.jpg" alt=""></li>
  62. <li><img src="../3.jpg" alt=""></li>
  63. <li><img src="../4.jpg" alt=""></li>
  64. </ul>
  65. <div class="circle">
  66. <!-- <a href=""></a>
  67. <a href=""></a>
  68. <a href=""></a>
  69. <a href=""></a> -->
  70. </div>
  71. </div>
  72. <script>
  73. window.onload = function () {
  74. var imgList = document.querySelector('.imgList');
  75. var circle = document.querySelector('.circle');
  76. var thisIndex = 0;
  77. var imgListLi = imgList.children;
  78. var circleA = circle.children;
  79. var flag = true;
  80. imgList.style.width = imgList.children.length * 620 + 'px';
  81. for (var i = 0; i < imgList.children.length; i++) {
  82. var aNode = document.createElement('a');
  83. aNode.setAttribute('index', i); //设置自定义属性
  84. if (i == 0) {
  85. aNode.className = 'hover';
  86. }
  87. circle.appendChild(aNode);
  88. }
  89. circle.addEventListener('click', function (e) {
  90. if (flag) {
  91. flag = false;
  92. // console.log(e.target);
  93. if (e.target.nodeName != 'A') {
  94. return false;
  95. }
  96. thisIndex = e.target.getAttribute('index');
  97. // imgList.style.left = -thisIndex * 620 + 'px';
  98. slow(imgList, -thisIndex * 620, function () {
  99. flag = true;
  100. });
  101. circleChange();
  102. }
  103. })
  104. function antoChange() {
  105. setInterval(function () {
  106. if (flag) {
  107. flag = false;
  108. if (thisIndex >= circleA.length) {
  109. thisIndex = 0;
  110. }
  111. slow(imgList, -thisIndex * 620, function () {
  112. flag = true;
  113. });
  114. circleChange();
  115. thisIndex++;
  116. }
  117. }, 3000);
  118. }
  119. function circleChange() {
  120. for (var i = 0; i < circleA.length; i++) {
  121. circleA[i].className = '';
  122. }
  123. circleA[thisIndex].className = 'hover';
  124. }
  125. function slow(obj, target, callback) {
  126. obj.myInter = setInterval(function () {
  127. var offsetLeft = obj.offsetLeft;
  128. var num = (target - offsetLeft) / 10;
  129. num > 0 ? num = Math.ceil(num) : num = Math.floor(num);
  130. if (offsetLeft == target) {
  131. clearInterval(obj.myInter);
  132. callback && callback();
  133. } else {
  134. obj.style.left = offsetLeft + num + 'px';
  135. }
  136. }, 10)
  137. }
  138. antoChange();
  139. }
  140. </script>
  141. </body>
  142. </html>

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发