繁体   English   中英

什么是最有效的方法(jquery片段)?

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

$("#content").scroll(function(e){
    var distance_from_top = $("#content").scrollTop();
    var theinverse = -1 * distance_from_top;
    var theinverse2 = theinverse + "px";
    $("#topheader").css( "marginTop", theinverse2);
});

什么是最有效的方法来做到这一点? 基本上使#topheader上边距等于从顶部滚动的负距离。

有效还是短,因为你可以简单地做到这一点。

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

如果你想要效率,可能需要更多的上下文(网页的来源)。

缓存缓存缓存。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM