function addEvent(obj, sEv, fn) { if (obj.addEventListener) { obj.addEventListener(sEv, fn, false); } else { obj.attachEvent('on' + sEv, fn); } } function addWheel(obj, fn) { function wheel(ev) { var oEvent = ev || event; var bDown = true; bDown = oEvent.wheelDelta ? oEvent.wheelDelta > 0 : oEvent.detail < 0; fn && fn(bDown); oEvent.preventDefault && oEvent.preventDefault(); return false; } if (window.navigator.userAgent.toLowerCase().indexOf('firefox') != -1) { obj.addEventListener('DOMMouseScroll', wheel, false); } else { addEvent(obj, 'mousewheel', wheel) } } function wheel(obj) { if (!obj) return; var oBlock = obj.siderParent, //滚动条大块 oBar = obj.siderChild, //滚动条小块 oContent = obj.conParent, //大内容 oPara = obj.conChild; //小内容 //小条的大小根据内容大小走 oBar.css({ height: (oContent.outerHeight() / oPara.outerHeight()) * oBlock.outerHeight() }); function setTop(t) { if (t < 0) { t = 0; } else if (t > oBlock.outerHeight() - oBar.outerHeight()) { t = oBlock.outerHeight() - oBar.outerHeight(); } var addWheelScale = t / (oBlock.outerHeight() - oBar.outerHeight()); oBar.css('top', t); oPara.css('top', addWheelScale * (oContent.outerHeight() - oPara.outerHeight())); }; oBar.on('mousedown', function (ev) { var disY = ev.clientY - oBar.position().top; $(document).on({ 'mousemove': function (ev) { var t = ev.clientY - disY; setTop(t); }, 'mouseup': function () { $(document).off('mousemove'); $(document).off('mouseup'); oBar[0].releaseCapture && oBar[0].releaseCapture(); }, }); oBar[0].setCapture && oBar[0].setCapture(); return false; }); $(document).on('keydown', function (ev) { var t = oBar.position().top; switch (ev.keyCode) { case 38: t -= 10; break; case 40: t += 10; break; } setTop(t); }); addWheel(oContent[0], function (bDown) { var t = oBar.position().top; if (bDown) { t -= 10; } else { t += 10; } setTop(t); }); }