ulovliglogning.dk/assets/js/main.js

120 lines
3.8 KiB
JavaScript
Raw Normal View History

2018-01-22 15:30:08 +00:00
document.body.className = "js";
2018-01-21 12:56:54 +00:00
//are we running in dev mode?
var debug = document.body.getAttribute("data-env") === "development" ? true : false;
2018-01-20 23:49:35 +00:00
2018-01-22 15:30:08 +00:00
document.body.style.height = document.body.offsetHeight + "px";
2018-01-21 12:56:54 +00:00
var controllerPanes = new ScrollMagic.Controller({
globalSceneOptions: {
//triggerHook: 'onLeave'
}
});
2018-01-22 15:30:08 +00:00
// var controllerAnimations = new ScrollMagic.Controller({
// globalSceneOptions: {
// //triggerHook: 'onLeave'
// }
// });
var md = new MobileDetect(window.navigator.userAgent);
var isDesktop = md.mobile() === null ? true : false;
2018-01-21 12:56:54 +00:00
function calculateOffset(){
var containerHeight = document.getElementById("pinContainer").offsetHeight;
var introHeight = document.getElementById("intro").offsetHeight;
var initialOffset = (containerHeight / introHeight * 100);
if (initialOffset > 100) {
return "-100%";
} else {
return "-" + Math.floor(initialOffset) + "%";
}
}
function createTween(section, duration ,index){
if (index === 0) {
var startY = calculateOffset();
} else {
var startY = "-0%";
}
var tween = TweenMax.fromTo(section, duration, {
y: startY,
2018-01-22 15:30:08 +00:00
force3D: true,
2018-01-21 12:56:54 +00:00
}, {
y: "-100%",
2018-01-22 15:30:08 +00:00
force3D: true
2018-01-21 12:56:54 +00:00
})
return tween;
}
function createTweenTimeline(){
var sections = document.querySelectorAll("section");
var sectionCount = sections.length;
var sectionDurations = [];
2018-01-20 23:49:35 +00:00
for (var i = 0; i < sections.length; i++) {
2018-01-21 12:56:54 +00:00
sectionDurations.push(sections[i].offsetHeight/300);
}
2018-01-21 12:56:54 +00:00
var tweens = [];
var timeline = new TimelineMax();
for (var i = 0; i < sections.length; i++) {
tweens.push(createTween(sections[i], sectionDurations[i], i));
timeline.add(tweens[i]);
}
return {
timeline: timeline,
tweens: tweens
};
}
2018-01-22 15:30:08 +00:00
function toggleDataAttr(attribute, toggle){
if (toggle) {
document.body.setAttribute("data-" + attribute, "enabled");
} else {
document.body.setAttribute("data-" + attribute, "disabled");
}
}
2018-01-20 23:49:35 +00:00
2018-01-22 15:30:08 +00:00
//only use the pane animations on desktop
if (isDesktop) {
toggleDataAttr("scrollmagicPanes", true);
wipeAnimation = createTweenTimeline();
2018-01-20 23:49:35 +00:00
2018-01-21 12:56:54 +00:00
var triggerElement = document.getElementsByClassName("container")[0];
var panelScene = new ScrollMagic.Scene({
triggerElement: triggerElement,
duration: "400%",
triggerHook: "onLeave"
})
.setPin(triggerElement)
.setTween(wipeAnimation.timeline)
.addTo(controllerPanes);
if (debug) {
panelScene.addIndicators()
2018-01-22 15:30:08 +00:00
}
wipeAnimation.tweens[0].updateTo({startAt: {y: calculateOffset()}}, false);
2018-01-21 12:56:54 +00:00
window.addEventListener('resize', function(){
2018-01-22 15:30:08 +00:00
//document.body.style.height = document.body.offsetHeight + "px";
console.log("pinContainer " + document.getElementById("pinContainer").offsetHeight)
console.log("body " + document.body.offsetHeight)
console.log("resize fired!")
// if (document.body.offsetHeight - document.getElementById("pinContainer").offsetHeight > 100) {
// console.log("adjusting offset!");
// wipeAnimation.tweens[0].updateTo({startAt: {y: calculateOffset()}}, false)
// }
2018-01-21 12:56:54 +00:00
})
2018-01-22 15:30:08 +00:00
} else {
toggleDataAttr("scrollmagicPanes", false);
}
2018-01-21 12:56:54 +00:00
2018-01-22 15:30:08 +00:00
var controllerAnimations = new ScrollMagic.Controller({});
var sections = document.querySelectorAll("section");
for (var i = 0; i < sections.length; i++) {
sectionScene = new ScrollMagic.Scene({
triggerElement: sections[i]
})
.setClassToggle(sections[i], "appear")
.addTo(controllerAnimations);
if (debug) {
sectionScene.addIndicators({name: "1 - add a class"}) // add indicators (requires plugin)
}
}