踩到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...

16,992 Responses

  1. вывод из запоя на дому ростов-на-дону вывод из запоя на дому ростов-на-дону .

  2. Jamesknowl表示:

    https://canadianpharmacy.win/# canadian pharmacy victoza

  3. Brianchoky表示:

    https://canadianpharmacy.win/# canadadrugpharmacy com

  4. Though it is expensive, varicella zoster immune globulin (VZIG) can protect patients from the virus up to 96 hours after the initial exposure.

  5. Jamesknowl表示:

    https://mexicanpharmacy.store/# best online pharmacies in mexico

  6. I appreciate, cause I found exactly what I was looking for. You’ve ended my four day long hunt! God Bless you man. Have a nice day. Bye

  7. ClaytonGauct表示:

    buying prescription drugs in mexico: mexico pharmacies prescription drugs – mexican border pharmacies shipping to usa

  8. る 活用表表示:

    A daily listed unit of account or Day by day Client Value Index (Daily CPI) might be used in contracts or within the Capital Upkeep in Items of Fixed Buying Energy accounting mannequin, to ensure that deferred payments and constant actual value non-financial items are indexed to the final price stage in terms of a Day by day Index.

  9. LarryHom表示:

    http://indianpharmacy.win/# indian pharmacy

  10. LarryHom表示:

    https://indianpharmacy.win/# india pharmacy

  11. Revan and Malak as a substitute opted to make use of the Star Forge to gas their very own imperial war machine and led a brutal and massively profitable marketing campaign against the Republic.

  12. ClaytonGauct表示:

    india pharmacy: online shopping pharmacy india – best india pharmacy

  13. вывод из запоя цены ростов на дону [url=http://vyvod-iz-zapoya-rostov233.ru/]вывод из запоя цены ростов на дону[/url] .

  14. вывод из запоя ростов и область вывод из запоя ростов и область .

  15. Jamesknowl表示:

    https://indianpharmacy.win/# india pharmacy mail order

  16. LarryHom表示:

    http://indianpharmacy.win/# Online medicine order

  17. But many of these automakers are worth remembering as they built some of the best and most valuable vehicles ever.

  18. Brianchoky表示:

    https://mexicanpharmacy.store/# mexican border pharmacies shipping to usa

  19. Brianchoky表示:

    https://mexicanpharmacy.store/# pharmacies in mexico that ship to usa

  20. вывод из запоя цены ростов-на-дону вывод из запоя цены ростов-на-дону .

  21. ClaytonGauct表示:

    reputable indian pharmacies: п»їlegitimate online pharmacies india – online pharmacy india

  22. Jamestus表示:

    buying prescription drugs in mexico online medicine in mexico pharmacies mexican border pharmacies shipping to usa

  23. вывод из запоя на дому ростов недорого вывод из запоя на дому ростов недорого .

  24. вывод из запоя на дому ростов-на-дону вывод из запоя на дому ростов-на-дону .

發佈留言

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