![](/img/trans.png)
[英]how to find the vertical distance from top in px of an element using jQuery
[英]compare vertical distance from the top of an element and scroll distance from top of the page
我的js代码有问题,我想将距元素顶部的垂直距离与滚动距离进行比较,如果滚动距离大于距元素顶部的距离,则将div的位置固定为“ thead” id页。
我的js代码:
var distance = $("#thead").offset().top;
var wdistance = $(window).scrollTop();
if (wdistance > distance) {
alert('this is test');
};
演示
您只需要添加一个滚动处理程序:
var distance = $("#thead").offset().top;
$(window).scroll(function () {
var wdistance = $(window).scrollTop();
if (wdistance > distance) {
alert('this is test');
};
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.