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

  1. Diplomi_xfpt表示:

    купить диплом в когалыме prema-diploms.ru .

  2. BrettTIT表示:

    Buy Tadalafil 20mg: buy cialis online – Cialis 20mg price

  3. Donaldabula表示:

    Viagra without a doctor prescription Canada cheap viagra viagra canada

  4. Donaldabula表示:

    buy erectile dysfunction pills fast pills easy buy ed meds online

  5. неотложная наркологическая помощь в москве неотложная наркологическая помощь в москве .

  6. вызов нарколога на дом частная скорая помощь вызов нарколога на дом частная скорая помощь .

  7. как вызвать наркологическую скорую помощь в москве http://www.cah.forum24.ru/?1-19-0-00000470-000-0-0-1730875154 .

  8. Barrybib表示:

    cheapest cialis: MaxPillsForMen – Buy Cialis online

  9. частная скорая наркологическая помощь http://www.setter.borda.ru/?1-7-0-00000686-000-0-0-1730875306/ .

  10. Donaldabula表示:

    п»їcialis generic Generic Cialis without a doctor prescription cheapest cialis

  11. CharlesFeeta表示:

    We’re thrilled with the results after using strategic sourcing solutions for tailored procurement solutions.

  12. Thomasmeshy表示:

    Using advanced tools for supplier evaluation, we streamlined workflows and enhanced our procurement accuracy.

  13. BrettTIT表示:

    Buy Tadalafil 5mg: buy cialis online – cheapest cialis

  14. Diplomi_ulpt表示:

    купить диплом о среднем образовании в сочи prema-diploms.ru .

  15. 8xbet1880.com表示:

    I really believe you will do well in the future I appreciate everything you have added to my knowledge base.

  16. RogerFaf表示:

    https://maxpillsformen.com/# Buy Cialis online

  17. Diplomi_gypt表示:

    купить диплом геолога prema-diploms.ru .

  18. Barrybib表示:

    Generic Cialis price: MaxPillsForMen – Buy Tadalafil 10mg

  19. Charlesbot表示:

    http://maxpillsformen.com/# Buy Cialis online

  20. Charlesbot表示:

    http://fastpillseasy.com/# best ed meds online

  21. Thomasmeshy表示:

    The integration of supplier risk management into our processes has yielded great results.

發佈留言

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