不知道为什么使用链式调用就报错,第33~34行
使用链式调用就报错,说 _bindContScroll 不是函数,不用的话运行一切正常。
完整代码:
(function(win, doc, $){ 'use strict'; // 构造函数 function CusScrollBar(options) { this._init(options); } $.extend(CusScrollBar.prototype, { _init : function(options) { let self = this; self.options = { scrollDir : "y", contSelector : "", barSelector : "", sliderSelector: "" }; $.extend(true, self.options, options || {}); self._initDomEvent(); return self; }, // _initDomEvent : function() { let opts = this.options; this.$cont = $(opts.contSelector); this.$slider = $(opts.sliderSelector); this.$bar = opts.barSelector ? $(opts.barSelector) : self.$slider.parent(); this.$doc = $(doc); // 就是这里报错 this._initSilderDragEvent() this._bindContScroll(); }, // _initSilderDragEvent : function() { let slider = this.$slider; let sliderEl = slider[0]; if (sliderEl) { let self = this; let doc = self.$doc; let dragStartPagePosition; let dragStartScrollPosition; let dragContBarRate; function mousemoveHandler(e) { e.preventDefault(); console.log('mousemove') if (dragStartPagePosition == null) { return; } self.scrollTo(dragStartScrollPosition + (e.pageY - dragStartPagePosition) * dragContBarRate); }; slider.on('mousedown', function(e) { e.preventDefault(); console.log('mousedown') dragStartPagePosition = e.pageY; dragStartScrollPosition = self.$cont[0].scrollTop; dragContBarRate = self.getMaxScrollPosition() / self.getMaxSliderPosition(); console.log(dragContBarRate) // 命名空间 doc.on('mousemove.nnHoney', mousemoveHandler).on('mouseup.nn', function(event) { event.preventDefault(); console.log('mouseup') // doc.off("mousemove mouseup"); doc.off(".nn"); });; }); } return self; }, getMaxScrollPosition : function() { let self = this; // 内容可以滚动的高度 // self.$cont.height() 内容可视区域的高度(文章未充满页面的情况) // self.$cont[0].scrollHeight 内容完整高度(文章超出页面包括隐藏部分) return Math.max(self.$cont.height(), self.$cont[0].scrollHeight) - self.$cont.height(); }, getMaxSliderPosition : function() { let self = this; return self.$bar.height() - self.$slider.height(); }, scrollTo : function(postionVal) { let self = this; self.$cont.scrollTop(postionVal) }, _bindContScroll : function() { let self = this; self.$cont.on("scroll", function(e) { e.preventDefault(); let sliderEl = self.$slider && self.$slider[0]; if (sliderEl) { sliderEl.style.top = self.getSliderPosition() + 'px'; } }); return self; }, getSliderPosition : function() { let self = this; let maxSliderPosition = self.getMaxSliderPosition(); return Math.min(maxSliderPosition, maxSliderPosition * self.$cont[0].scrollTop / self.getMaxScrollPosition()); } }); win.CusScrollBar = CusScrollBar; })(window, document, jQuery);