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

标签 javascript jquery performance

$("#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 = $("#content");
topheader = document.getElementById("topheader");
content.scroll(function() {
    topheader.style.marginTop  = -content.scrollTop() + "px";
});

关于javascript - 最有效的方法是什么(jquery 片段)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6065432/

相关文章:

Python:迭代列表与字典项目效率

javascript - 如何在有关 td 旁边显示响应

javascript - jquery中如何检测按钮是否折叠?

javascript - 给出输入后更改图像

javascript - 解析 'background-position' 值时出错

jquery css选择器将Class添加到具有背景图像的div

javascript - KnockoutJS 和外部 JSON

javascript - $(...).fullCalendar 不是一个函数 - Webpack

c - PIC32MZ 上的 Microchip Harmony 时序问题

database - 关系型数据库适用于软实时系统吗?