简体   繁体   English

jQuery窗口调整大小事件不适用于初始加载

[英]jquery window resize event not working on initial load

I know flex box is an option to change the orders of divs however I am using a bit of jquery instead of installing flexbox for one needed feature 我知道可以使用flex box来更改div的顺序,但是我使用了一些jquery而不是为一个所需的功能安装flexbox

This is the script I am using which works fine when resizing the window 这是我使用的脚本,在调整窗口大小时可以正常工作

<script type="text/javascript">
$(document).load($(window).bind("resize", listenWidth));

    function listenWidth( e ) {
        if($(window).width()>910)
        {
            $(".loginblock").remove().insertAfter($("#step1"));
        } else {
            $(".loginblock").remove().insertBefore($(".regblock"));
        }
    }
</script>

Problem: 问题:

Although the script works well if you physically resize the browser window yourself. 尽管如果您亲自调整浏览器窗口的大小,该脚本也可以很好地工作。 If I refresh the screen once the change has been made. 如果在进行更改后刷新屏幕。 It does not work. 这是行不通的。 for example if I go to mobile size and refresh the loginblock goes back to the bottom instead of the top. 例如,如果我转到移动设备大小并刷新,则loginblock返回底部而不是顶部。

Is there a simple way to edit my script to know what size the screen is on load and make the changes needed? 有没有一种简单的方法可以编辑我的脚本,以了解加载屏幕的大小并进行所需的更改?

Thanks! 谢谢!

You bind the function on resize... Ok. 您可以在调整大小时绑定该函数...确定。
You only need to also trigger it onload. 您只需要在加载时触发它即可。

<script type="text/javascript">
$(document).load($(window).bind("resize", listenWidth));

    function listenWidth( e ) {
        if($(window).width()>910)
        {
            $(".loginblock").remove().insertAfter($("#step1"));
        } else {
            $(".loginblock").remove().insertBefore($(".regblock"));
        }
    }
    listenWidth();  // Add this here to execute the funtion onload.
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM