blog
copyright ©
creamu Inc. All Rights Reserved.
art direction & design: Kunitaka Kawashimo
code, interaction & photography: creamu Inc.
category: Technology

Parallaxを実装する方法

Pocket

<div class=”parallax-img” data-speed=”0.2″>
</div>
 
通常版:

function handleScroll() {
const scrollY = window.scrollY;
const fadeStart = 0;
const fadeEnd = 300;

// パララックス処理
const parallaxEls = document.querySelectorAll(‘[data-speed]’);
parallaxEls.forEach(el => {
const speed = parseFloat(el.getAttribute(‘data-speed’));
const yPos = scrollY * speed;
el.style.transform = `translateY(${yPos}px)`;
});

// フェード処理(リアルタイムにopacityを変える)
const wrappers = document.querySelectorAll(‘.fv_text_wrapper’);
wrappers.forEach(wrapper => {
const fadeRange = fadeEnd – fadeStart;
const scrollFade = Math.min(Math.max((scrollY – fadeStart) / fadeRange, 0), 1); // 0〜1に正規化
const opacity = 1 – scrollFade;
wrapper.style.opacity = opacity;
});
}

// 実行タイミング
window.addEventListener(‘load’, handleScroll);
window.addEventListener(‘scroll’, handleScroll);

 

heightが、100vw / 16 *9の計算。heightが0の時に完全に消したいバージョン:

if (!_ua.Mobile) {
functionhandleScroll() {
constscrollY=window.scrollY;
 
// 16:9の比率で高さを取得
constviewportWidth=window.innerWidth;
constfadeEnd= (viewportWidth/16) *9; // 例: 100vw → 56.25vh相当
 
// パララックス処理
constparallaxEls=document.querySelectorAll(‘[data-speed]’);
parallaxEls.forEach(el=> {
constspeed=parseFloat(el.getAttribute(‘data-speed’));
constyPos=scrollY*speed;
el.style.transform =`translateY(${yPos}px)`;
});
 
// フェード処理(高さ基準)
constwrappers=document.querySelectorAll(‘.mv’);
wrappers.forEach(wrapper=> {
// スクロール量を 0〜1 に正規化
constscrollFade=Math.min(Math.max(scrollY/fadeEnd, 0), 1);
constopacity=1-scrollFade;
 
wrapper.style.opacity =opacity;
});
}
 
// 初期表示とスクロール時に実行
window.addEventListener(‘load’, handleScroll);
window.addEventListener(‘scroll’, handleScroll);
} else {
}
 

Warning: A non-numeric value encountered in /home/users/1/creamuinc/web/creamu.co.jp/wp/wp-content/plugins/wp-social-bookmarking-light/vendor/twig/twig/lib/Twig/Environment.php(462) : eval()'d code on line 43

Warning: A non-numeric value encountered in /home/users/1/creamuinc/web/creamu.co.jp/wp/wp-content/plugins/wp-social-bookmarking-light/vendor/twig/twig/lib/Twig/Environment.php(462) : eval()'d code on line 43

Warning: A non-numeric value encountered in /home/users/1/creamuinc/web/creamu.co.jp/wp/wp-content/plugins/wp-social-bookmarking-light/vendor/twig/twig/lib/Twig/Environment.php(462) : eval()'d code on line 43