scroll
Advanced tools
Comparing version 2.0.1 to 2.0.2
85
index.js
var raf = require('rafl') | ||
var E_NOSCROLL = new Error('Element already at target scroll position') | ||
var E_CANCELLED = new Error('Scroll cancelled') | ||
var min = Math.min | ||
function scroll (prop, element, to, options, callback) { | ||
var start = +new Date | ||
var from = element[prop] | ||
var cancelled = false | ||
module.exports = { | ||
left: make('scrollLeft'), | ||
top: make('scrollTop') | ||
} | ||
var ease = inOutSine | ||
var duration = 350 | ||
function make (prop) { | ||
return function scroll (el, to, opts, cb) { | ||
if (typeof opts == 'function') cb = opts, opts = {} | ||
if (typeof cb != 'function') cb = noop | ||
if (typeof options === 'function') { | ||
callback = options | ||
} | ||
else { | ||
options = options || {} | ||
ease = options.ease || ease | ||
duration = options.duration || duration | ||
callback = callback || function () {} | ||
} | ||
var start = +new Date | ||
var from = el[prop] | ||
var ease = opts.ease || inOutSine | ||
var duration = !isNaN(opts.duration) ? +opts.duration : 350 | ||
var cancelled = false | ||
if (from === to) { | ||
return callback( | ||
new Error('Element already at target scroll position'), | ||
element[prop] | ||
) | ||
} | ||
return from === to ? | ||
cb(E_NOSCROLL, el[prop]) : | ||
raf(animate), cancel | ||
function cancel () { | ||
cancelled = true | ||
} | ||
function animate (timestamp) { | ||
if (cancelled) { | ||
return callback( | ||
new Error('Scroll cancelled'), | ||
element[prop] | ||
) | ||
function cancel () { | ||
cancelled = true | ||
} | ||
var now = +new Date | ||
var time = Math.min(1, ((now - start) / duration)) | ||
var eased = ease(time) | ||
function animate (timestamp) { | ||
if (cancelled) return cb(E_CANCELLED, el[prop]) | ||
element[prop] = (eased * (to - from)) + from | ||
var now = +new Date | ||
var time = min(1, ((now - start) / duration)) | ||
var eased = ease(time) | ||
time < 1 ? raf(animate) : raf(function () { | ||
callback(null, element[prop]) | ||
}) | ||
el[prop] = (eased * (to - from)) + from | ||
time < 1 ? raf(animate) : raf(function () { | ||
cb(null, el[prop]) | ||
}) | ||
} | ||
} | ||
raf(animate) | ||
return cancel | ||
} | ||
function inOutSine (n) { | ||
return .5 * (1 - Math.cos(Math.PI * n)) | ||
return 0.5 * (1 - Math.cos(Math.PI * n)) | ||
} | ||
module.exports = { | ||
top: function (element, to, options, callback) { | ||
return scroll('scrollTop', element, to, options, callback) | ||
}, | ||
left: function (element, to, options, callback) { | ||
return scroll('scrollLeft', element, to, options, callback) | ||
} | ||
} | ||
function noop () {} |
{ | ||
"name": "scroll", | ||
"version": "2.0.1", | ||
"author": "Michael Rhodes", | ||
"version": "2.0.2", | ||
"main": "index.js", | ||
"repository": "git@github.com:michaelrhodes/scroll", | ||
"description": "A function that animates an element’s scrollTop or scrollLeft position.", | ||
"main": "index.js", | ||
"keyword": ["scrollTop", "scrollTo", "animate"], | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "tape test.js" | ||
}, | ||
"dependencies": { | ||
@@ -12,33 +19,3 @@ "rafl": "~1.2.1" | ||
"tape": "~2.3.2" | ||
}, | ||
"scripts": { | ||
"test": "tape test/*.js" | ||
}, | ||
"testling": { | ||
"files": "test/*.js", | ||
"browsers": { | ||
"ie": [6, 7, 8, 9, 10], | ||
"chrome": [20, 25, 29], | ||
"firefox": [3, 4, 7, 19, 24], | ||
"safari": [5.1, 6], | ||
"opera": [10, 12, 15], | ||
"iphone": [6], | ||
"android": [4.2] | ||
} | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:michaelrhodes/scroll.git" | ||
}, | ||
"keywords": [ | ||
"scrollTop", | ||
"scrollTo", | ||
"animate" | ||
], | ||
"author": "Michael Rhodes", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/michaelrhodes/scroll/issues" | ||
}, | ||
"homepage": "https://github.com/michaelrhodes/scroll" | ||
} | ||
} |
@@ -7,11 +7,4 @@ # scroll | ||
| compression | size | | ||
| :--------------- | ------: | | ||
| scroll.js | 2.6 kB | | ||
| scroll.min.js | 1.47 kB | | ||
| scroll.min.js.gz | 700 B | | ||
## install | ||
## Install | ||
```sh | ||
@@ -21,3 +14,3 @@ $ npm install scroll | ||
### Usage | ||
## use | ||
@@ -33,4 +26,4 @@ ```js | ||
// Register a callback | ||
scroll.top(page, 200, function (error, scrollTop) { | ||
console.log(error) | ||
scroll.top(page, 200, function (err, scrollTop) { | ||
console.log(err) | ||
// { message: "Scroll cancelled" } or | ||
@@ -42,3 +35,3 @@ // { message: "Element already at target scroll position" } or | ||
// => The new scrollTop position of the element | ||
// This is always returned, even when there’s an `error`. | ||
// This is always returned, even when there’s an `err`. | ||
}) | ||
@@ -50,9 +43,9 @@ | ||
// Specify a duration in milliseconds (default: 350) and register a callback. | ||
scroll.left(page, 200, { duration: 1000 }, function (error, scrollLeft) { | ||
scroll.left(page, 200, { duration: 1000 }, function (err, scrollLeft) { | ||
}) | ||
// Cancel a scroll animation | ||
// Cancel a scroll animation | ||
var options = { duration: 1000 } | ||
var cancel = scroll.top(page, 200, options, function (error, scrollTop) { | ||
console.log(error.message) | ||
var cancel = scroll.top(page, 200, options, function (err, scrollTop) { | ||
console.log(err.message) | ||
// => Scroll cancelled | ||
@@ -68,4 +61,11 @@ | ||
### License | ||
| compression | size | | ||
| :--------------- | ------: | | ||
| scroll.js | 2.86 kB | | ||
| scroll.min.js | 1.71 kB | | ||
| scroll.min.js.gz | 764 B | | ||
## obey | ||
[MIT](http://opensource.org/licenses/MIT) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
5152
4
102
1