Comparing version 1.0.0 to 2.0.0
{ | ||
"name": "zenscroll", | ||
"version": "1.0.0", | ||
"description": "A module to smooth-scroll web pages and container elements", | ||
"version": "2.0.0", | ||
"description": "A module to smooth-scroll web pages and inside elements", | ||
"main": "zenscroll.js", | ||
@@ -6,0 +6,0 @@ "files": ["zenscroll.js", "zenscroll-min.js"], |
@@ -13,7 +13,8 @@ <p align="center"> | ||
Smooth animated scrolling. No more abrupt jumps on your page. | ||
Move elements into view, center them, or scroll to any Y position. | ||
Smooth animated scrolling. No more abrupt jumps. | ||
Move elements into view, to the center, or scroll to any vertical position. | ||
870 bytes of pure JavaScript. No dependencies. | ||
*864 bytes of pure JavaScript. No dependencies.* | ||
## About | ||
@@ -31,4 +32,4 @@ | ||
- Specify the spacing between the element and the edge of the screen (required for fixed navigation bars and footers). | ||
- Only 864 bytes minimized & gzipped. | ||
- No dependencies. | ||
- Small size: 870 bytes (minimized & gzipped). | ||
@@ -45,7 +46,7 @@ Full support tested and works under: | ||
- Opera 10.6+ (probably earlier too) | ||
- Safari 4+ (including Safari 9 on OS X 10.11 beta) | ||
- Safari 4+ | ||
- Windows Phone 8.1 | ||
- Yandex 14.12 | ||
Basic support (animated scroll in document) tested and works under: | ||
Limited support (programmatic animated scroll in document) tested and works under: | ||
@@ -66,3 +67,3 @@ - Firefox 3+ | ||
You can also use npm to install Zenscroll: | ||
You can also use npm to get Zenscroll: | ||
@@ -75,2 +76,3 @@ ```` | ||
### 1. Smooth scroll within your page | ||
@@ -84,9 +86,3 @@ | ||
You can also disable this automatic smoothing for all internal links by executing: | ||
````js | ||
zenscroll.setup(null, null, true) | ||
```` | ||
### 2. Scroll to the top of an element | ||
@@ -101,8 +97,10 @@ | ||
### 3. Scroll to a specific vertical position | ||
````js | ||
zenscroll.toY(123) | ||
zenscroll.toY(50) | ||
```` | ||
### 4. Scroll an element into view | ||
@@ -142,5 +140,6 @@ | ||
### 6. Set the duration of the scroll | ||
The default duration is 999 which is ~1 second. The duration is automatically reduced for elements that are very close. You can specifically set the duration via an optional second parameter. (Note that a value of zero for duration is ignored.) | ||
The default duration is 999 which is ~1 second. The duration is automatically reduced for elements that are very close. You can specifically set the duration for each scroll method via an optional second parameter. (Note that a value of zero for duration is ignored.) | ||
@@ -150,3 +149,3 @@ Examples: | ||
````js | ||
zenscroll.toY(70, 100) // 100ms == 0.1 second | ||
zenscroll.toY(50, 100) // 100ms == 0.1 second | ||
```` | ||
@@ -162,2 +161,3 @@ | ||
### 7. Scroll inside a scrollable DIV | ||
@@ -184,3 +184,3 @@ | ||
var edgeOffset = 4 | ||
var cScroll = zenscroll.newFor(c, defaultDuration, edgeOffset) | ||
var cScroll = new Zenscroll(c, defaultDuration, edgeOffset) | ||
var target = document.getElementById("item4") | ||
@@ -201,2 +201,3 @@ cScroll.center(target) | ||
### 8. Change settings | ||
@@ -207,10 +208,8 @@ | ||
- You can set the default value for duration. This will be valid for internal scrolls and all your direct scroll calls where you don’t specify a duration. | ||
- Change the edge offset (the spacing between the element and the screen edge). This is very useful if you have a fixed navigation bar or footer bar: set it to at least the height of your fixed bar. | ||
- You can also turn on or off the automatic smoothing for internal links. | ||
- Change the edge offset (the spacing between the element and the screen edge). If you have a fixed navigation bar or footer bar then set the offset to their height. | ||
````js | ||
var defaultDuration = 777 | ||
var edgeOffset = 42 | ||
var disableInternalLinkSmoothing = false | ||
zenscroll.setup(defaultDuration, edgeOffset, disableInternalLinkSmoothing) | ||
var defaultDuration = 777 // ms | ||
var edgeOffset = 42 // px | ||
zenscroll.setup(defaultDuration, edgeOffset) | ||
```` | ||
@@ -221,3 +220,3 @@ | ||
````js | ||
zenscroll.setup(500) | ||
zenscroll.setup(777) | ||
```` | ||
@@ -228,12 +227,6 @@ | ||
````js | ||
zenscroll.setup(null, 5) | ||
zenscroll.setup(null, 42) | ||
```` | ||
Disable the automatic smoothing for internal links, while leaving both other settings unchanged: | ||
````js | ||
zenscroll.setup(null, null, true) | ||
```` | ||
### 9. Controlling the smooth operation | ||
@@ -260,3 +253,3 @@ | ||
- [Zenfonts](https://github.com/zengabor/zenfonts), a tiny JavaScript helper for @font-face loading | ||
- [Zenvite.com](http://zenvite.com/): Create beautiful invitation pages & get everybody on the same page | ||
- [Zenfonts](https://github.com/zengabor/zenfonts), a tiny JavaScript helper for @font-face web font loading. | ||
- [Zenvite.com](http://zenvite.com/): Create invitation pages & get everybody on the same page. |
@@ -1,1 +0,1 @@ | ||
!function(t,e){var n=function i(n,o,c){"use strict";o=o||999,c&&0===c||(c=9);var r,a=e.documentElement,l=function(){return n?n.scrollTop:t.scrollY||a.scrollTop},u=function(){return n?Math.min(n.offsetHeight,t.innerHeight):t.innerHeight||a.clientHeight},f=function(t){return t.offsetTop-(n||a).offsetTop},s=function M(){clearTimeout(r),r=0},h=function(e,i){s();var c=l(),f=Math.max(e,0)-c;i=i||Math.min(Math.abs(f),o);var h=(new Date).getTime();!function g(){r=setTimeout(function(){var e=Math.min(((new Date).getTime()-h)/i,1),o=Math.max(Math.floor(c+f*(.5>e?2*e*e:e*(4-2*e)-1)),0);n?n.scrollTop=o:t.scrollTo(0,o),1>e&&u()+o<(n||a).scrollHeight?g():setTimeout(s,99)},9)}()},g=function H(t,e){h(f(t)-c,e)},m=function k(t,e){var n=t.getBoundingClientRect().height+2*c,i=u(),o=f(t),r=o+n,a=l();c>o-a||n>i?g(t,e):c>a+i-r&&h(r-i,e)},v=function w(t,e,n){h(Math.max(f(t)-u()/2+(n||t.getBoundingClientRect().height/2),0),e)},d=function x(e){t.history.replaceState&&history.replaceState({},"",t.location.href.split("#")[0]+"#"+e)},p=function D(e){var n=e.target,i=n.getAttribute("href")||"";if("A"===n.tagName&&0===i.indexOf("#"))if("#"===i)e.preventDefault(),t.zenscroll.toY(0),d("");else{var o=n.hash.substring(1),c=document.getElementById(o);c&&(e.preventDefault(),t.zenscroll.to(c),d(o))}},T;n||("addEventListener"in t?(t.addEventListener("click",p,!1),T=function(){t.removeEventListener("click",p,!1)}):t.attachEvent&&(t.attachEvent("onclick",p),T=function(){t.detachEvent("onclick",p)}));var E=function b(t,e,n){t&&(o=t),null!==e&&(c=e),n&&T&&(T(),T=null)};return{newFor:i,setup:E,to:g,toY:h,intoView:m,center:v,stop:s,moving:function(){return!!r}}};t.zenscroll=n()}(this,document); | ||
!function(t,e){"use strict";t.Zenscroll=function n(o,i,r){i=i||999,r&&0===r||(r=9);var c,l=e.documentElement,a=function(){return o?o.scrollTop:t.scrollY||l.scrollTop},u=function(){return o?Math.min(o.offsetHeight,t.innerHeight):t.innerHeight||l.clientHeight},f=function(t){return o?t.offsetTop-o.offsetTop:t.getBoundingClientRect().top+a()-l.offsetTop},s=function M(){clearTimeout(c),c=0},h=function(e,n){s();var r=a(),f=Math.max(e,0)-r;n=n||Math.min(Math.abs(f),i);var h=(new Date).getTime();!function g(){c=setTimeout(function(){var e=Math.min(((new Date).getTime()-h)/n,1),i=Math.max(Math.floor(r+f*(.5>e?2*e*e:e*(4-2*e)-1)),0);o?o.scrollTop=i:t.scrollTo(0,i),1>e&&u()+i<(o||l).scrollHeight?g():setTimeout(s,99)},9)}()},g=function H(t,e){h(f(t)-r,e)},m=function w(t,e){var n=t.getBoundingClientRect().height+2*r,o=u(),i=f(t),c=i+n,l=a();r>i-l||n>o?g(t,e):r>l+o-c&&h(c-o,e)},p=function x(t,e,n){h(Math.max(f(t)-u()/2+(n||t.getBoundingClientRect().height/2),0),e)},v=function B(e){try{history.replaceState({},"",t.location.href.split("#")[0]+(e?"#"+e:""))}catch(n){}},d=function D(e){for(var n=e.target;n&&"A"!==n.tagName;)n=n.parentNode;if(n){var o=n.getAttribute("href")||"";if(0===o.indexOf("#"))if("#"===o)e.preventDefault(),t.zenscroll.toY(0),v("");else{var i=n.hash.substring(1),r=document.getElementById(i);r&&(e.preventDefault(),t.zenscroll.to(r),v(i))}}};!o&&"addEventListener"in t&&t.addEventListener("click",d,!1);var T=function E(t,e){t&&(i=t),null!==e&&(r=e)};return{setup:T,to:g,toY:h,intoView:m,center:p,stop:s,moving:function(){return!!c}}},t.zenscroll=new t.Zenscroll}(this,document); |
/** | ||
* Zenscroll 1.0.0 | ||
* Zenscroll 2.0.0 | ||
* https://github.com/zengabor/zenscroll/ | ||
* | ||
* Copyright 2015 Gabor Lenard | ||
* Copyright 2015–2016 Gabor Lenard | ||
* | ||
@@ -38,7 +38,6 @@ * This is free and unencumbered software released into the public domain. | ||
(function (win, doc) { | ||
"use strict" | ||
win.Zenscroll = function Zenscroll(scrollContainer, defaultDuration, edgeOffset) { | ||
var makeScroller = function makeScroller(scrollContainer, defaultDuration, edgeOffset) { | ||
"use strict" | ||
defaultDuration = defaultDuration || 999 //ms | ||
@@ -63,6 +62,10 @@ if (!edgeOffset || edgeOffset !== 0) { | ||
var getRelativeTopOf = function (elem) { | ||
return elem.offsetTop - (scrollContainer || docElem).offsetTop | ||
var getRelativeTopOf = function (elem) { | ||
if (scrollContainer) { | ||
return elem.offsetTop - scrollContainer.offsetTop | ||
} else { | ||
return elem.getBoundingClientRect().top + getScrollTop() - docElem.offsetTop | ||
} | ||
} | ||
/** | ||
@@ -158,6 +161,8 @@ * Immediately stops the current smooth scroll operation | ||
} | ||
var replaceUrl = function replaceUrl(hash) { | ||
if (win.history.replaceState) { | ||
history.replaceState({}, "", win.location.href.split("#")[0] + "#" + hash) | ||
try { | ||
history.replaceState({}, "", win.location.href.split("#")[0] + (hash ? "#" + hash : "")) | ||
} catch (e) { | ||
// To avoid the Security exception in Chrome when the page was opened via the file protocol, e.g., file://index.html | ||
} | ||
@@ -167,15 +172,20 @@ } | ||
var anchor = event.target | ||
var href = anchor.getAttribute("href") || "" | ||
if (anchor.tagName === "A" && href.indexOf("#") === 0) { | ||
if (href === "#") { | ||
event.preventDefault() | ||
win.zenscroll.toY(0) | ||
replaceUrl("") | ||
} else { | ||
var targetId = anchor.hash.substring(1) | ||
var targetElem = document.getElementById(targetId) | ||
if (targetElem) { | ||
while (anchor && anchor.tagName !== "A") { | ||
anchor = anchor.parentNode | ||
} | ||
if (anchor) { | ||
var href = anchor.getAttribute("href") || "" | ||
if (href.indexOf("#") === 0) { | ||
if (href === "#") { | ||
event.preventDefault() | ||
win.zenscroll.to(targetElem) | ||
replaceUrl(targetId) | ||
win.zenscroll.toY(0) | ||
replaceUrl("") | ||
} else { | ||
var targetId = anchor.hash.substring(1) | ||
var targetElem = document.getElementById(targetId) | ||
if (targetElem) { | ||
event.preventDefault() | ||
win.zenscroll.to(targetElem) | ||
replaceUrl(targetId) | ||
} | ||
} | ||
@@ -185,17 +195,18 @@ } | ||
} | ||
var removeEventListener | ||
if (!scrollContainer) { | ||
if ("addEventListener" in win) { | ||
win.addEventListener("click", internalLinkHandler, false) | ||
removeEventListener = function () { win.removeEventListener("click", internalLinkHandler, false) } | ||
} else if (win.attachEvent) { | ||
win.attachEvent("onclick", internalLinkHandler) | ||
removeEventListener = function () { win.detachEvent("onclick", internalLinkHandler) } | ||
} | ||
// create listeners for the documentElement only & exclude IE8- | ||
if (!scrollContainer && "addEventListener" in win) { | ||
win.addEventListener("click", internalLinkHandler, false) | ||
} | ||
var setup = function setup(newDuration, newEdgeOffset, disableInternalLinks) { | ||
if (newDuration) { | ||
defaultDuration = newDuration | ||
/** | ||
* Changes default settings for this scroller. | ||
* | ||
* @param {newDefaultDuration} New value for default duration, used for each scroll method by default. | ||
* Ignored if 0 or falsy. | ||
* @param {newEdgeOffset} New value for the edge offset, used by each scroll method by default. | ||
*/ | ||
var setup = function setup(newDefaultDuration, newEdgeOffset) { | ||
if (newDefaultDuration) { | ||
defaultDuration = newDefaultDuration | ||
} | ||
@@ -205,10 +216,5 @@ if (newEdgeOffset !== null) { | ||
} | ||
if (disableInternalLinks && removeEventListener) { | ||
removeEventListener() | ||
removeEventListener = null | ||
} | ||
} | ||
return { | ||
newFor: makeScroller, | ||
setup: setup, | ||
@@ -226,3 +232,3 @@ to: scrollToElem, | ||
win.zenscroll = makeScroller() | ||
win.zenscroll = new win.Zenscroll() | ||
@@ -229,0 +235,0 @@ |
206
18176
240