Skip to content

Commit 7c61159

Browse files
committed
add treshold option, fixed cs
1 parent 93ea2a4 commit 7c61159

7 files changed

Lines changed: 230 additions & 189 deletions

File tree

.eslintrc.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@
230230
],
231231
"linebreak-style": [
232232
2,
233-
"windows"
233+
"unix"
234234
],
235235
"max-nested-callbacks": [
236236
2,
@@ -367,4 +367,4 @@
367367
"prefer-template": 2,
368368
"require-yield": 2
369369
}
370-
}
370+
}

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Using a CDN via jsDelivr:
1818

1919
********************************************
2020

21-
##Method - trigger(options, callback)
21+
## Method - trigger(options, callback)
2222

2323
**Description:**
2424
A method for adding triggers when element is visible in the viewport.
@@ -36,6 +36,8 @@ as long as the scroll event is happening.
3636

3737
**alwaysRunOnTrigger (boolean)**: by default the triggered element callback will only be executed one time. Setting to true will re-trigger the callback everytime the element has been in and out of the viewport.
3838

39+
**treshold (number)**: add this much pixels to calculation to the visibility check (useful if you want to execute code just before the element becomes visible)
40+
3941
**callback (object)**:
4042
This is the function which will be exectued when the element is detected in the viewport. To reference the node, pass it
4143
into the callback as an argument.
@@ -46,14 +48,15 @@ into the callback as an argument.
4648
target: '.collection-list .items',
4749
surfaceVisible: 0.5,
4850
runOnScroll: true,
49-
alwaysRunOnTrigger: true
51+
alwaysRunOnTrigger: true,
52+
treshold: 200
5053
}, (element) => {
5154
$(element).addClass("visible");
5255
});
5356

5457
********************************************
5558

56-
##Method - sequence(options, callback)
59+
## Method - sequence(options, callback)
5760

5861
**Description:**
5962
A method for staggering an array of triggers.
@@ -98,7 +101,7 @@ can get the item and index of the array as arguments
98101

99102
********************************************
100103

101-
##Method - out(function)
104+
## Method - out(function)
102105

103106
When the trigger is has been executed and the element is no longer in the viewport, the out method
104107
can be chained to the trigger to execute the specified function.
@@ -116,7 +119,7 @@ can be chained to the trigger to execute the specified function.
116119

117120
********************************************
118121

119-
##Hooks
122+
## Hooks
120123

121124
**data-scrollmap-loaded (boolean):**
122125
Once the element is initialized.

dist/scrollmap.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
2020
* @namespace scrollMap
2121
* @description store element points and check if
2222
* elements are visible
23-
*/
23+
*/
2424

