踩到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,403 Responses

  1. Jam Jenny表示:

    Very wonderful post. Thanks for sharing it. Wonderful & very informative blog. Thanks for sharing such a informative and useful post. https://www.vivaindia.com.co/

  2. Adding value to the conversation in a way that’s as engaging as a flirtatious wink. Can’t wait to hear more.

  3. Brilliant writing! You’ve captured the essence perfectly, much like a photographer captures a stunning landscape.

  4. I loved The fresh take on this topic. The points resonated with me deeply.

  5. JasonUnsoG表示:

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

  6. Inge Santulli表示:

    Unique viewpoints, because who needs echo chambers?

  7. Beautifully written and incredibly informative, The post has captured my attention as if it were a love letter written just for me.

  8. http://www.airlines-inform.ru/personal/user/?UID=73969 [url=https://crimea.narutorpg.ru/viewtopic.php?id=75#p75]https://crimea.narutorpg.ru/viewtopic.php?id=75#p75[/url] .

  9. The grace and authority with which you handle topics are awe-inspiring. I always come away with new knowledge.

  10. FrankEmine表示:

    slot oyunlar? puf noktalar?: az parayla cok kazandiran slot oyunlar? – en cok kazand?ran slot oyunlar?

  11. Uodier表示:

    accutane 20mg drug – order linezolid linezolid 600 mg over the counter

  12. JasonUnsoG表示:

    http://casinositeleri25.com/# canl? casino siteleri

  13. Wallacefex表示:

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

  14. BradleyEdino表示:

    en guvenilir casino siteleri en guvenilir casino siteleri guvenilir casino siteleri

  15. stamp making online [url=http://www.stamp-creator-online0.com]stamp making online[/url] .

  16. Wallacefex表示:

    canl? casino siteleri: Casino Siteleri – guvenilir casino siteleri

  17. BradleyEdino表示:

    sweet bonanza oyna sweet bonanza sweet bonanza giris

  18. FrankEmine表示:

    Canl? Casino Siteleri: guvenilir casino siteleri – en guvenilir casino siteleri

  19. BradleyEdino表示:

    deneme bonusu veren casino siteleri deneme bonusu veren casino siteleri Гјcretsiz deneme bonusu veren bahis siteleri

  20. Wallacefex表示:

    casino bahis siteleri: Casino Siteleri – guvenilir casino siteleri

  21. JasonUnsoG表示:

    http://casinositeleri25.com/# Deneme Bonusu Veren Siteler

  22. FrankEmine表示:

    Deneme Bonusu Veren Siteler: Deneme Bonusu Veren Siteler – betler

發佈留言

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