原生js点击轮播 点击切换图片

╰+哭是因爲堅強的太久メ 2022-05-27 06:51 377阅读 0赞

20180413143155395

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>点击轮播图</title>
  6. </head>
  7. <style>
  8. .container {
  9. position: relative;
  10. width: 600px;
  11. height: 400px;
  12. margin:100px auto 0 auto;
  13. box-shadow: 0 0 5px green;
  14. overflow: hidden;
  15. }
  16. .wrap {
  17. position: absolute;
  18. width: 4200px;
  19. height: 400px;
  20. z-index: 1;
  21. }
  22. .container .wrap img {
  23. float: left;
  24. width: 600px;
  25. height: 400px;
  26. }
  27. .container .buttons {
  28. position: absolute;
  29. right: 40%;
  30. bottom:20px;
  31. z-index: 2;
  32. }
  33. .container .buttons span {
  34. margin-left: 10px;
  35. display: inline-block;
  36. width: 7px;
  37. height: 7px;
  38. background-color: #D4D4D4;
  39. text-align: center;
  40. color:white;
  41. cursor: pointer;
  42. }
  43. .container .buttons span.on{
  44. background-color: #558949;
  45. }
  46. .container .arrow {
  47. position: absolute;
  48. top: 35%;
  49. color: green;
  50. padding:0px 14px;
  51. border-radius: 50%;
  52. font-size: 50px;
  53. z-index: 2;
  54. display: none;
  55. }
  56. </style>
  57. <body>
  58. <div class="container">
  59. <div class="wrap" style="left:-600px;">
  60. <img alt="">
  61. <img src="./img/jd_app3.png" alt="">
  62. <img src="./img/jd_app4.png" alt="">
  63. <img src="./img/jd_app5.png" alt="">
  64. <img src="./img/jd_app6.png" alt="">
  65. <img src="./img/jd_app7.png" alt="">
  66. </div>
  67. <div class="buttons">
  68. <span></span>
  69. <span></span>
  70. <span></span>
  71. <span></span>
  72. <span></span>
  73. </div>
  74. </div>
  75. <script>
  76. var wrap = document.querySelector(".wrap");
  77. var index = 0;
  78. var dots = document.getElementsByTagName("span");
  79. function showCurrentDot () {
  80. for(var i = 0, len = dots.length; i < len; i++){
  81. dots[i].className = "";
  82. }
  83. dots[index].className = "on";
  84. }
  85. index++;
  86. if(index > 4){
  87. index = 0;
  88. }
  89. index--;
  90. if(index < 0){
  91. index = 4;
  92. }
  93. showCurrentDot();
  94. for (var i = 0, len = dots.length; i < len; i++){
  95. (function(i){
  96. dots[i].onclick = function () {
  97. var dis = index - i;
  98. if(index == 4 && parseInt(wrap.style.left)!==-3000){
  99. dis = dis - 5;
  100. }
  101. //和使用prev和next相同,在最开始的照片5和最终的照片1在使用时会出现问题,导致符号和位数的出错,做相应地处理即可
  102. if(index == 0 && parseInt(wrap.style.left)!== -600){
  103. dis = 5 + dis;
  104. }
  105. wrap.style.left = (parseInt(wrap.style.left) + dis * 600)+"px";
  106. index = i;
  107. showCurrentDot();
  108. }
  109. })(i);
  110. }
  111. </script>
  112. </body>
  113. </html>

发表评论

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

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

相关阅读

    相关 js实现

    效果图 今天讲下如何利用js做个点击操作的轮播图,效果图如下,因为无法传视频,只能截图了,代码在下面讲解,需要可以自己打出来运行。 ![在这里插入图片描述][wate