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

5,620 Responses

  1. MichaelPieno表示:

    when will viagra be generic viagra discount where to buy viagra

  2. Augdeu表示:

    purchase doxycycline pills – buy doxycycline 200mg without prescription tadalafil 5mg canada

  3. MichaelPieno表示:

    viagra from canada viagra online usa mexican viagra

  4. EddieLip表示:

    stromectol tablets for humans ivermectin 6 tablet ivermectin poisoning in humans

  5. Ougqgi表示:

    stromectol brand – stromectol brand name levitra 20mg tablet

  6. EddieLip表示:

    injectible ivermectin stromectol for humans for sale stromectol for humans for sale

  7. EddieLip表示:

    stromectol 12 mg tablets ivermectin scabies treatment stromectol 12 mg tablets

  8. mnvjteif表示:

    provigil pill modafinil 200mg us

  9. Pblgnv表示:

    buy amoxicillin 250mg online – amoxicillin 500mg generic levitra 10mg uk

  10. EddieLip表示:

    stromectol for sale stromectol for humans for sale stromectol for sale

  11. razrovbe表示:

    order modafinil 100mg generic buy modafinil pills modafinil 100mg pill

  12. Johnnyden表示:

    viagra viagra where to buy sildenafil

  13. yxtdnxru表示:

    order modafinil 100mg online cheap order generic modafinil 200mg oral modafinil 100mg

  14. kmyngitl表示:

    provigil 200mg usa provigil 200mg generic provigil 200mg pill

  15. Johnnyden表示:

    clomid clomid clomid tablets

  16. styymmyn表示:

    provigil 100mg without prescription provigil 200mg for sale modafinil without prescription

  17. jjrpuyxr表示:

    cost provigil 200mg purchase modafinil without prescription generic provigil

發佈回覆給「iwalhpug」的留言 取消回覆

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