Home>

I don't understand how to write Jquery.

Original source

$(window) .bind ("load resize", function () {
~~~
}). trigger ("resize");

When the window is loaded
When the screen is resized
It is designed to execute the corresponding function in
This "load" part

$(document) .ready (function () {


How do you write "when resized" to this syntax?
I searched with "JQuery ready resize" etc., but it is stuck in a situation where only hits using the same load as the original source are hit.

Even if it's not just how to write it, it would be helpful to have information such as "You can hit if you look up with these words".

I would appreciate your wisdom.

  • Answer # 1

    Since $(fnc) doesn't move at the time of load, it may be inserted as shown below or originally.

    $(function () {
        $(window) .on ("load resize", function () {
            // ~~~
        }) .trigger ("resize");
    });
    $(function () {
        $(window) .trigger ("resize");
        // ~~~
    });
    $(window) .on ("load resize", function () {
        // ~~~
    });

    [Difference between $(function () {}) and $(window) .on ('load', function () {})]
    https://qiita.com/mimoe/items/74cb3a01a30162759fdd

    【About jQuery reading "ready", "load" and "function" | web-wizardry]
    http://web-wizardry.net/jquery/About jquery read/load order/

  • Answer # 2

    $(document) .ready (function () {
        wresize ();
        $(window) .on ("resize", function () {
            wresize ();
        });
    });
    var wresize = function () {
     ~~~
    };

    I personally want to make the load part short.

    $(function () {

  • Answer # 3

    Many people feel that anonymous function expressions are elegant. I don't think so.

    function handleEvent () {}
    jQuery (handleEvent);
    jQuery (window) .on ('resize', handleEvent) .trigger (handleEvent);

    I'm worried thattrigger ()is executed once, andready ()is re-executed.
    If it makes sense to re-execute, it would be nice.

    Re: Mr. Alice0225