Skip to content

Added API function to restart SmoothScroll after it was destroyed #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
62 changes: 37 additions & 25 deletions SmoothScroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ var observer;
var refreshSize;
var deltaBuffer = [];
var isMac = /^Mac/.test(navigator.platform);
var userAgent = window.navigator.userAgent;
var isEdge = /Edge/.test(userAgent); // thank you MS
var isChrome = /chrome/i.test(userAgent) && !isEdge;
var isSafari = /safari/i.test(userAgent) && !isEdge;
var isMobile = /mobile/i.test(userAgent);
var isIEWin7 = /Windows NT 6.1/i.test(userAgent) && /rv:11/i.test(userAgent);
var isEnabledForBrowser = (isChrome || isSafari || isIEWin7) && !isMobile;
var wheelEvent;

var key = { left: 37, up: 38, right: 39, down: 40, spacebar: 32,
pageup: 33, pagedown: 34, end: 35, home: 36 };
Expand Down Expand Up @@ -161,6 +169,22 @@ function init() {
}
}

/**
* Attaches event listeners
*/
function start() {
if ('onwheel' in document.createElement('div'))
wheelEvent = 'wheel';
else if ('onmousewheel' in document.createElement('div'))
wheelEvent = 'mousewheel';

if (wheelEvent && isEnabledForBrowser) {
addEvent(wheelEvent, wheel);
addEvent('mousedown', mousedown);
addEvent('load', init);
}
}

/**
* Removes event listeners and other traces left on the page.
*/
Expand Down Expand Up @@ -678,31 +702,6 @@ function pulse(x) {
}


/***********************************************
* FIRST RUN
***********************************************/

var userAgent = window.navigator.userAgent;
var isEdge = /Edge/.test(userAgent); // thank you MS
var isChrome = /chrome/i.test(userAgent) && !isEdge;
var isSafari = /safari/i.test(userAgent) && !isEdge;
var isMobile = /mobile/i.test(userAgent);
var isIEWin7 = /Windows NT 6.1/i.test(userAgent) && /rv:11/i.test(userAgent);
var isEnabledForBrowser = (isChrome || isSafari || isIEWin7) && !isMobile;

var wheelEvent;
if ('onwheel' in document.createElement('div'))
wheelEvent = 'wheel';
else if ('onmousewheel' in document.createElement('div'))
wheelEvent = 'mousewheel';

if (wheelEvent && isEnabledForBrowser) {
addEvent(wheelEvent, wheel);
addEvent('mousedown', mousedown);
addEvent('load', init);
}


/***********************************************
* PUBLIC INTERFACE
***********************************************/
Expand All @@ -713,10 +712,23 @@ function SmoothScroll(optionsToSet) {
options[key] = optionsToSet[key];
}
SmoothScroll.destroy = cleanup;
SmoothScroll.start = start;


/***********************************************
* FIRST RUN
***********************************************/

SmoothScroll.start();

if (window.SmoothScrollOptions) // async API
SmoothScroll(window.SmoothScrollOptions);


/***********************************************
* DEPENDENCY MANAGEMENT EXPORT
***********************************************/

if (typeof define === 'function' && define.amd)
define(function() {
return SmoothScroll;
Expand Down