Hello,
I want to add to my site some JavaScript interaction but I can’t make anything working, despite some research here and there.
I noticed that vanilla JS won’t work on Wix, or Velo doesn’t support DOM.
The idea is ideally (PLEASE correct my code if you see any mistakes, failures,…):
import wixWindow from 'wix-window';
$w.onReady(function () {
// get section ID to be fade out
const sectionId = 'sectionFadeOut';
// add scrolling event to the window
wixWindow.addEventListener('scroll', function () {
// get the actual window position relative to the top of the page
const scrollTop = wixWindow.scrollY;
// get the section position relative to the top of the page
const sectionOffset = $w(sectionId).offsetTop;
// get the height of the section
const sectionHeight = $w(sectionId).clientHeight;
// put the opacity according to scrolling position
const minOpacity = 0;
const maxOpacity = 1;
let opacity = 1 - (scrollTop - sectionOffset) / sectionHeight;
// Limit opacity between 0 and 1
opacity = Math.max(minOpacity, Math.min(maxOpacity, opacity));
// apply opacity to the section
$w(sectionId).opacity = opacity;
});
});
Thanks for any help