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

  1. Younger puppies and older, weaker dogs have the next tendency to develop the sickness as properly.

  2. In preparation for your return trip, you simply apply the same precept however in reverse, going to mattress later rather than earlier.

  3. Xzeupc表示:

    how to get prednisone without a prescription – order prednisone 20mg pills buy capoten online cheap

  4. Purchase some thick, heavy curtains that can block out all incoming light and establish a colour scheme for your house theater.

  5. Voyager 1 would then investigate Saturn, particularly the rings, as well as the moon Titan.

  6. снятие ломки цены [url=https://kvitka.ukrbb.net/viewtopic.php?f=58&t=28106]снятие ломки цены[/url] .

  7. Разбавленные [url=www.honey.ukrbb.net/viewtopic.php?f=45&t=16763/]Разбавленные[/url] .

  8. ペコス表示:

    Letterman’s system was profitable at each the Battle of Fredericksburg and the Battle of Gettysburg, where hundreds of wounded troopers’ lives were saved.

  9. April 18, 20 and may 3, 1990: Cruse, Steve (April 17, 1990).

  10. While you enter your login and password, the phishers take them and use them on the real firm Internet site.

  11. You can even create a tranquil atmosphere in your meditation area by incorporating giant amethyst clusters and using them to harness the stone鈥檚 calming power.

  12. I like this weblog very much so much great info .

  13. In case your finances won’t go quite that far, painted partitions, large slate tiles, and distressed oak (or another wooden with a pronounced grain) can be utilized for a similar impact if the workmanship is nice.

發佈留言

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