踩到Window resize在Preview print的雷

最近在作案的過程中遇到一個狀況,從Art design那邊拿到的頁面,會在Window resize的時候執行某個Function,本來一切都正常,但測試到Chrome和Firefox預覽列印時發現也會Trigger這個Event,會讓在預覽列印過後的頁面發生問題,所以必須要進行修正,以下是這個案例的說明:

先在測試頁加入以下的程式,發現在預覽列印的時候會Trigger window resize event:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Preview Print Test</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>
        $(window).resize(function() {
            console.log("Window Resize");
        });
    </script>
</head>
<body>
<h1>Window Resize Test</h1>
</body>
</html>
Preview print trigger window resize event
Preview print trigger window resize event

網路上搜尋了一下解決方案,將Script改寫如下,可以順利解決,但不知道是不是最好的方法,如果大家有更好的也別忘了告訴我唷~

openResizeFn();

// Chorme
var print = false;
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function (mql) {
    if (mql.matches) {
        closeResizeFn();
    } else {
        setTimeout(openResizeFn, 2000);
    }
});

// Firefox
window.onbeforeprint = function () {
    closeResizeFn();
};
window.onafterprint = function () {
    setTimeout(openResizeFn, 2000);
};

// Common Function
function openResizeFn() {
    $(window).on("resize", windowResize);
}

function closeResizeFn() {
    $(window).off("resize", windowResize);
}

function windowResize() {
    console.log("Window Resize");
}

You may also like...

17,854 Responses

  1. ClairJaw表示:

    navigate to this website MetaMask Download

  2. BradleyEdino表示:

    yeni deneme bonusu veren siteler deneme bonusu veren siteler yeni deneme bonusu veren siteler

  3. вывод из запоя в стационаре [url=www.vyvod-iz-zapoya-sochi24.ru]вывод из запоя в стационаре[/url] .

  4. Baji表示:

    My brother suggested I might like this web site. He was entirely right. This post actually made my day. You can not imagine simply how much time I had spent for this info! Thanks!

  5. Wallacefex表示:

    en guvenilir casino siteleri: guvenilir casino siteleri – Casino Siteleri

  6. FrankEmine表示:

    sweet bonanza oyna: sweet bonanza – sweet bonanza demo oyna

  7. вывод из запоя круглосуточно сочи вывод из запоя круглосуточно сочи .

  8. FrankEmine表示:

    sweet bonanza slot: sweet bonanza oyna – sweet bonanza guncel

  9. вывод из запоя в наркологическом стационаре http://www.vyvod-iz-zapoya-sochi24.ru/ .

  10. JasonUnsoG表示:

    https://denemebonusuverensiteler25.com/# deneme bonusu veren siteler

  11. BradleyEdino表示:

    sweet bonanza slot sweet bonanza slot sweet bonanza kazanma saatleri

  12. JasonUnsoG表示:

    https://sweetbonanza25.com/# sweet bonanza yorumlar

  13. BradleyEdino表示:

    deneme bonusu veren yeni siteler deneme bonusu veren yeni siteler deneme bonusu veren siteler yeni

  14. Wallacefex表示:

    deneme bonusu veren siteler: deneme bonusu veren siteler yeni – deneme bonusu veren yeni siteler

  15. Timsothyabela表示:

    Wow, incredible blog layout! How long have you ever been blogging for? you make running a blog glance easy. The entire glance of your web site is fantastic, let alone the content material!
    https://slovo-pacana.info/

  16. BradleyEdino表示:

    sweet bonanza slot sweet bonanza guncel sweet bonanza

  17. JasonUnsoG表示:

    https://casinositeleri25.com/# guvenilir casino siteleri

  18. Wallacefex表示:

    guvenilir casino siteleri: casino bahis siteleri – Deneme Bonusu Veren Siteler

  19. Wallacefex表示:

    sweet bonanza yorumlar: sweet bonanza oyna – sweet bonanza kazanma saatleri

  20. BradleyEdino表示:

    az parayla cok kazandiran slot oyunlar? en cok kazand?ran slot oyunlar? az parayla cok kazandiran slot oyunlar?

  21. FrankEmine表示:

    deneme bonusu veren casino siteleri: deneme bonusu veren casino siteleri – deneme bonusu veren casino siteleri

  22. tylekeo表示:

    You are not right. I am assured. I can prove it. Write to me in PM, we will talk.

  23. Wallacefex表示:

    sweet bonanza slot: sweet bonanza yorumlar – sweet bonanza demo oyna

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。