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

15,100 Responses

  1. RobertJar表示:

    get clomid without a prescription: clomidonpharm – how to get clomid no prescription

  2. botnet表示:

    A cool post there mate ! Thank you for posting.

  3. SamuelBix表示:

    prednisone 5 mg brand name: PredniBest – can you buy prednisone over the counter in canada

  4. DanielAbice表示:

    http://clomidonpharm.com/# can you get cheap clomid price

  5. botnet表示:

    Cherished is likely to be what people say about your comments.

  6. Prodamus – промокод на подключение https://vip.rolevaya.info/viewtopic.php?id=7107#p1 .

  7. SamuelBix表示:

    buy prednisone tablets uk: prednisone tablet 100 mg – prednisone otc uk

  8. RobertJar表示:

    buy amoxicillin without prescription: buy amoxicillin from canada – generic amoxicillin online

  9. The start of a fast-growing trend?

  10. KennethPah表示:

    buy prednisone without a prescription Predni Best prednisone 5 mg brand name

  11. Jewels And Chains makes it straightforward to find quality silver chains in varied kinds and lengths, perfect for jewellery makers and collectors alike.

  12. SamuelBix表示:

    can i order generic clomid without dr prescription: clomid on pharm – cost of cheap clomid

  13. DanielAbice表示:

    http://cipharmdelivery.com/# buy cipro online usa

  14. RobertJar表示:

    prednisone 2.5 mg daily: Predni Best – where can i get prednisone

  15. DanielAbice表示:

    http://amoxstar.com/# amoxicillin 500mg capsules antibiotic

  16. KennethPah表示:

    get cheap clomid pills can i order clomid online where to buy generic clomid

  17. KennethPah表示:

    buy cipro without rx buy cipro cheap ciprofloxacin order online

  18. SamuelBix表示:

    where to buy prednisone in australia: prednisone 10 mg over the counter – prednisone tablets canada

發佈留言

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