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

  1. If most people wrote about this subject with the eloquence that you just did, I’m sure people would do much more than just read, they act. Great stuff here. Please keep it up.

  2. Eliasskype表示:

    vindecoder.expert free cadillac vin decoder

  3. qh88表示:

    Some truly nice stuff on this website , I like it.

  4. Donaldabula表示:

    Buy Cialis online MaxPillsForMen Cialis without a doctor prescription

  5. BrettTIT表示:

    cheap ed meds online: FastPillsEasy – low cost ed meds

  6. BrettTIT表示:

    Sildenafil 100mg price: FastPillsForMen.com – Cheap generic Viagra online

  7. 債権意味表示:

    Washington ordered each generals to hitch him, but Gates was delayed by heavy snows in transit to McConkey’s Ferry, and Lee, who did not have a excessive opinion of Washington, delayed following repeated orders, and remained on the British flank near Morristown.

  8. вызов нарколога на дом частная скорая помощь [url=http://www.ya.7bb.ru/viewtopic.php?id=14638]http://www.ya.7bb.ru/viewtopic.php?id=14638[/url] .

  9. Diplomi_phml表示:

    купить диплом колледжа в санкт петербурге купить диплом колледжа в санкт петербурге .

  10. Barrybib表示:

    generic ed meds online: FastPillsEasy – п»їed pills online

  11. BrettTIT表示:

    cheap viagra: FastPillsForMen – viagra without prescription

  12. RogerFaf表示:

    https://fastpillseasy.com/# ed meds online

  13. RogerFaf表示:

    http://maxpillsformen.com/# cheapest cialis

  14. Charlesbot表示:

    http://maxpillsformen.com/# cheapest cialis

  15. Donaldabula表示:

    Cheap Viagra 100mg buy viagra online viagra canada

  16. Barrybib表示:

    Buy Tadalafil 20mg: MaxPillsForMen – Buy Tadalafil 5mg

  17. Donaldabula表示:

    order viagra FastPillsForMen.com Viagra online price

  18. вызвать наркологическую помощь [url=www.superjackson.ukrbb.net/viewtopic.php?f=6&t=9765]вызвать наркологическую помощь[/url] .

  19. Diplomi_ybml表示:

    купить диплом о среднем специальном образовании в москве купить диплом о среднем специальном образовании в москве .

  20. Donaldabula表示:

    Buy Tadalafil 10mg Generic Cialis without a doctor prescription Tadalafil Tablet

  21. BrettTIT表示:

    online ed drugs: cheap cialis – buy erectile dysfunction pills

發佈留言

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