├── .gitignore ├── LICENSE ├── index.js ├── package.json ├── readme.md └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | example.js 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2014 Michael Rhodes 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var E_NOSCROLL = new Error('Element already at target scroll position') 2 | var E_CANCELLED = new Error('Scroll cancelled') 3 | var min = Math.min 4 | var ms = Date.now 5 | 6 | module.exports = { 7 | left: make('scrollLeft'), 8 | top: make('scrollTop') 9 | } 10 | 11 | function make (prop) { 12 | return function scroll (el, to, opts, cb) { 13 | opts = opts || {} 14 | 15 | if (typeof opts == 'function') cb = opts, opts = {} 16 | if (typeof cb != 'function') cb = noop 17 | 18 | var start = ms() 19 | var from = el[prop] 20 | var ease = opts.ease || inOutSine 21 | var duration = !isNaN(opts.duration) ? +opts.duration : 350 22 | var cancelled = false 23 | 24 | return from === to ? 25 | cb(E_NOSCROLL, el[prop]) : 26 | requestAnimationFrame(animate), cancel 27 | 28 | function cancel () { 29 | cancelled = true 30 | } 31 | 32 | function animate (timestamp) { 33 | if (cancelled) return cb(E_CANCELLED, el[prop]) 34 | 35 | var now = ms() 36 | var time = min(1, ((now - start) / duration)) 37 | var eased = ease(time) 38 | 39 | el[prop] = (eased * (to - from)) + from 40 | 41 | time < 1 ? 42 | requestAnimationFrame(animate) : 43 | requestAnimationFrame(function () { 44 | cb(null, el[prop]) 45 | }) 46 | } 47 | } 48 | } 49 | 50 | function inOutSine (n) { 51 | return 0.5 * (1 - Math.cos(Math.PI * n)) 52 | } 53 | 54 | function noop () {} 55 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "scroll", 3 | "author": "Michael Rhodes", 4 | "version": "3.0.1", 5 | "main": "index.js", 6 | "repository": "git@github.com:michaelrhodes/scroll", 7 | "description": "animates the scroll top/left position of an element", 8 | "keyword": [ 9 | "scrollTop", 10 | "scrollLeft", 11 | "scrollTo", 12 | "animate" 13 | ], 14 | "license": "MIT", 15 | "scripts": { 16 | "test": "wzrd test.js" 17 | }, 18 | "devDependencies": { 19 | "ease-component": "~1.0.0", 20 | "tape": "~2.3.2", 21 | "wzrd": "~1.5.0" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # scroll 2 | animates the scroll top/left position of an element 3 | 4 | [![browser support](https://ci.testling.com/michaelrhodes/scroll.png)](https://ci.testling.com/michaelrhodes/scroll) 5 | 6 | note: you may need to polyfill [`requestAnimationFrame`](https://caniuse.com/#feat=requestanimationframe) in older browsers 7 | 8 | ## install 9 | ```sh 10 | npm install scroll 11 | ``` 12 | 13 | ## use 14 | ```js 15 | var scroll = require('scroll') 16 | var page = require('scroll-doc')() 17 | var ease = require('ease-component') 18 | 19 | // Basic usage 20 | scroll.left(page, 200) 21 | 22 | // Register a callback 23 | scroll.top(page, 200, function (err, scrollTop) { 24 | console.log(err) 25 | // { message: "Scroll cancelled" } or 26 | // { message: "Element already at target scroll position" } or 27 | // null 28 | 29 | console.log(scrollTop) 30 | // => The new scrollTop position of the element 31 | // This is always returned, even when there’s an `err`. 32 | }) 33 | 34 | // Specify a custom easing function 35 | scroll.left(page, 200, { ease: ease.inBounce }) 36 | 37 | // Specify a duration in milliseconds (default: 350) and register a callback. 38 | scroll.left(page, 200, { duration: 1000 }, function (err, scrollLeft) { 39 | }) 40 | 41 | // Cancel a scroll animation 42 | var options = { duration: 1000 } 43 | var cancel = scroll.top(page, 200, options, function (err, scrollTop) { 44 | console.log(err.message) 45 | // => Scroll cancelled 46 | 47 | page.removeEventListener('wheel', cancel) 48 | }) 49 | 50 | page.addEventListener('wheel', cancel) 51 | ``` 52 | 53 | note: the default easing is `inOutSine` from [component/ease](https://github.com/component/ease). 54 | 55 | ## obey 56 | [MIT](https://github.com/michaelrhodes/scroll/blob/master/LICENSE) 57 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var run = require('tape') 2 | var ease = require('ease-component') 3 | var scroll = require('./') 4 | 5 | var container = document.createElement('div') 6 | var box = document.createElement('div') 7 | 8 | container.style.cssText = [ 9 | 'height: 100px', 10 | 'outline: 1px solid #000', 11 | 'overflow: scroll', 12 | 'width: 100px' 13 | ].join(';') 14 | 15 | box.style.cssText = [ 16 | 'outline: 1px solid #888', 17 | 'height: 50px', 18 | 'width: 300px' 19 | ].join(';') 20 | 21 | var n = 50 22 | while (--n) { 23 | container.appendChild(box.cloneNode(true)) 24 | } 25 | 26 | document.body.appendChild(container) 27 | 28 | run('it scrolls', function (test) { 29 | container.scrollTop = 0 30 | container.scrollLeft = 200 31 | 32 | test.plan(3) 33 | 34 | scroll.top(container, 200, function (error, position) { 35 | test.ok(position === 200, 'it scrolled down 200 pixels') 36 | 37 | scroll.top(container, 200, function (error, position) { 38 | test.equal(error.message, 'Element already at target scroll position') 39 | }) 40 | }) 41 | 42 | var leftOptions = { duration: 1000, ease: ease.inBounce } 43 | scroll.left(container, -200, leftOptions, function (error, position) { 44 | test.ok(position === 0, 'it scrolled across 200 pixels') 45 | }) 46 | }) 47 | 48 | run('it can be cancelled', function (test) { 49 | container.scrollTop = 0 50 | container.scrollLeft = 200 51 | 52 | test.plan(2) 53 | 54 | var options = { duration: 1000, ease: ease.inBounce } 55 | var cancel = scroll.left(container, -200, options, 56 | function (error, position) { 57 | test.ok(error, 'it produced an error') 58 | test.equal(error.message, 'Scroll cancelled', 'it cancelled the animation') 59 | }) 60 | 61 | setTimeout(cancel, 500) 62 | }) 63 | 64 | run('callback fires after scroll events', function (test) { 65 | container.scrollTop = 0 66 | test.plan(1) 67 | 68 | var okay = true 69 | var done = false 70 | 71 | container.addEventListener('scroll', function () { 72 | if (done) okay = false 73 | }, false) 74 | 75 | scroll.top(container, 200, function () { 76 | done = true 77 | setTimeout(function () { 78 | test.ok(okay, 'callback fired after scroll events') 79 | }, 100) 80 | }) 81 | }) 82 | --------------------------------------------------------------------------------