gpt4 book ai didi

javascript - 使用鼠标和触摸水平滚动 div

转载 作者:行者123 更新时间:2023-11-28 16:11:00 24 4
gpt4 key购买 nike

假设我有一个 white-space: nowrap; 带有 overflow: hidden; 的 div。它的内容当然比 div 长得多,需要滚动才能显示出来。

我正在使用 this库,但它不适用于具有触摸输入的移动设备。您知道实现此功能的任何替代方法或方法吗?

最佳答案

我的愿望终于实现了。这里我修改了 dragscroll.js 库以启用触摸支持。

/* Modified dragscroll.js by Undust4able */

(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['exports'], factory);
} else if (typeof exports !== 'undefined') {
factory(exports);
} else {
factory((root.dragscroll = {}));
}
}(this, function (exports) {
var _window = window;
var _document = document;

var mousemove = 'mousemove';
var mouseup = 'mouseup';
var mousedown = 'mousedown';

var touchmove = 'touchmove';
var touchup = 'touchend';
var touchdown = 'touchstart';

var EventListener = 'EventListener';
var addEventListener = 'add'+EventListener;
var removeEventListener = 'remove'+EventListener;

var dragged = [];
var reset = function(i, el) {
for (i = 0; i < dragged.length;) {
el = dragged[i++];
el = el.container || el;
el[removeEventListener](mousedown, el.md, 0);
_window[removeEventListener](mouseup, el.mu, 0);
_window[removeEventListener](mousemove, el.mm, 0);

el[removeEventListener](touchdown, el.td, 0);
_window[removeEventListener](touchup, el.tu, 0);
_window[removeEventListener](touchmove, el.tm, 0);
}

// cloning into array since HTMLCollection is updated dynamically
dragged = [].slice.call(_document.getElementsByClassName('dragscroll'));
for (i = 0; i < dragged.length;) {
(function(el, lastClientX, lastClientY, pushed, scroller, cont){
(cont = el.container || el)[addEventListener](
mousedown,
cont.md = function(e) {
if (!el.hasAttribute('nochilddrag') ||
_document.elementFromPoint(
e.pageX, e.pageY
) == cont
) {
pushed = 1;
lastClientX = e.clientX;
lastClientY = e.clientY;

e.preventDefault();
}
}, 0
);
(cont = el.container || el)[addEventListener](
touchdown,
cont.td = function(e) {
if (!el.hasAttribute('nochilddrag') ||
_document.elementFromPoint(
e.pageX, e.pageY
) == cont
) {
pushed = 1;
e.preventDefault();

e = e.targetTouches[0];
lastClientX = e.clientX;
lastClientY = e.clientY;

}
}, 0
);
_window[addEventListener](
mouseup, cont.mu = function() {pushed = 0;}, 0
);
_window[addEventListener](
touchup, cont.tu = function() {pushed = 0;}, 0
);
_window[addEventListener](
mousemove,
cont.mm = function(e) {
if (pushed) {
(scroller = el.scroller||el).scrollLeft -=
(- lastClientX + (lastClientX=e.clientX));
scroller.scrollTop -=
(- lastClientY + (lastClientY=e.clientY));
}
}, 0
);
_window[addEventListener](
touchmove,
cont.tm = function(e) {
if (pushed) {
e = e.targetTouches[0];
(scroller = el.scroller||el).scrollLeft -=
(- lastClientX + (lastClientX=e.clientX));
scroller.scrollTop -=
(- lastClientY + (lastClientY=e.clientY));
}

}, 0
);
})(dragged[i++]);
}
}


if (_document.readyState == 'complete') {
reset();
} else {
_window[addEventListener]('load', reset, 0);
}

exports.reset = reset;
}));

关于javascript - 使用鼠标和触摸水平滚动 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38337611/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com