27 - Click and Drag
筆記
作法:
1.監聽滑鼠點擊取起始位置1
2
3
4
5
6
7
8
9slider.addEventListener('mousedown',(e)=>{
isMouseDown = true;
slider.classList.add('active');
startX = e.pageX - slider.offsetLeft;
// 起始滾動到的位置
scrollLeft = slider.scrollLeft;
console.log(startX,scrollLeft)
})
2.監聽滑鼠移動
1 | slider.addEventListener('mousemove',(e)=>{ |
3.監聽滑鼠移開和點擊完成1
2
3
4
5
6
7
8slider.addEventListener('mouseup',()=>{
isMouseDown = false;
slider.classList.remove('active');
})
slider.addEventListener('mouseleave',()=>{
isMouseDown = false;
slider.classList.remove('active');
})