废话不多说,直接上代码(注:js部分代码会有截图对其解释)
一、HTML
| <div class="bigBox"> |
| |
| <div class="box"> |
| <div class="littleBox">1</div> |
| <div class="littleBox">2</div> |
| <div class="littleBox">3</div> |
| </div> |
| |
| <div class="point"> |
| <div class="active"></div> |
| <div></div> |
| <div></div> |
| </div> |
| |
| <div class="prev"></div> |
| |
| <div class="next"></div> |
| </div> |
复制
二、CSS
| * { |
| margin: 0; |
| padding: 0; |
| } |
| |
| .bigBox { |
| width: 300px; |
| height: 180px; |
| border: 2px solid black; |
| overflow: hidden; |
| margin: 100px auto; |
| position: relative; |
| } |
| |
| |
| .box { |
| width: 900px; |
| display: flex; |
| transition: all .6s; |
| } |
| |
| .littleBox { |
| text-align: center; |
| line-height: 180px; |
| width: 300px; |
| height: 180px; |
| } |
| |
| .littleBox:nth-of-type(1) { |
| background-color: skyblue; |
| } |
| |
| .littleBox:nth-of-type(2) { |
| background-color: pink; |
| } |
| |
| .littleBox:nth-of-type(3) { |
| background-color: yellow; |
| } |
| |
| |
| .point { |
| position: absolute; |
| width: 50px; |
| height: 10px; |
| bottom: 10px; |
| left: 50%; |
| transform: translateX(-50%); |
| display: flex; |
| justify-content: space-evenly; |
| cursor: pointer; |
| } |
| |
| .point div { |
| width: 10px; |
| height: 10px; |
| background-color: rgb(151, 151, 151); |
| border-radius: 10px; |
| } |
| |
| .point .active { |
| background-color: white; |
| } |
| |
| |
| .prev::after, |
| .next::after { |
| position: absolute; |
| width: 20px; |
| height: 30px; |
| top: 50%; |
| transform: translateY(-50%); |
| background-color: aliceblue; |
| line-height: 30px; |
| text-align: center; |
| cursor: pointer; |
| } |
| |
| |
| .prev::after { |
| content: '<'; |
| left: 0; |
| border-top-right-radius: 15px; |
| border-bottom-right-radius: 15px; |
| } |
| |
| |
| .next::after { |
| content: '>'; |
| right: 0; |
| border-top-left-radius: 15px; |
| border-bottom-left-radius: 15px; |
| } |
复制
三、JS以及解释截图
| |
| const doms = { |
| box: document.querySelector('.box'), |
| point: document.querySelectorAll('.point div'), |
| prev: document.querySelector('.prev'), |
| next: document.querySelector('.next') |
| } |
| |
| |
| let currentIndex = 0 |
| |
| |
| function moved(index) { |
| currentIndex = index |
| const x = -300 * index |
| doms.point.forEach((item, i) => { |
| item.classList.remove('active') |
| }) |
| doms.point[index].classList.add('active') |
| doms.box.style.transform = `translateX(${x}px)` |
| } |
| |
| |
| function changePrev() { |
| currentIndex === 0 ? currentIndex = 2 : currentIndex-- |
| moved(currentIndex) |
| } |
| doms.prev.addEventListener('click', changePrev) |
| |
| |
| function changeNext() { |
| currentIndex === 2 ? currentIndex = 0 : currentIndex++ |
| moved(currentIndex) |
| } |
| doms.next.addEventListener('click', changeNext) |
| |
| |
| doms.point.forEach((item, i) => { |
| item.addEventListener('click', function () { |
| moved(i) |
| }) |
| }) |
| |
| |
| let timer = setInterval(changeNext, 2000) |
| |
| |
| doms.box.addEventListener('mouseenter', function () { |
| clearInterval(timer) |
| }) |
| |
| |
| const boxPosition = { |
| x: Math.floor(doms.box.getBoundingClientRect().x), |
| y: Math.floor(doms.box.getBoundingClientRect().y) |
| } |
| |
| |
| let mouseX = 0 |
| |
| |
| let transformX = 0 |
| let x = 0 |
| |
| |
| function onMoueseDown(e) { |
| mouseX = e.clientX - boxPosition.x |
| doms.box.addEventListener('mousemove', onMoueseMove) |
| } |
| |
| function onMoueseMove(e) { |
| e.preventDefault() |
| x = transformX + -1 * (mouseX - (e.clientX - boxPosition.x)); |
| doms.box.style.transform = `translateX(${x}px)` |
| } |
| |
| function onMoueseUp() { |
| |
| if (Math.round(-1 * x / 300) < 0) { |
| currentIndex = 2 |
| } else if (Math.round(-1 * x / 300) > 2) { |
| currentIndex = 0 |
| } else { |
| currentIndex = Math.round(-1 * x / 300) |
| } |
| transformX = currentIndex * -300 |
| moved(currentIndex) |
| |
| doms.box.removeEventListener('mousemove', onMoueseMove) |
| } |
| |
| |
| function onMoueseLeave() { |
| |
| doms.box.removeEventListener('mousemove', onMoueseMove) |
| |
| timer = setInterval(changeNext, 2000) |
| } |
| |
| doms.box.addEventListener('mousedown', onMoueseDown) |
| |
| |
| doms.box.addEventListener('mouseup', onMoueseUp) |
| |
| doms.box.addEventListener('mouseleave', onMoueseLeave) |
复制
解释截图(最左侧为可视窗口最左侧):
