Skip to content

Instantly share code, notes, and snippets.

@joshrod
Last active April 28, 2020 01:46
Show Gist options
  • Save joshrod/e108a2bafe8d6b25d0e35500c284e326 to your computer and use it in GitHub Desktop.
Save joshrod/e108a2bafe8d6b25d0e35500c284e326 to your computer and use it in GitHub Desktop.
Manual "Pagepiling.js" effect for both mobile and desktop but last slide has the ability to scroll.
const homeBody = document.querySelector('.home');
const sections = document.querySelectorAll(".full-page");
let sectionNum = 0;
let currentSection = sections[sectionNum];
let nextSection;
let prevSection;
const newsSection = document.getElementById("news");
let footerTrigger = footer.offset().top;
/* VARIABLES FOR SWIPE DETECTION ON HOME PAGE */
const touchContainer = document.querySelector('.touch-container');
// Swipe Up / Down
let initailY = null;
if (window.innerWidth < 1240) {
touchContainer.addEventListener(
'touchstart',
function(e) {
for (let i = 0; i < sections.length; i++) {
const section = sections[i];
if (section === currentSection) {
nextSection = sections[i + 1];
if (nextSection === sections[5]) {
nextSection = sections[4];
}
prevSection = sections[i - 1];
if (prevSection === sections[-1]) {
prevSection = sections[0];
}
}
}
startTouch(e);
},
false);
// touchContainer.addEventListener('touchmove', moveTouch, false);
touchContainer.addEventListener(
'touchmove',
_.throttle(
function(e) {
let direction = moveTouch(e);
if (direction === "up") {
console.log("scrolled up");
animationHandler(
currentSection,
prevSection,
"moveToBottomEasing"
);
sectionNum--;
} else if (direction === "down") {
console.log("scrolled down");
animationHandler(
currentSection,
nextSection,
"moveToTopEasing"
);
sectionNum++;
// if (sectionNum === 4) {
// disableScroll(e);
// window.setTimeout(function(e){ enableScroll(e); }, 1300);
// window.setTimeout(function() { console.log('scroll is back'); });
// }
if (sectionNum === 4) {
nextSection.addEventListener('animationend', function() {
$('.home').css('overflow', 'visible');
console.log('body overflow should be visible');
})
console.log('sectionNum became 4');
}
}
currentSection = sections[sectionNum];
console.log(sectionNum);
console.log(currentSection);
//If at the last slide, change overflow to enable scroll
// if (currentSection === sections[sections.length - 1]) {
// $('.home').css('overflow', 'visible');
// console.log('body overflow should be visible');
// } else {
// $('.home').css('overflow', 'hidden');
// console.log('body overflow should be hidden');
// }
},
1300,
{trailing: false}
)
);
newsSection.addEventListener('touchstart', function(e) { startTouch(e); }, false);
newsSection.addEventListener('touchmove', _.throttle(function(e) {
let direction = moveTouch(e);
if (direction === "up") {
console.log("scrolled up");
animationHandler(
sections[4],
sections[3],
"moveToBottomEasing"
);
sectionNum--;
// $('.home').css('overflow', 'hidden');
// console.log('body overflow should be hidden');
}
currentSection = sections[sectionNum];
console.log(sectionNum);
console.log(currentSection);
},
1300,
{trailing: false}
)
);
if(window.innerWidth >= 1240) {
// * When changing the order of sections using animationHandler() MANUALLY SET THE nextSection AND prevSection VARIABLES. Don't forget to update the currentSection and sectionNum as well! Then, make cases where a section cannot animate to itself, etc.
// Throttle scrolling using lodash by 1.3s to prevent continuous section scrolling and give animations time to finish
window.addEventListener(
"wheel",
_.throttle(
function(e) {
for (let i = 0; i < sections.length; i++) {
const section = sections[i];
if (section === currentSection) {
nextSection = sections[i + 1];
if (nextSection === sections[5]) {
nextSection = sections[4];
}
prevSection = sections[i - 1];
if (prevSection === sections[-1]) {
prevSection = sections[0];
}
}
}
let direction = checkScrollDirection(e);
if (direction === "up") {
console.log("scrolled up");
animationHandler(
currentSection,
prevSection,
"moveToBottomEasing"
);
sectionNum--;
} else if (direction === "down") {
console.log("scrolled down");
animationHandler(
currentSection,
nextSection,
"moveToTopEasing"
);
sectionNum++;
}
currentSection = sections[sectionNum];
},
1300,
{ trailing: false }
)
);
}
}
function checkScrollDirection(e) {
if (
e.deltaY < 0 &&
currentSection !== sections[0] &&
currentSection !== sections[4]
) {
return "up";
}
// else if (e.deltaY < 0 &&
// currentSection === sections[4] &&
// window.pageYOffset === 0 &&
// window.innerWidth < 1240) {
// return "mobile-up";
// }
else if (
e.deltaY < 0 &&
currentSection === sections[4] &&
newsSection.scrollTop === 0
) {
return "up";
} else if (
e.deltaY > 0 &&
currentSection !== sections[sections.length - 1]
) {
return "down";
}
}
function animationHandler(fromPage, toPage, animation) {
let counterpart = "";
switch (animation) {
case "moveToTopEasing":
counterpart = "moveFromBottom";
fromPage.classList.add("on-top");
break;
case "moveToBottomEasing":
counterpart = "moveFromTop";
fromPage.classList.add("on-top");
break;
}
fromPage.classList.add(animation);
toPage.classList.add(counterpart);
toPage.classList.add("displayed");
const removeClasses = function() {
fromPage.classList.remove("displayed");
fromPage.classList.remove(animation);
toPage.classList.remove(counterpart);
fromPage.classList.remove("on-top");
toPage.classList.remove("on-top");
fromPage.removeEventListener("animationend", removeClasses);
};
fromPage.addEventListener("animationend", removeClasses);
}
function startTouch(e) {
initialY = e.touches[0].clientY;
}
function moveTouch(e) {
if (initialY === null) {
return;
}
let currentY = e.touches[0].clientY;
let diffY = initialY - currentY;
if (diffY < 0 &&
currentSection !== sections[0] &&
currentSection !== sections[4]) {
console.log('swiped down');
return "up";
} else if (
diffY < 0 &&
currentSection === sections[4] &&
window.pageYOffset === 0) {
console.log('swiped down');
return "up";
} else if (
diffY > 0 &&
currentSection !== sections[sections.length - 1]
) {
console.log('swiped up');
return "down";
}
initialY = null;
e.preventDefault();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment