Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions src/jquery.smoothwheel.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,26 @@
*/
(function ($) {

var self = this, container, running=false, currentY = 0, targetY = 0, oldY = 0, maxScrollTop= 0, minScrollTop, direction, onRenderCallback=null,
fricton = 0.95, // higher value for slower deceleration
var self = this, container, running=false, currentY = 0, targetY = 0, oldY = 0, maxScrollTop= 0, minScrollTop, direction,
vy = 0,
stepAmt = 1,
minMovement= 0.1,
ts=0.1;
var settings = {
friction: 0.95, // higher value for slower deceleration
stepAmt: 1,
minMovement: 0.1,
onRender: null,
};

var updateScrollTarget = function (amt) {
targetY += amt;
vy += (targetY - oldY) * stepAmt;
vy += (targetY - oldY) * settings.stepAmt;

oldY = targetY;


}
var render = function () {
if (vy < -(minMovement) || vy > minMovement) {
if (vy < -(settings.minMovement) || vy > settings.minMovement) {

currentY = (currentY + vy);
if (currentY > maxScrollTop) {
Expand All @@ -33,17 +36,17 @@
vy = 0;
currentY = minScrollTop;
}

container.scrollTop(-currentY);

vy *= fricton;
vy *= settings.friction;

// vy += ts * (currentY-targetY);
// scrollTopTweened += settings.tweenSpeed * (scrollTop - scrollTopTweened);
// currentY += ts * (targetY - currentY);

if(onRenderCallback){
onRenderCallback();
if(settings.onRender){
settings.onRender();
}
}
}
Expand All @@ -56,7 +59,7 @@
var onWheel = function (e) {
e.preventDefault();
var evt = e.originalEvent;

var delta = evt.detail ? evt.detail * -1 : evt.wheelDelta / 40;
var dir = delta < 0 ? -1 : 1;
if (dir != direction) {
Expand All @@ -66,8 +69,12 @@

//reset currentY in case non-wheel scroll has occurred (scrollbar drag, etc.)
currentY = -container.scrollTop();

updateScrollTarget(delta);

//add an additional height declaration to allow for elements that
//change height after initial page load.
minScrollTop = container.get(0).clientHeight - container.get(0).scrollHeight;
}

/*
Expand All @@ -81,9 +88,9 @@
window.msRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 60);
};
};


})();

/*
Expand Down Expand Up @@ -120,6 +127,7 @@
$.fn.smoothWheel = function () {
// var args = [].splice.call(arguments, 0);
var options = jQuery.extend({}, arguments[0]);
settings = jQuery.extend(settings,options);
return this.each(function (index, elm) {

if(!('ontouchstart' in window)){
Expand All @@ -130,11 +138,8 @@
//set target/old/current Y to match current scroll position to prevent jump to top of container
targetY = oldY = container.get(0).scrollTop;
currentY = -targetY;

minScrollTop = container.get(0).clientHeight - container.get(0).scrollHeight;
if(options.onRender){
onRenderCallback = options.onRender;
}
if(options.remove){
log("122","smoothWheel","remove", "");
running=false;
Expand Down