我有下面的dragscroll代码,该代码在页面加载时自动运行。

但我想手动调用此函数,因为我正在使用DOM元素添加html。

我已经尝试使用window.factory和window.reset了,但是它对我不起作用。

我的Javascript模块文件:

   (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 EventListener = 'EventListener';
 var addEventListener = 'add'+EventListener;
 var removeEventListener = 'remove'+EventListener;
 var newScrollX, newScrollY;

 var dragged = [];
 var reset = function(i, el) {
  for (i = 0; i < dragged.length;)
   {
      <some code>
  }

  // cloning into array since HTMLCollection is updated dynamically
  dragged = [].slice.call(_document.getElementsByClassName('dragscroll'));
  for (i = 0; i < dragged.length;)
   {
     <some code>
     }, 0
    );

    _window[addEventListener](
     mouseup, cont.mu = function() {pushed = 0;}, 0
    );

    _window[addEventListener](
     mousemove,
     cont.mm = function(e) {
      if (pushed) {
       (scroller = el.scroller||el).scrollLeft -=
        newScrollX = (- lastClientX + (lastClientX=e.clientX));
       scroller.scrollTop -=
        newScrollY = (- lastClientY + (lastClientY=e.clientY));
       if (el == _document.body) {
        (scroller = _document.documentElement).scrollLeft -= newScrollX;
        scroller.scrollTop -= newScrollY;
       }
      }
     }, 0
    );
    })(dragged[i++]);
  }
 }


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

 exports.reset = reset;
}));
   })

我想手动调用以上函数。

最佳答案

你可以试试吗



这将手动调用您的重置功能。

让我知道是否有帮助。

09-25 18:42