Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在调整窗口大小时更新jquery脚本?_Javascript_Jquery - Fatal编程技术网

Javascript 在调整窗口大小时更新jquery脚本?

Javascript 在调整窗口大小时更新jquery脚本?,javascript,jquery,Javascript,Jquery,我制作了一个jquery脚本,它依赖于视口宽度和视口高度的变量。 这是包含viewportwidth和viewportheight变量的脚本,存储在其自己的文档中: var viewportwidth,viewportheight;"undefined"!=typeof window.innerWidth?(viewportwidth=window.innerWidth,viewportheight=window.innerHeight):"undefined"!=typeof document

我制作了一个jquery脚本,它依赖于视口宽度和视口高度的变量。 这是包含
viewportwidth
viewportheight
变量的脚本,存储在其自己的文档中:

var viewportwidth,viewportheight;"undefined"!=typeof window.innerWidth?(viewportwidth=window.innerWidth,viewportheight=window.innerHeight):"undefined"!=typeof document.documentElement&&"undefined"!=typeof document.documentElement.clientWidth&&0!=document.documentElement.clientWidth?(viewportwidth=document.documentElement.clientWidth,viewportheight=document.documentElement.clientHeight):(viewportwidth=document.getElementsByTagName("body")[0].clientWidth,viewportheight=document.getElementsByTagName("body")[0].clientHeight);
以下是脚本:

function kjør() {
    if ((viewportwidth / viewportheight) >= 1.33) {
            $('#redline').appendTo('body');
        $('#sitewrap').on('scroll', function(e) {
            if ($(this).scrollTop() > (viewportheight / 100 * 40)) {
                $('body').addClass('fix');
                $('#headerwrap').appendTo('body');
            } else {
                $('body').removeClass('fix');
                $('#headerwrap').appendTo('header');
            }
        $("#logo-top").css("opacity", 1 - $(this).scrollTop() / (viewportheight / 100 * 30));
        });
    } else {
        $('#headerwrap').appendTo('body');
        $('#redline').prependTo('#headerwrap');
        $('body').removeClass('fix');
    };
};
$(function() {
    kjør();
    $(window).resize(kjør());
});
就像你在底部看到的那样。我已经尝试过使脚本在调整
窗口大小后更新。然而,这不起作用。脚本仍将使用在pageload上建立的
视口宽度
视口高度

如何使脚本在调整窗口大小时重新计算视口高度和视口宽度?

您的问题是下面这行:

 $(window).resize(kjør());
将其更改为:

 $(window).resize(kjør);

当您使用sintax
函数_name()
时,您引用的是函数的返回值,而不是函数本身。

您将
kjør()
函数的结果分配给调整大小处理程序。您需要为它提供函数引用:
$(window).resize(kjør)不完全重复,但其答案将解决问题。请不要回答此类问题。这是一个非常常见的错误。将其标记为副本