当前位置:   article > 正文

无缝轮播图(自动轮播+点击轮播+拖拽轮播)

无缝轮播图(自动轮播+点击轮播+拖拽轮播)

废话不多说,直接上代码(注:js部分代码会有截图对其解释)

一、HTML

  1. <div class="bigBox">
  2. <!-- 轮播主体 -->
  3. <div class="box">
  4. <div class="littleBox">1</div>
  5. <div class="littleBox">2</div>
  6. <div class="littleBox">3</div>
  7. </div>
  8. <!-- 底部小点 -->
  9. <div class="point">
  10. <div class="active"></div>
  11. <div></div>
  12. <div></div>
  13. </div>
  14. <!-- 上一页 -->
  15. <div class="prev"></div>
  16. <!-- 下一页 -->
  17. <div class="next"></div>
  18. </div>

二、CSS

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. .bigBox {
  6. width: 300px;
  7. height: 180px;
  8. border: 2px solid black;
  9. overflow: hidden;
  10. margin: 100px auto;
  11. position: relative;
  12. }
  13. /* 轮播主体 */
  14. .box {
  15. width: 900px;
  16. display: flex;
  17. transition: all .6s;
  18. }
  19. .littleBox {
  20. text-align: center;
  21. line-height: 180px;
  22. width: 300px;
  23. height: 180px;
  24. }
  25. .littleBox:nth-of-type(1) {
  26. background-color: skyblue;
  27. }
  28. .littleBox:nth-of-type(2) {
  29. background-color: pink;
  30. }
  31. .littleBox:nth-of-type(3) {
  32. background-color: yellow;
  33. }
  34. /* 小点 */
  35. .point {
  36. position: absolute;
  37. width: 50px;
  38. height: 10px;
  39. bottom: 10px;
  40. left: 50%;
  41. transform: translateX(-50%);
  42. display: flex;
  43. justify-content: space-evenly;
  44. cursor: pointer;
  45. }
  46. .point div {
  47. width: 10px;
  48. height: 10px;
  49. background-color: rgb(151, 151, 151);
  50. border-radius: 10px;
  51. }
  52. .point .active {
  53. background-color: white;
  54. }
  55. /* 上一页/下一页 */
  56. .prev::after,
  57. .next::after {
  58. position: absolute;
  59. width: 20px;
  60. height: 30px;
  61. top: 50%;
  62. transform: translateY(-50%);
  63. background-color: aliceblue;
  64. line-height: 30px;
  65. text-align: center;
  66. cursor: pointer;
  67. }
  68. /* 上一页 */
  69. .prev::after {
  70. content: '<';
  71. left: 0;
  72. border-top-right-radius: 15px;
  73. border-bottom-right-radius: 15px;
  74. }
  75. /* 下一页 */
  76. .next::after {
  77. content: '>';
  78. right: 0;
  79. border-top-left-radius: 15px;
  80. border-bottom-left-radius: 15px;
  81. }

三、JS以及解释截图

  1. // dom元素
  2. const doms = {
  3. box: document.querySelector('.box'),
  4. point: document.querySelectorAll('.point div'),
  5. prev: document.querySelector('.prev'),
  6. next: document.querySelector('.next')
  7. }
  8. // 当前所在页面(从0开始)
  9. let currentIndex = 0
  10. // 切换页面
  11. function moved(index) {
  12. currentIndex = index
  13. const x = -300 * index
  14. doms.point.forEach((item, i) => {
  15. item.classList.remove('active')
  16. })
  17. doms.point[index].classList.add('active')
  18. doms.box.style.transform = `translateX(${x}px)`
  19. }
  20. // 上一页点击事件
  21. function changePrev() {
  22. currentIndex === 0 ? currentIndex = 2 : currentIndex--
  23. moved(currentIndex)
  24. }
  25. doms.prev.addEventListener('click', changePrev)
  26. // 下一页点击事件
  27. function changeNext() {
  28. currentIndex === 2 ? currentIndex = 0 : currentIndex++
  29. moved(currentIndex)
  30. }
  31. doms.next.addEventListener('click', changeNext)
  32. // 小点点击事件
  33. doms.point.forEach((item, i) => {
  34. item.addEventListener('click', function () {
  35. moved(i)
  36. })
  37. })
  38. // 自动轮播
  39. let timer = setInterval(changeNext, 2000)
  40. // 鼠标移入后清除自动轮播
  41. doms.box.addEventListener('mouseenter', function () {
  42. clearInterval(timer)
  43. })
  44. // box元素相对视口的位置
  45. const boxPosition = {
  46. x: Math.floor(doms.box.getBoundingClientRect().x),
  47. y: Math.floor(doms.box.getBoundingClientRect().y)
  48. }
  49. // 鼠标最初位置
  50. let mouseX = 0
  51. // box移动的位置
  52. let transformX = 0
  53. let x = 0
  54. // 手动拖拽
  55. function onMoueseDown(e) {
  56. mouseX = e.clientX - boxPosition.x
  57. doms.box.addEventListener('mousemove', onMoueseMove)
  58. }
  59. function onMoueseMove(e) {
  60. e.preventDefault()
  61. x = transformX + -1 * (mouseX - (e.clientX - boxPosition.x));
  62. doms.box.style.transform = `translateX(${x}px)`
  63. }
  64. function onMoueseUp() {
  65. // 判断当前页数
  66. if (Math.round(-1 * x / 300) < 0) {
  67. currentIndex = 2
  68. } else if (Math.round(-1 * x / 300) > 2) {
  69. currentIndex = 0
  70. } else {
  71. currentIndex = Math.round(-1 * x / 300)
  72. }
  73. transformX = currentIndex * -300
  74. moved(currentIndex)
  75. // 移出监听
  76. doms.box.removeEventListener('mousemove', onMoueseMove)
  77. }
  78. function onMoueseLeave() {
  79. // 移出监听
  80. doms.box.removeEventListener('mousemove', onMoueseMove)
  81. // 添加自动轮播
  82. timer = setInterval(changeNext, 2000)
  83. }
  84. // 鼠标按下添加拖拽
  85. doms.box.addEventListener('mousedown', onMoueseDown)
  86. // 鼠标抬起移除事件
  87. doms.box.addEventListener('mouseup', onMoueseUp)
  88. // 鼠标移出后添加自动轮播以及移出事件
  89. doms.box.addEventListener('mouseleave', onMoueseLeave)

解释截图(最左侧为可视窗口最左侧):

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号