I am using the code below to change the scroll speed of certain elements. Code I have copied from HERE.
To double check my overall code I have also added a color change to ".slogan-a-line".
The if/else is depending on if "#image-ul" is fully above the bottom edge of the browser window.
My problem is that –when either resizing or scrolling the browser window– the color of ".slogan-a-line" does change as it should, but the data-scroll-speeds on other elements do not. When (re)loading they do.
Initially, when first loading the webpage, the correct data-scroll-speed is set by the code. But when resizing the browser window –to change whether "#image-ul" is fully above the bottom edge of the browser window or not– the data scroll speeds do not change, till I refresh the webpage (so the id and class names are correct).
I need the data-scroll-speeds to change without having to refresh the browser window. Can anyone see what I have done wrong?
<script>
// Assign attribute specific "data-scroll-speed" to elements upon loading, resizing and scrolling of the webpage page. "if/else" is depending on if #image-ul is fully above the bottom edge of the browser window.
$(document).ready(function() {
$(window).on('load resize scroll', function() {
var WindowScrollTop = $(this).scrollTop(),
Div_one_top = $('#image-ul').offset().top,
Div_one_height = $('#image-ul').outerHeight(true),
Window_height = $(this).outerHeight(true);
if (WindowScrollTop + Window_height >= (Div_one_top + Div_one_height)) {
$('#sloganenglish').attr('data-scroll-speed', '2');
$('.slow-scroll-slider').attr('data-scroll-speed', '5');
$('.slogan-a-line').css('color', 'green');
} else {
$('#sloganenglish').attr('data-scroll-speed', '1');
$('.slow-scroll-slider').attr('data-scroll-speed', '1');
$('.slogan-a-line').css('color', 'red');
}
}).scroll();
});
// data-scroll-speed script
$.fn.moveIt = function() {
var $window = $(window);
var instances = [];
$(this).each(function() {
instances.push(new moveItItem($(this)));
});
window.onscroll = function() {
var scrollTop = $window.scrollTop();
instances.forEach(function(inst) {
inst.update(scrollTop);
});
}
}
var moveItItem = function(el) {
this.el = $(el);
this.speed = parseInt(this.el.attr('data-scroll-speed'));
};
moveItItem.prototype.update = function(scrollTop) {
var pos = scrollTop / this.speed;
this.el.css('transform', 'translateY(' + -pos + 'px)');
};
// Initialization
$(function() {
$('[data-scroll-speed]').moveIt();
});
</script>