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

  1. New 88表示:

    It’s continually awesome when you can not only be informed, but also entertained! I’m sure you had fun writing this article. Regards, Clotilde.

  2. Barrybib表示:

    buy viagra here: Generic Viagra for sale – Sildenafil 100mg price

  3. Fatalities continue to be reported to the CDC.

  4. This blog post is excellent, probably because of how well the subject was developed. I like some of the comments too.

  5. Barrybib表示:

    Cialis over the counter: buy cialis online – Buy Tadalafil 10mg

  6. Donaldabula表示:

    Cialis 20mg price in USA buy cialis online Cheap Cialis

  7. Donaldabula表示:

    best ed pills online FastPillsEasy buy ed meds

  8. Charlesbot表示:

    http://fastpillseasy.com/# online prescription for ed

  9. Charlesbot表示:

    http://fastpillseasy.com/# where can i get ed pills

  10. Accountants may fit at small operations or at massive corporations, serving to to ensure their shoppers are making good monetary choices, maintaining track of expenditures and paying their fair proportion of taxes.

  11. BrettTIT表示:

    generic sildenafil: Fast Pills For Men – Generic Viagra for sale

  12. Charlesbot表示:

    http://fastpillsformen.com/# sildenafil 50 mg price

  13. New 88表示:

    Your idea is outstanding; the issue is something that not enough persons are speaking intelligently about. I’m very happy that I stumbled throughout this in my seek for one thing regarding this.

  14. I’d like to be able to write like this, but taking the time and developing articles is hard…. Takes a lot of effort.

  15. Donaldabula表示:

    Generic Cialis price Max Pills For Men Tadalafil Tablet

  16. Diplomi_egpi表示:

    купить диплом с проверкой 4russkiy365-diplomy.ru .

  17. RogerFaf表示:

    https://fastpillsformen.com/# buy Viagra over the counter

  18. RogerFaf表示:

    https://fastpillseasy.com/# discount ed pills

  19. Part of the artwork of being a pawnbroker is having a sixth sense about how much things are price each new and used.

  20. BrettTIT表示:

    Generic Tadalafil 20mg price: MaxPillsForMen.com – cialis for sale

  21. BrettTIT表示:

    best price for viagra 100mg: FastPillsForMen.com – sildenafil over the counter

  22. He’s proven standing on a ship, symbolically representing the crossing.

  23. Diplomi_mbpi表示:

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

發佈留言

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