2525
var Scroll_Event_Trigger = function () {
2626
function Scroll_Event_Trigger() {
@@ -157,7 +157,7 @@ var Scroll_Event_Trigger = function () {
157157
}
158158
}, {
159159
key: "elementInViewport",
160-
value: function elementInViewport(el, percetageOfElement) {
160+
value: function elementInViewport(el, percetageOfElement, treshold) {
161161

162162
/*
163163
* @desc check if element is in viewport
@@ -173,7 +173,7 @@ var Scroll_Event_Trigger = function () {
173173
var rect = el.getBoundingClientRect();
174174

175175
var stats = {
176-
top: rect.top - window.innerHeight,
176+
top: rect.top - window.innerHeight + treshold,
177177
bottom: rect.bottom + rect.height,
178178
height: rect.height
179179
};
@@ -188,7 +188,7 @@ var Scroll_Event_Trigger = function () {
188188
}, {
189189
key: "checkVisible",
190190
value: function checkVisible(point) {
191-
var viewport = this.elementInViewport(point.element, point.surfaceVisible);
191+
var viewport = this.elementInViewport(point.element, point.surfaceVisible, point.treshold);
192192

193193
if (viewport) {
194194
this.setTriggerIn(point);
@@ -234,8 +234,20 @@ var Scroll_Event_Trigger = function () {
234234
value: function events() {
235235
var _this2 = this;
236236

237+
var supportsPassive = false;
238+
239+
try {
240+
var opts = Object.defineProperty({}, "passive", {
241+
get: function get() {
242+
supportsPassive = true;
243+
}
244+
});
245+
246+
window.addEventListener("test", null, opts);
247+
} catch (e) {} // eslint-disable-line no-empty
248+
237249
// initial check on page load to see if elements are visible
238-
window.addEventListener('load', function () {
250+
window.addEventListener("load", function () {
239251
_this2.points.forEach(function (point) {
240252
_this2.checkVisible(point);
241253
});
@@ -247,15 +259,13 @@ var Scroll_Event_Trigger = function () {
247259
_this2.points.forEach(function (point) {
248260
_this2.checkVisible(point);
249261
});
250-
});
262+
}, supportsPassive ? { passive: true } : false);
251263
}
252264
}]);
253265

254266
return Scroll_Event_Trigger;
255267
}();
256268

257-
;
258-
259269
var Scrollmap = new Scroll_Event_Trigger();
260270

261271
window.Scrollmap = Scrollmap;

dist/trigger.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
"use strict";
22

33
Object.defineProperty(exports, "__esModule", {
4-
value: true
4+
value: true
55
});
66

77
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
88

99
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
1010

1111
var Trigger = function () {
12-
function Trigger(element, options, callback) {
13-
_classCallCheck(this, Trigger);
14-
15-
this.element = element;
16-
this.surfaceVisible = 0.5;
17-
this.callback = callback;
18-
this.triggeredIn = false;
19-
this.triggeredOut = false;
20-
this.runOnScroll = false;
21-
this.alwaysRunOnTrigger = false;
22-
if (options) {
23-
Object.assign(this, options);
24-
}
25-
}
26-
27-
_createClass(Trigger, [{
28-
key: "onTriggerIn",
29-
value: function onTriggerIn() {
30-
this.callback(this.element);
31-
return this;
32-
}
33-
}, {
34-
key: "destroy",
35-
value: function destroy() {
36-
this.element = null;
37-
this.isDestroyed = true;
38-
}
39-
}]);
40-
41-
return Trigger;
12+
function Trigger(element, options, callback) {
13+
_classCallCheck(this, Trigger);
14+
15+
this.element = element;
16+
this.surfaceVisible = 0.5;
17+
this.treshold = 0;
18+
this.callback = callback;
19+
this.triggeredIn = false;
20+
this.triggeredOut = false;
21+
this.runOnScroll = false;
22+
this.alwaysRunOnTrigger = false;
23+
if (options) {
24+
Object.assign(this, options);
25+
}
26+
}
27+
28+
_createClass(Trigger, [{
29+
key: "onTriggerIn",
30+
value: function onTriggerIn() {
31+
this.callback(this.element);
32+
return this;
33+
}
34+
}, {
35+
key: "destroy",
36+
value: function destroy() {
37+
this.element = null;
38+
}
39+
}]);
40+
41+
return Trigger;
4242
}();
4343

4444
exports.default = Trigger;

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
"description": "A module for testing if an element is visible in the viewport, then triggers callbacks on execution.",
55
"main": "./dist/scrollmap.js",
66
"scripts": {
7-
"start": "set PROD_ENV=false&&webpack --progress --colors --watch",
7+
"start": "PROD_ENV=false webpack --progress --colors --watch",
88
"watch": "./node_modules/.bin/webpack -d --watch --colors",
9-
"build": "set PROD_ENV=true&&babel src -d dist",
10-
"dev": "set PROD_ENV=false&&webpack-dev-server ./src/scrollmap.js --hot --watch --colors",
11-
"cdn" : "set PROD_ENV=false&&webpack&&set PROD_ENV=true&&webpack -p",
9+
"build": "PROD_ENV=true babel src -d dist",
10+
"dev": "PROD_ENV=false webpack-dev-server ./src/scrollmap.js --hot --watch --colors",
11+
"cdn": "PROD_ENV=false webpack && PROD_ENV=true webpack -p",
1212
"lint": "eslint src"
1313
},
1414
"repository": {
@@ -47,7 +47,9 @@
4747
"webpack-dev-server": "^2.4.2"
4848
},
4949
"babel": {
50-
"presets": ["latest"]
50+
"presets": [
51+
"latest"
52+
]
5153
},
5254
"keywords": [
5355
"element",

0 commit comments

Comments
 (0)