jQuery:如何在浏览器调整大小时更新变量? [英] JQuery: how to update variable as browser resizes?

查看:65
本文介绍了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"设置为在浏览器调整大小时更新的正确/首选方法吗?脚本的上半部分显然在文档加载时确定了变量,但是我不确定下半部分是否可以正确编写脚本以在窗口扩展/收缩时更新该变量.

Is this the proper/preferred way to set the variable "footerheight" to update as the browser resizes? The top half of the script obviously determines the variable when the document loads but I'm not sure if the bottom half is scripted properly to update that variable as the window expands/contracts.

推荐答案

您可以通过使用变量来压缩它,例如:

You could condense it a little by using a variable, like:

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(),如下所示:

For resizing alone use .resize() like this:

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

这篇关于jQuery:如何在浏览器调整大小时更新变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