开发者

What is the most efficient way to do this (jquery snippet)?

开发者 https://www.devze.com 2023-03-07 16:04 出处:网络
$(\"#content\").scroll(function(e){ var distance_from_top = $(\"#content\").scrollTop(); var theinverse = -1 * distance_from_top;
$("#content").scroll(function(e){
    var distance_from_top = $("#content").scrollTop();
    var theinverse = -1 * distance_from_top;
    var theinverse2 = theinverse + "px";
    $("#topheader").css( "marginTop", theinverse2);
});

What's the most efficient way to do the above? Basically make #topheader top margin equal to the negati开发者_如何学JAVAve distance scrolled from the top.


caching caching caching.

content = $("#content");
topheader = document.getElementById("topheader");
content.scroll(function() {
    topheader.style.marginTop  = -content.scrollTop() + "px";
});


Efficient or short because you could simply do this for shortness.

$("#content").scroll(function(e){
    $("#topheader").css( "marginTop", -$("#content").scrollTop() + "px");
});

When probably need more context (source of the web page) if you want efficiency.

0

精彩评论

暂无评论...
验证码 换一张
取 消