适用于内容超出展示范围,出现滚动条,但是不拖动滚动条而是拖内容让其移动的场景。利用了dom的scrollTo方法
dom
1 2 3 4 5 6 7 8
| <div class="yule-wrapper" v-move> <img src="@/assets/partnerHome/yule1.png" class="yule-item" alt=""> <img src="@/assets/partnerHome/yule2.png" class="yule-item" alt=""> <img src="@/assets/partnerHome/yule3.png" class="yule-item" alt=""> <img src="@/assets/partnerHome/yule4.png" class="yule-item" alt=""> <img src="@/assets/partnerHome/yule5.png" class="yule-item" alt=""> <img src="@/assets/partnerHome/yule6.png" class="yule-item" alt=""> </div>
|
css
1 2 3 4 5 6 7 8 9 10
| .yule-wrapper { width: 1200px; overflow-x: auto; margin: 22px auto; display: flex; .yule-item { width: 321px; height: 324px; } }
|
js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| export default{ directives: { move: { mounted: function (el) { el.onmousedown = function (e) { const disX = e.clientX const disY = e.clientY const originalScrollLeft = el.scrollLeft const originalScrollTop = el.scrollTop const originalScrollBehavior = el.style['scroll-behavior'] const originalPointerEvents = el.style['pointer-events'] el.style['scroll-behavior'] = 'auto' document.onmousemove = function (ev) { ev.preventDefault() const distanceX = ev.clientX - disX const distanceY = ev.clientY - disY el.scrollTo(originalScrollLeft - distanceX, originalScrollTop - distanceY) el.style['pointer-events'] = 'none' } document.onmouseup = function () { document.onmousemove = null document.onmouseup = null el.style['scroll-behavior'] = originalScrollBehavior el.style['pointer-events'] = originalPointerEvents } } } } } }
|