Comparing version 0.1.4 to 0.2.0
@@ -1,18 +0,1 @@ | ||
if (!Date.now) { | ||
Date.now = function now() { | ||
return new Date().getTime(); | ||
}; | ||
} | ||
function throttle(callback, limit) { | ||
var lastCallTime = void 0; | ||
return function (parameters) { | ||
var currentCallTime = Date.now(); | ||
if (!lastCallTime || currentCallTime > lastCallTime + limit) { | ||
callback(parameters); | ||
lastCallTime = currentCallTime; | ||
} | ||
}; | ||
} | ||
var classCallCheck = function (instance, Constructor) { | ||
@@ -47,2 +30,3 @@ if (!(instance instanceof Constructor)) { | ||
this.window = window; | ||
this.withinRangeOfPageBottom = undefined; | ||
} | ||
@@ -59,6 +43,24 @@ | ||
var scroll = function scroll() { | ||
if (_this._withinRangeOfPageBottom(range)) { | ||
this.withinRangeOfPageBottom = this._withinRangeOfPageBottom(range); | ||
if (this.withinRangeOfPageBottom) { | ||
bottomFoundCallback(); | ||
if (!bottomLostCallback) { | ||
return; | ||
} | ||
} else if (bottomLostCallback) { | ||
bottomLostCallback(); | ||
} | ||
this._scroll = function () { | ||
if (_this._enteringRangeOfPageBottom(range)) { | ||
_this.withinRangeOfPageBottom = true; | ||
bottomFoundCallback(); | ||
} else { | ||
if (!bottomLostCallback) { | ||
_this.stop(); | ||
} | ||
} else if (_this._leavingRangeOfPageBottom(range)) { | ||
_this.withinRangeOfPageBottom = false; | ||
bottomLostCallback(); | ||
@@ -68,3 +70,2 @@ } | ||
this._scroll = throttle(scroll, 50); | ||
this.window.addEventListener("scroll", this._scroll); | ||
@@ -84,2 +85,12 @@ } | ||
}, { | ||
key: "_enteringRangeOfPageBottom", | ||
value: function _enteringRangeOfPageBottom(range) { | ||
return this.withinRangeOfPageBottom === false && this._withinRangeOfPageBottom(range); | ||
} | ||
}, { | ||
key: "_leavingRangeOfPageBottom", | ||
value: function _leavingRangeOfPageBottom(range) { | ||
return this.withinRangeOfPageBottom === true && !this._withinRangeOfPageBottom(range); | ||
} | ||
}, { | ||
key: "_pageHeight", | ||
@@ -86,0 +97,0 @@ get: function get$$1() { |
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.Sonar=e()}(this,function(){"use strict";function t(t,e){var n=void 0;return function(o){var i=Date.now();(!n||i>n+e)&&(t(o),n=i)}}Date.now||(Date.now=function(){return(new Date).getTime()});var e=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},n=function(){function t(t,e){for(var n=0;n<e.length;n++){var o=e[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}return function(e,n,o){return n&&t(e.prototype,n),o&&t(e,o),e}}(),o=function(){function o(t){e(this,o),this.window=t}return n(o,[{key:"ping",value:function(e,n,o){var i=this;if(!this._scroll){var r=function(){i._withinRangeOfPageBottom(e)?n():o()};this._scroll=t(r,50),this.window.addEventListener("scroll",this._scroll)}}},{key:"stop",value:function(){this.window.removeEventListener("scroll",this._scroll),this._scroll=null}},{key:"_withinRangeOfPageBottom",value:function(t){return this._viewportBottomScrollPosition>=this._pageHeight-t}},{key:"_pageHeight",get:function(){return this.window.document.body.offsetHeight}},{key:"_viewportBottomScrollPosition",get:function(){return this._viewportHeight+this._viewportTopScrollPosition}},{key:"_viewportTopScrollPosition",get:function(){return this.window.scrollY}},{key:"_viewportHeight",get:function(){return this.window.innerHeight}}]),o}();return o}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.Sonar=e()}(this,function(){"use strict";var t=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},e=function(){function t(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,n,i){return n&&t(e.prototype,n),i&&t(e,i),e}}(),n=function(){function n(e){t(this,n),this.window=e,this.withinRangeOfPageBottom=void 0}return e(n,[{key:"ping",value:function(t,e,n){var i=this;if(!this._scroll){if(this.withinRangeOfPageBottom=this._withinRangeOfPageBottom(t),this.withinRangeOfPageBottom){if(e(),!n)return}else n&&n();this._scroll=function(){i._enteringRangeOfPageBottom(t)?(i.withinRangeOfPageBottom=!0,e(),n||i.stop()):i._leavingRangeOfPageBottom(t)&&(i.withinRangeOfPageBottom=!1,n())},this.window.addEventListener("scroll",this._scroll)}}},{key:"stop",value:function(){this.window.removeEventListener("scroll",this._scroll),this._scroll=null}},{key:"_withinRangeOfPageBottom",value:function(t){return this._viewportBottomScrollPosition>=this._pageHeight-t}},{key:"_enteringRangeOfPageBottom",value:function(t){return this.withinRangeOfPageBottom===!1&&this._withinRangeOfPageBottom(t)}},{key:"_leavingRangeOfPageBottom",value:function(t){return this.withinRangeOfPageBottom===!0&&!this._withinRangeOfPageBottom(t)}},{key:"_pageHeight",get:function(){return this.window.document.body.offsetHeight}},{key:"_viewportBottomScrollPosition",get:function(){return this._viewportHeight+this._viewportTopScrollPosition}},{key:"_viewportTopScrollPosition",get:function(){return this.window.scrollY}},{key:"_viewportHeight",get:function(){return this.window.innerHeight}}]),n}();return n}); | ||
//# sourceMappingURL=sonar.js.map |
@@ -1,3 +0,1 @@ | ||
import { throttle } from "volve" | ||
export default class Sonar { | ||
@@ -7,2 +5,3 @@ | ||
this.window = window | ||
this.withinRangeOfPageBottom = undefined | ||
} | ||
@@ -13,6 +12,20 @@ | ||
let scroll = () => { | ||
if (this._withinRangeOfPageBottom(range)) { | ||
this.withinRangeOfPageBottom = this._withinRangeOfPageBottom(range) | ||
if (this.withinRangeOfPageBottom) { | ||
bottomFoundCallback() | ||
if (!bottomLostCallback) { return } | ||
} else if (bottomLostCallback) { | ||
bottomLostCallback() | ||
} | ||
this._scroll = () => { | ||
if (this._enteringRangeOfPageBottom(range)) { | ||
this.withinRangeOfPageBottom = true | ||
bottomFoundCallback() | ||
} else { | ||
if (!bottomLostCallback) { this.stop() } | ||
} else if (this._leavingRangeOfPageBottom(range)) { | ||
this.withinRangeOfPageBottom = false | ||
bottomLostCallback() | ||
@@ -22,3 +35,2 @@ } | ||
this._scroll = throttle(scroll, 50) | ||
this.window.addEventListener("scroll", this._scroll) | ||
@@ -36,2 +48,10 @@ } | ||
_enteringRangeOfPageBottom(range) { | ||
return this.withinRangeOfPageBottom === false && this._withinRangeOfPageBottom(range) | ||
} | ||
_leavingRangeOfPageBottom(range) { | ||
return this.withinRangeOfPageBottom === true && !this._withinRangeOfPageBottom(range) | ||
} | ||
get _pageHeight() { | ||
@@ -38,0 +58,0 @@ return this.window.document.body.offsetHeight |
{ | ||
"name": "sonar-js", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "A tiny library for detecting when a browser is scrolled to the bottom of a web page.", | ||
@@ -10,4 +10,6 @@ "main": "./dist/sonar.js", | ||
"build": "rollup --config rollup.config.umd.js && rollup --config rollup.config.es.js", | ||
"eslint": "eslint --format=node_modules/eslint-formatter-pretty ./lib; exit 0", | ||
"prepublish": "npm run build" | ||
"eslint": "eslint --format=node_modules/eslint-formatter-pretty ./lib ./test; exit 0", | ||
"prepublish": "npm run build", | ||
"test": "ava", | ||
"test:watch": "ava --watch" | ||
}, | ||
@@ -25,3 +27,7 @@ "author": { | ||
"devDependencies": { | ||
"ava": "^0.17.0", | ||
"babel-eslint": "^7.1.1", | ||
"babel-preset-es2015": "^6.18.0", | ||
"babel-preset-es2015-rollup": "^3.0.0", | ||
"babel-register": "^6.18.0", | ||
"eslint": "^3.12.2", | ||
@@ -32,2 +38,3 @@ "eslint-config-standard": "^6.2.1", | ||
"eslint-plugin-standard": "^2.0.1", | ||
"jsdom": "^9.9.1", | ||
"rollup": "^0.39.0", | ||
@@ -39,5 +46,13 @@ "rollup-plugin-babel": "^2.7.1", | ||
}, | ||
"dependencies": { | ||
"volve": "^0.0.8" | ||
"dependencies": {}, | ||
"ava": { | ||
"require": [ | ||
"babel-register" | ||
] | ||
}, | ||
"babel": { | ||
"presets": [ | ||
"es2015" | ||
] | ||
} | ||
} |
@@ -20,4 +20,8 @@ # Sonar.js | ||
Create an instance of `Sonar` passing in the `window` and then call `ping` passing in the range to the bottom of the page, a function to call when within range of the bottom and a function to call when not within range of the bottom. | ||
Create an instance of `Sonar` passing in the `window` and then call `ping` passing in the range to the bottom of the page, a function to call when within range of the bottom and an optional function to call when not within range of the bottom. | ||
When `ping` is called one of the callbacks will fire, depending on whether or not the scroll is currently within range of the page bottom. The behavior after that depends on which callbacks are provided. | ||
### One callback | ||
```javascript | ||
@@ -30,2 +34,18 @@ var sonar = new Sonar(window) | ||
sonar.ping(600, withinRangeOfPageBottom) | ||
``` | ||
Without a second callback to fire when losing the bottom of the page this is essentially a one-time use. The callback will fire once when finding the bottom of the page and then never fire again, even if you scroll up and back down again. | ||
This is useful for making a permanent, persistent change when someone scrolls to the bottom of the page. | ||
### Two callbacks | ||
```javascript | ||
var sonar = new Sonar(window) | ||
var withinRangeOfPageBottom = function() { | ||
document.querySelector('.popover').classList.remove('hidden') | ||
} | ||
var notWithinRangeOfPageBottom = function() { | ||
@@ -38,2 +58,8 @@ document.querySelector('.popover').classList.add('hidden') | ||
`Sonar#ping` is throttled to not call the callbacks more than once every 50ms. | ||
With a second callback to fire when losing the bottom of the page this becomes a toggle. The callbacks will only fire once when finding the bottom or losing the bottom. That is, the callbacks will only fire or re-fire when the state changes. | ||
This is useful for making a permanent, temporary change when someone scrolls to the bottom of the page and then reversing it when they scroll away from the bottom of the page. | ||
## Tests | ||
`yarn test` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
149880
0
10
288
63
16
1
- Removedvolve@^0.0.8
- Removedvolve@0.0.8(transitive)