-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support multiple classes for rf-scrollclass
- Loading branch information
1 parent
cadc257
commit 8a3f011
Showing
1 changed file
with
16 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,27 @@ | ||
/** | ||
* Add/remove class on scroll position | ||
* | ||
* Usage: | ||
* <a href='#' rf-scrollclass='show@300'>Add class "show" at 300px scrollposition</a> | ||
* | ||
* Add multiple classes (thx @StefanThumann) | ||
* <a href='#' rf-scrollclass='show@300 show2@600'>Add class "show" at 300px scrollposition, "show2" at 600px</a> | ||
*/ | ||
(function () { | ||
let scrollElements = document.querySelectorAll("[rf-scrollclass]"); | ||
for (i = 0; i < scrollElements.length; i++) { | ||
let el = scrollElements[i]; | ||
let attr = el.getAttribute("rf-scrollclass"); | ||
let parts = attr.split("@"); | ||
if (parts.length != 2) return; | ||
let cls = parts[0]; | ||
let y = parts[1] * 1; | ||
window.addEventListener("scroll", function () { | ||
scrollpos = window.scrollY; | ||
if (scrollpos >= y) el.classList.add(cls); | ||
else el.classList.remove(cls); | ||
}); | ||
let attrs = el.getAttribute("rf-scrollclass").split(" "); | ||
for (j = 0; j < attrs.length; j++) { | ||
let parts = attrs[j].split("@"); | ||
if (parts.length != 2) return; | ||
let cls = parts[0]; | ||
let y = parts[1] * 1; | ||
window.addEventListener("scroll", function () { | ||
scrollpos = window.scrollY; | ||
if (scrollpos >= y) el.classList.add(cls); | ||
else el.classList.remove(cls); | ||
}); | ||
} | ||
} | ||
})(); |