From e2da06d765540fcd36b20a68eea153aab6b5abbb Mon Sep 17 00:00:00 2001 From: AlexXsWx Date: Sat, 18 Jun 2016 12:40:28 +0200 Subject: [PATCH] Added API function to restart SmoothScroll after it was destroyed --- SmoothScroll.js | 62 +++++++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/SmoothScroll.js b/SmoothScroll.js index bd7ba2a..de2c975 100644 --- a/SmoothScroll.js +++ b/SmoothScroll.js @@ -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 }; @@ -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. */ @@ -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 ***********************************************/ @@ -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;