I only want the full-page parallax effect to be applied on screen width more than 1366.
I've tried if(window.innerWidth >= 1366 ) {}
but then the script will have totally no effect at all when I added the script into it.
I know some people would say the script only run once on load but I did try add an resize EventListener as well, eventually it's the same where the script will have no effect after adding into something like
window.addEventListener("resize", () => {
if(window.innerWidth >= 1366) {
//Script here...
});
or
if(window.innerWidth >= 1366){
//Script here...
}
My intention is to only apply the effect on screen width more than 1366 and that's why I use if(window.innerWidth >= ) {}
, but if there are other ways to make the same result then I'm fine.
*It's different from What is the best way to detect a mobile device? because I can't define the exact width and most importantly, it's making the script not working.
Below is the script:
var app = new Vue({
el: '#app',
data: {
inMove: false,
activeSection: 0,
offsets: [],
touchStartY: 0
},
methods: {
calculateSectionOffsets() {
let sections = document.getElementsByTagName('section');
let length = sections.length;
for(let i = 0; i < length; i++) {
let sectionOffset = sections[i].offsetTop;
this.offsets.push(sectionOffset);
}
},
handleMouseWheel: function(e) {
if (e.wheelDelta < 30 && !this.inMove) {
this.moveUp();
} else if (e.wheelDelta > 30 && !this.inMove) {
this.moveDown();
}
e.preventDefault();
return false;
},
handleMouseWheelDOM: function(e) {
if (e.detail > 0 && !this.inMove) {
this.moveUp();
} else if (e.detail < 0 && !this.inMove) {
this.moveDown();
}
return false;
},
moveDown() {
this.inMove = true;
this.activeSection--;
if(this.activeSection < 0) this.activeSection = this.offsets.length - 1;
this.scrollToSection(this.activeSection, true);
},
moveUp() {
this.inMove = true;
this.activeSection++;
if(this.activeSection > this.offsets.length - 1) this.activeSection = 0;
this.scrollToSection(this.activeSection, true);
},
scrollToSection(id, force = false) {
if(this.inMove && !force) return false;
this.activeSection = id;
this.inMove = true;
document.getElementsByTagName('section')[id].scrollIntoView({behavior: 'smooth'});
setTimeout(() => {
this.inMove = false;
}, 400);
},
touchStart(e) {
e.preventDefault();
this.touchStartY = e.touches[0].clientY;
},
touchMove(e) {
if(this.inMove) return false;
e.preventDefault();
const currentY = e.touches[0].clientY;
if(this.touchStartY < currentY) {
this.moveDown();
} else {
this.moveUp();
}
this.touchStartY = 0;
return false;
}
},
created() {
this.calculateSectionOffsets();
window.addEventListener('DOMMouseScroll', this.handleMouseWheelDOM); // Mozilla Firefox
window.addEventListener('mousewheel', this.handleMouseWheel, { passive: false }); // Other browsers
window.addEventListener('touchstart', this.touchStart, { passive: false }); // mobile devices
window.addEventListener('touchmove', this.touchMove, { passive: false }); // mobile devices
},
destroyed() {
window.removeEventListener('mousewheel', this.handleMouseWheel, { passive: false }); // Other browsers
window.removeEventListener('DOMMouseScroll', this.handleMouseWheelDOM); // Mozilla Firefox
window.removeEventListener('touchstart', this.touchStart); // mobile devices
window.removeEventListener('touchmove', this.touchMove); // mobile devices
}
});
body {
margin: 0;
color: #FFF;
font-family: Helvetica, arial, sans-serif;
overflow: hidden;
}
h2 {
position: fixed;
}
.fullpage {
height: 100vh;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
h1 {
font-size: 6em;
margin: 0;
text-align: center;
padding: 0 1rem;
}
p {
font-size: 1em;
}
.fullpage a {
text-decoration: none;
font-weight: 600;
background: rgba(255, 255, 255, 0.3);
padding: 5px 10px;
color: #FFF;
margin-left: 5px;
}
.red {
background-color: #ab4545;
}
section.black {
background-color: #000;
}
.blue {
background-color: #237ad4;
}
.green {
background-color: #68c368;
}
h1.black {
color: #000;
}
.sections-menu {
position: fixed;
right: 1rem;
top: 50%;
transform: translateY(-50%);
}
.sections-menu .menu-point {
width: 10px;
height: 10px;
background-color: #FFF;
display: block;
margin: 1rem 0;
opacity: .6;
transition: .4s ease all;
cursor: pointer;
}
.sections-menu .menu-point.active {
opacity: 1;
transform: scale(1.5);
}
@media screen and (max-width: 1200px) {
h1 {
font-size: 2.5em;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Vue.js Fullpage Scroll</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="app">
<div class="sections-menu">
<span
class="menu-point"
v-bind:class="{active: activeSection == index}"
v-on:click="scrollToSection(index)"
v-for="(offset, index) in offsets"
v-bind:key="index">
</span>
</div>
<section class="fullpage blue">
<h1>Vue.js Fullpage Scroll</h1>
<p>by <a href="https://webdeasy.de/?referer=cp-NVOEBL" target="_blank">WebDEasy</a></p>
</section>
<section class="fullpage black">
<h1>Section 2</h1>
<p>made with <a href="https://vuejs.org/" target="_blank">Vue.js</a></p>
</section>
<section class="fullpage red">
<h1>Section 3</h1>
<p>works on <b>desktop & mobile</b></p>
</section>
<section class="fullpage green">
<h1>Section 4</h1>
<p>Tutorial <a href="https://webdeasy.de/en/programming-vue-js-fullpage-scroll/?referer=cp-NVOEBL" target="_blank">here</a></p>
</section>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js"></script>
</body>
</html>