How to perform the initialization of the plugin (JS, jQuery code), with a screen larger than 768px?

This is how the problem was solved, but this only works if you move the screen with the browser, but I need it to act immediately, and not resize the screen (try to explain it to the user =)

window.onresize = function () { if (window.innerHeight >= 768) { // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ var $container = $('#container'); $container.masonry({ columnWidth: 232.5, itemSelector: '.item' }); } }; 

    4 answers 4

    It is necessary to call the function immediately + hang up its execution on the resize event.

     (checkAndRepaint)(); window.onresize = checkAndRepaint; function checkAndRepaint() { var $container = $('#container'); if (window.innerHeight >= 768) { // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ $container.masonry({ columnWidth: 232.5, itemSelector: '.item' }); } else { // Π£Π΄Π°Π»Π΅Π½ΠΈΠ΅ $container.masonry('destroy'); } }; 

      Use the standard jquery height function.

       if ($(window).height() >= 768) { // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ var $container = $('#container'); $container.masonry({ columnWidth: 232.5, itemSelector: '.item' }); } 
         window.onload = function () { if (window.innerHeight >= 768) { // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ var $container = $('#container'); $container.masonry({ columnWidth: 232.5, itemSelector: '.item' }); } }; 
           function functionName() { if (window.innerHeight >= 768) { // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ var $container = $('#container'); $container.masonry({ columnWidth: 232.5, itemSelector: '.item' }); } }; window.onload = functionName; // выполнится ΠΏΡ€ΠΈ Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠ΅ window.onresize = functionName; // выполнится ΠΏΡ€ΠΈ рСсайзС 
          • The function thus assigned will be executed twice when the page is loaded, and will not be executed upon resize. - Sasha Omelchenko
          • one
            @SashaOmelchenko, I repaired - vp_arth
          • As if 、 window.onload 、 is responsible for running when loading the station ... one of the options, but it works for me. - Constantine Tretyakoff
          • "will be executed twice at boot" and have you tried? Handlers hanged for different events - Constantine Tretyakoff