js 轮播图

秒速五厘米 2022-09-08 13:53 302阅读 0赞

文章目录

  • html
  • css
  • js
  • 参考博客

html

  1. <div class="container">
  2. <div class="wrap" style="left:-600px;">
  3. <img src="./img/4.png" alt="" class="img">
  4. <img src="./img/1.png" alt="" class="img">
  5. <img src="./img/2.png" alt="" class="img">
  6. <img src="./img/3.png" alt="" class="img">
  7. <img src="./img/4.png" alt="" class="img">
  8. <img src="./img/1.png" alt="" class="img">
  9. </div>
  10. <div class="buttons">
  11. <span class="btnIndex">1</span>
  12. <span class="btnIndex">2</span>
  13. <span class="btnIndex">3</span>
  14. <span class="btnIndex">4</span>
  15. </div>
  16. <a href="javascript:;" class="arrow arrow_left"><</a>
  17. <a href="javascript:;" class="arrow arrow_right">></a>
  18. </div>

css

  1. * {
  2. margin:0;
  3. padding:0;
  4. }
  5. a{
  6. text-decoration: none;
  7. }
  8. .container {
  9. position: relative;
  10. width: 100vw;
  11. height: calc(100vh - 56px);
  12. box-shadow: 0 0 5px green;
  13. overflow: hidden;
  14. }
  15. .wrap {
  16. position: absolute;
  17. width: 700vw;
  18. height: calc(100vh - 56px);
  19. z-index: 1;
  20. }
  21. img{
  22. background-color: coral;
  23. }
  24. .container .wrap img {
  25. float: left;
  26. width: 100vw;
  27. height: calc(100vh - 56px);
  28. }
  29. .container .buttons {
  30. position: absolute;
  31. right: 150px;
  32. bottom:20px;
  33. width: 200px;
  34. height: 10px;
  35. z-index: 2;
  36. }
  37. .container .buttons span {
  38. margin-left: 5px;
  39. display: inline-block;
  40. width: 20px;
  41. height: 20px;
  42. border-radius: 50%;
  43. background-color: green;
  44. text-align: center;
  45. color:white;
  46. cursor: pointer;
  47. }
  48. .container .buttons span.on{
  49. background-color: red;
  50. }
  51. .container .arrow {
  52. position: absolute;
  53. top: 35%;
  54. color: green;
  55. padding:0px 14px;
  56. border-radius: 50%;
  57. font-size: 50px;
  58. z-index: 2;
  59. display: none;
  60. }
  61. .container .arrow_left {
  62. left: 10px;
  63. }
  64. .container .arrow_right {
  65. right: 10px;
  66. }
  67. .container:hover .arrow {
  68. display: block;
  69. }
  70. .container .arrow:hover {
  71. background-color: rgba(0,0,0,0.2);
  72. }

js

  1. var wrap = document.querySelector(".wrap");
  2. var next = document.querySelector(".arrow_right");
  3. var prev = document.querySelector(".arrow_left");
  4. next.onclick = function () {
  5. next_pic();
  6. }
  7. var index = 0;
  8. // var dots = document.getElementsByTagName("span");
  9. var dots = document.getElementsByClassName("btnIndex")
  10. dots[index].className = "on btnIndex";
  11. function showCurrentDot() {
  12. for (var i = 0, len = dots.length; i < len; i++) {
  13. dots[i].className = "btnIndex";
  14. }
  15. dots[index].className = "on btnIndex";
  16. }
  17. prev.onclick = function () {
  18. prev_pic();
  19. }
  20. var prev_vw;
  21. var prev_index;
  22. let vw = document.body.clientWidth
  23. // console.log(vw)
  24. wrap.style.left = -vw + "px";
  25. // console.log(wrap.style.left )
  26. prev_index = parseInt(wrap.style.left)/vw
  27. // console.log(prev_index)
  28. window.onresize = function(){
  29. let vw = document.body.clientWidth
  30. // console.log(vw)
  31. // console.log(wrap.style.left)
  32. wrap.style.left = prev_index*vw + "px";
  33. }
  34. function next_pic() {
  35. let vw = document.body.clientWidth
  36. wrap.style.left = prev_index*vw + "px";
  37. var newLeft;
  38. //Math.abs(parseInt(wrap.style.left)-4*vw) < 20
  39. //parseInt(wrap.style.left) == -4*vw
  40. // console.log(vw)
  41. // console.log(wrap.style.left)
  42. if ( parseInt(wrap.style.left) == -4*vw) {
  43. console.log("进来")
  44. newLeft = -1*vw;
  45. } else {
  46. newLeft = parseInt(wrap.style.left) - vw;
  47. }
  48. prev_index = newLeft/vw
  49. wrap.style.left = newLeft + "px";
  50. // console.log(vw)
  51. console.log(wrap.style.left)
  52. index++;
  53. if (index > 3) {
  54. index = 0;
  55. }
  56. console.log(index)
  57. showCurrentDot();
  58. }
  59. function prev_pic() {
  60. let vw = document.body.clientWidth
  61. // console.log(vw)
  62. wrap.style.left = prev_index*vw + "px";
  63. var newLeft;
  64. if (parseInt(wrap.style.left) == -1*vw) {
  65. newLeft = -4*vw;
  66. } else {
  67. newLeft = parseInt(wrap.style.left) + vw;
  68. }
  69. prev_index = newLeft/vw
  70. wrap.style.left = newLeft + "px";
  71. index--;
  72. if (index < 0) {
  73. index = 3;
  74. }
  75. showCurrentDot();
  76. }
  77. var timer = null;
  78. function autoPlay() {
  79. timer = setInterval(function () {
  80. next_pic();
  81. }, 3000);
  82. }
  83. autoPlay();
  84. var container = document.querySelector(".container");
  85. container.onmouseenter = function () {
  86. clearInterval(timer);
  87. }
  88. container.onmouseleave = function () {
  89. autoPlay();
  90. }
  91. for (var i = 0, len = dots.length; i < len; i++) {
  92. (function (i) {
  93. dots[i].onclick = function () {
  94. let vw = document.body.clientWidth
  95. wrap.style.left = prev_index*vw + "px";
  96. var dis = index - i;
  97. if (index == 4 && parseInt(wrap.style.left) !== -5*vw) {
  98. dis = dis - 5;
  99. }
  100. //和使用prev和next相同,在最开始的照片5和最终的照片1在使用时会出现问题,导致符号和位数的出错,做相应地处理即可
  101. if (index == 0 && parseInt(wrap.style.left) !== -vw) {
  102. dis = 5 + dis;
  103. }
  104. // console.log(dis)
  105. prev_index = (parseInt(wrap.style.left) + dis * vw)/vw
  106. wrap.style.left = (parseInt(wrap.style.left) + dis * vw) + "px";
  107. index = i;
  108. showCurrentDot();
  109. }
  110. })(i);
  111. }
  112. $(".img").on("click",function(){
  113. console.log("img")
  114. console.log($(this).index())
  115. })

参考博客

原生js实现轮播图

发表评论

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

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

相关阅读