JQuery:如何在浏览器调整大小时更新变量?

标签 jquery

<script>
    $(document).ready ( function () {
        var footerheight = $('#footer').height();
        $('#footer').css('height', footerheight);
        $('#footer').css('marginTop', - footerheight);
        $('#nonfooterinner').css('paddingBottom', footerheight);
    });
    $(window).bind("resize", function () {
        var footerheight = $('#footer').height();
        $('#footer').css('height', footerheight);
        $('#footer').css('marginTop', - footerheight);
        $('#nonfooterinner').css('paddingBottom', footerheight);
    });
</script>

这是将变量“footerheight”设置为随着浏览器大小调整而更新的正确/首选方法吗?脚本的上半部分显然决定了文档加载时的变量,但我不确定下半部分是否已正确编写脚本以在窗口扩展/收缩时更新该变量。

最佳答案

您可以使用变量稍微压缩它,例如:

var f = (function () {
       var footerheight = $('#footer').height();
       $('#footer').css('height', footerheight);
       $('#footer').css('marginTop', -footerheight);
       $('#nonfooterinner').css('paddingBottom', footerheight);
   });

$(document).ready(f);
$(window).resize(f);

单独调整大小时使用.resize()像这样:

$(window).resize(function () {
    var footerheight = $('#footer').height();
    $('#footer').css('height', footerheight);
    $('#footer').css('marginTop', - footerheight);
    $('#nonfooterinner').css('paddingBottom', footerheight);
});

关于JQuery:如何在浏览器调整大小时更新变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16907863/

相关文章:

javascript - 使用url hash执行点击事件

javascript - Wordpress + JQuery UI 选项卡 + 动态改变选项卡宽度

javascript - 为 div 中的每个子元素运行 javascript

javascript - 为什么 ajaxStop() 没有在 Internet Explorer 中触发?

javascript - 获取父函数的上下文-jquery

Jquery 循环 TR 并获取子选项

javascript - 从中心点旋转 SVG

jquery - Bootstrap CSS - 缩略图图像调整大小和响应式

JQuery Datepicker 未显示在 ASP.NET MVC 中

javascript - 使用 Javascript 在页面加载时显示随机单词