//改写自https://github.com/asvd/dragscroll //使用: var myDragScroll = dragScroll.create({});myDragScroll.reset(); //选项:container--.dragscroll元素容器 const dragScroll = (function ($) { function seal(options) { var newScrollX, newScrollY; options = $.extend({ dragscroll: '.dragscroll', dragscrollno: '.dragscroll-no', xscroll: true, yscroll: true, throttle: null }, options); !options.dragscroll && (options.dragscroll = '.dragscroll'); function _dragScroll(el) { var lastClientX, lastClientY, pushed = 0; function __mousedown(e) { if (!$(e.target).parentsUntil(options.dragscroll, options.dragscrollno).length) { pushed = 1; lastClientX = e.clientX; lastClientY = e.clientY; } } function __mouseup(e) { pushed = 0; } function __mousemove(e) { if (pushed) { var scroller = el.scroller || el; if (options.xscroll) { scroller.scrollLeft -= newScrollX = - lastClientX + (lastClientX = e.clientX); } if (options.yscroll) { scroller.scrollTop -= newScrollY = - lastClientY + (lastClientY = e.clientY); } if (el === document.body) { scroller = document.documentElement; options.xscroll && (scroller.scrollLeft -= newScrollX); options.yscroll && (scroller.scrollTop -= newScrollY); } } } const _throttleMove = options.throttle ? options.throttle(__mousemove, 100) : __mousemove; $(el).off('mousedown', __mousedown).off('mouseup', __mouseup).off('mousemove', _throttleMove) .on('mousedown', __mousedown).on('mouseup', __mouseup).on('mousemove', _throttleMove) .on('mouseleave', function () { pushed = 0; }) .on('selectstart', function (e) { return !pushed; }); } this.options = options; this.reset = function (options) { options && Object.assign(this.options, options); $(this.options.dragscroll, this.options.container).each(function () { _dragScroll(this); }); }; this.getOptions = function () { return Object.assign({}, this.options); }; } return { create(options) { return new seal(options); } }; }(jQuery));