I'm trying to convert some jQuery code to Vanilla JS. The purpose is to invert the scroll direction of a container. The jQuery version works fine. However I'm struggling to convert it in JS.
$(window).on('scroll',function(){
$(".container").css('bottom',$(window).scrollTop()*-1);
});
The JS:
const container = document.querySelector('.container');
var scroll = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
window.addEventListener('scroll', function() {
container.style.bottom = "";
});
Thank you.
You might want to consider working with CSS
position:fixedand without JS. Otherwise, as I said in the first comment:1.) Make a function that returns the current scroll position
2.) On each scroll event call the function
3.) Assign the returned value/scroll position with a negative multiplication (* -1) to the
.style.bottomproperty on the container, don't forget to tack "px" onto the valueTry it: