mkafiyan
mkafiyan

Reputation: 954

compare vertical distance from the top of an element and scroll distance from top of the page

I have problem with my js codes,I want to compare vertical distance from top of an element with scroll distance and if scroll distance is more than distance from the top of an element fixed the position of div with "thead" id in top of the page.

my js code :

var distance = $("#thead").offset().top;
var wdistance = $(window).scrollTop();
if (wdistance > distance) {
 alert('this is test');
};

demo in

jsfiddle

Upvotes: 1

Views: 421

Answers (1)

Ionică Bizău
Ionică Bizău

Reputation: 113475

You just need to add a scroll handler:

var distance = $("#thead").offset().top;
$(window).scroll(function () {
    var wdistance = $(window).scrollTop();
    if (wdistance > distance) {
        alert('this is test');
    };
});

JSFIDDLE

Upvotes: 1

Related Questions