2011-05-19 83 views
0
$("#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上边距等于从顶部滚动的负距离。什么是最有效的方式来做到这一点(jQuery的片段)?

回答

1

缓存缓存缓存。

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

高效或短暂,因为你可以简单地做到这一点的简短。

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

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

相关问题