JQuery:窗口调整大小的更改宽度始终为0

时间:2015-07-02 14:03:34

标签: javascript jquery html css

我试图在窗口调整大小时更改div元素的宽度,但由于调整大小功能,元素的宽度最后总是为

   function resizeUploadField() {
        var uploadInputField = $('.input-append');
        var uploadSelectButton = $('.btn-file');
        var uploadRemoveButton = $('.fileupload-exists');

        if (uploadRemoveButton.length) {

        }
        console.log(uploadInputField.width());
        uploadInputField.width(uploadInputField.width() - uploadSelectButton.outerWidth() - 2);
        console.log(uploadInputField.width());
    }


    $(document).ready(function() {
        var windowWidth = $(window).width();
        resizeUploadField();

        $(window).resize(function(event) {
            console.log($(event.target).width());
            if ($(event.target).width() != windowWidth) {
                resizeUploadField();
                windowWidth =  $(event.target).width();
            }

        });

resizeUploadField函数似乎工作正常,因为在onDocumentReady中调用时字段已正确调整大小

也许有人可以给我一个暗示出错的地方

1 个答案:

答案 0 :(得分:0)

尝试重新声明windowWidth = $(window).width();功能中的resize()

<强> E.g。

$(document).ready(function() {
    var windowWidth = $(window).width();
    resizeUploadField();

    $(window).resize(function(event) {
        windowWidth = $(window).width();
        console.log($(event.target).width());
        if ($(event.target).width() != windowWidth) {
            resizeUploadField();
            windowWidth = $(event.target).width();
        }
    });
});

希望它有所帮助。