Created
August 16, 2016 14:10
-
-
Save cee-chen/9e332c56d23176d0554eed6001647186 to your computer and use it in GitHub Desktop.
Reusable js-class-hook & data attributes for setting pins
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
/*---------------------------------------------- | |
Generic & reusable logic for sticky/fixed pins | |
Requires scrollmagic.io | |
-----------------------------------------------*/ | |
// Dependencies | |
if (ScrollMagic === undefined) { return; } | |
document.addEventListener("DOMContentLoaded", function() { | |
var controller = new ScrollMagic.Controller(); | |
var elements = document.getElementsByClassName("js-scroll-trigger"); | |
for (var i = 0; i < elements.length; i++) { | |
var el = elements[i]; | |
var pin = el.querySelector(".js-scroll-pin"); | |
var opts = { triggerElement: el }; | |
if (el.dataset && el.dataset.scrollTriggerhook) { | |
opts.triggerHook = parseInt(el.dataset.scrollTriggerhook); | |
} | |
if (el.dataset && el.dataset.scrollOffset) { | |
opts.offset = parseInt(el.dataset.scrollOffset); | |
} | |
opts.duration = el.clientHeight; | |
var scene = new ScrollMagic.Scene(opts) | |
.setPin(pin) | |
.addTo(controller); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment