Dennis
Dennis

Reputation: 368

Match the height of 2 elements and log when higher

I have created a small script which checks the offset of 2 items and then when the item has a higher offset than the recorded screen height when scrolling. It needs to log something 'lower'.

(function () {
    $(document).ready(function () {    
        var heightScreen =  $('.hero-screen').height();
        var item1 = $('.contact-menu').offset().top;

        $(window).scroll(function() {    
            if (item1 >= heightScreen) {
                console.log('lower');
            }
        });
    });
})();

The script works but only when I refresh the page when it is already out of the heightScreen variable.

Upvotes: 0

Views: 25

Answers (1)

Kevin Grosgojat
Kevin Grosgojat

Reputation: 1379

If you want to log the height at the end of scrolling, you have do calculate it in the callbaclk.

try this.

$(document).ready(function () {
    $(window).scroll(function() {
        var heightScreen =  $('.hero-screen').height();
        var item1 = $('.contact-menu').offset().top;
        if (item1 >= heightScreen) {
            console.log('lower');
        }
    });
});

Upvotes: 1

Related Questions