├── .editorconfig ├── .gitattributes ├── .gitignore ├── .npmrc ├── example ├── index.html └── index.js ├── index.js ├── license ├── package.json └── readme.md /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | example/build.js 2 | node_modules 3 | yarn.lock 4 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | scroll-past 7 | 24 | 25 | 26 |
27 |
28 |
29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /example/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const scrollPast = require('../'); 3 | 4 | scrollPast(document.querySelector('.ScrollPast')).then(pos => { 5 | console.log(`I'm at ${pos}`); 6 | }); 7 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | module.exports = function (el, opts) { 3 | opts = Object.assign({threshold: 0}, opts); 4 | 5 | const scrollTop = el.scrollTop; 6 | const height = el.offsetHeight + opts.threshold; 7 | const bottom = scrollTop + height; 8 | 9 | let tick; 10 | 11 | return new Promise(resolve => { 12 | window.addEventListener('scroll', () => { 13 | const pos = window.scrollY; 14 | 15 | if (!tick) { 16 | requestAnimationFrame(() => { 17 | tick = null; 18 | 19 | if (pos < bottom) { 20 | return; 21 | } 22 | 23 | resolve(pos); 24 | }); 25 | } 26 | 27 | tick = true; 28 | }); 29 | }); 30 | }; 31 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Kevin Mårtensson (github.com/kevva) 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "scroll-past", 3 | "version": "2.0.0", 4 | "description": "Return a promise when any element is scroll past", 5 | "license": "MIT", 6 | "repository": "kevva/scroll-past", 7 | "author": { 8 | "name": "Kevin Mårtensson", 9 | "email": "kevinmartensson@gmail.com", 10 | "url": "github.com/kevva" 11 | }, 12 | "engines": { 13 | "node": ">=4" 14 | }, 15 | "scripts": { 16 | "build": "webpack example/index.js example/build.js", 17 | "test": "xo" 18 | }, 19 | "files": [ 20 | "index.js" 21 | ], 22 | "keywords": [ 23 | "past", 24 | "promise", 25 | "scroll", 26 | "viewport" 27 | ], 28 | "devDependencies": { 29 | "webpack": "^3.5.5", 30 | "xo": "*" 31 | }, 32 | "xo": { 33 | "envs": [ 34 | "browser", 35 | "node" 36 | ], 37 | "ignores": [ 38 | "example/build.js" 39 | ] 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # scroll-past 2 | 3 | > Return a Promise when any element is scroll past 4 | 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install scroll-past 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```js 16 | const scrollPast = require('scroll-past'); 17 | 18 | scrollPast(document.querySelector('.container')).then(pos => { 19 | console.log(`I'm at ${pos}`); 20 | }); 21 | ``` 22 | 23 | 24 | ## API 25 | 26 | ### scrollPast(element, [options]) 27 | 28 | Returns a `Promise` that resolves the `scrollY` position of the window. 29 | 30 | #### element 31 | 32 | Type: `Element` 33 | 34 | The element to check for. 35 | 36 | #### options 37 | 38 | ##### threshold 39 | 40 | Type: `number`
41 | Default: `0` 42 | 43 | Add a threshold to scroll past before the `Promise` is returned. 44 | 45 | 46 | ## License 47 | 48 | MIT © [Kevin Mårtensson](https://github.com/kevva) 49 | --------------------------------------------------------------------------------