Skip to content

Commit b762ed3

Browse files
committed
Added percentage based offset #22
1 parent 843817b commit b762ed3

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ The currently available global options are:
2828
$('.dummy').viewportChecker({
2929
classToAdd: 'visible', // Class to add to the elements when they are visible
3030
classToRemove: 'invisible', // Class to remove before adding 'classToAdd' to the elements
31-
offset: 100, // The offset of the elements (let them appear earlier or later)
31+
offset: [100 OR 10%], // The offset of the elements (let them appear earlier or later). This can also be percentage based by adding a '%' at the end
3232
invertBottomOffset: true, // Add the offset as a negative number to the element's bottom
3333
repeat: false, // Add the possibility to remove the class if the elements are not visible
3434
callbackFunction: function(elem, action){}, // Callback to do after a class was added to an element. Action will return "add" or "remove", depending if the class was added or removed
@@ -42,7 +42,7 @@ Available attributes are:
4242
```code
4343
<div data-vp-add-class="random"></div> > classToAdd
4444
<div data-vp-remove-class="random"></div> > classToRemove
45-
<div data-vp-offset="100"></div> > offset
45+
<div data-vp-offset="[100 OR 10%]"></div> > offset
4646
<div data-vp-repeat="true"></div> > repeat
4747
<div data-vp-scrollHorizontal="false"></div> > scrollHorizontal
4848
```

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jQuery-viewport-checker",
3-
"version": "1.7.4",
3+
"version": "1.8.0",
44
"homepage": "https://github.com/dirkgroenen/jQuery-viewport-checker",
55
"authors": [
66
"Dirk Groenen <[email protected]>",

demo/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070
<body>
7171
<div class="spacer"><div class="text">Scroll down</div></div>
72-
<div class="dummy" data-vp-add-class="visible random1" data-vp-repeat="true" data-vp-offset="100" style="background-color: #2ecc71;">element 1 (repeat=true, offset=100)</div>
72+
<div class="dummy" data-vp-add-class="visible random1" data-vp-repeat="true" data-vp-offset="50%" style="background-color: #2ecc71;">element 1 (repeat=true, offset=100)</div>
7373
<div class="spacer"></div>
7474
<div class="dummy" data-vp-add-class="visible random1" data-vp-repeat="false" data-vp-offset="100" style="background-color: #9b59b6;">element 2 (repeat=false, offset=100)</div>
7575
<div class="spacer"></div>

src/jquery.viewportchecker.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Version 1.7.4
2+
Version 1.8.0
33
The MIT License (MIT)
44
55
Copyright (c) 2014 Dirk Groenen
@@ -79,6 +79,12 @@
7979
return;
8080
}
8181

82+
// Check if the offset is percentage based
83+
if(String(objOptions.offset).indexOf("%") > 0)
84+
objOptions.offset = (parseInt(objOptions.offset) / 100) * windowSize.height;
85+
86+
console.log(objOptions.offset);
87+
8288
// define the top position of the element and include the offset which makes is appear earlier or later
8389
var elemStart = (!objOptions.scrollHorizontal) ? Math.round( $obj.offset().top ) + objOptions.offset : Math.round( $obj.offset().left ) + objOptions.offset,
8490
elemEnd = (!objOptions.scrollHorizontal) ? elemStart + $obj.height() : elemStart + $obj.width();

0 commit comments

Comments
 (0)