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

  1. продамус промокод https://promokod-prod.ru .

  2. Josephnor表示:

    kumar oynama siteleri: bilinmeyen siteler – bahis siteleri slot oyunlarД±

  3. RobertPex表示:

    пин ап казино пин ап казино пин ап

  4. Justinlib表示:

    http://casinositeleri2025.pro/# en Г§ok kazandД±ran site

  5. RobertPex表示:

    en yeni kaçak bahis siteleri orjinal siteler deneme bonusu veren güvenilir siteler

  6. Justinlib表示:

    https://casinositeleri2025.pro/# tГјrkiye nin en iyi yasal bahis sitesi

  7. TravisFlasp表示:

    en cok kazand?ran slot oyunlar?: en cok kazand?ran slot oyunlar? – slot oyunlar?

  8. EdwardApest表示:

    https://pinup2025.com/# pinup 2025

  9. Justinlib表示:

    http://slottr.top/# en cok kazand?ran slot oyunlar?

  10. Josephnor表示:

    пинап казино: pinup 2025 – пин ап казино официальный сайт

  11. The clarity in your post is just nice and I can tell you are an expert in the subject matter.

  12. We knew we would should wean gamers from traditional puzzle/solution considering and present them that Deus Ex was a game of problems (not puzzles!), all solvable in a variety of how.

  13. EdwardApest表示:

    https://pinup2025.com/# пин ап зеркало

  14. RobertPex表示:

    son bahis gГјncel giriЕџ en iyi bahis sitesi hangisi ilk Гјyelik deneme bonusu veren siteler

  15. Winter days are often mild, but most years have at the very least in the future (common 3.2) where the excessive remains at or beneath freezing.

  16. A extreme thunderstorm watch was put into effect in the area simply after 10 a.m., as warm, humid temperatures further destabilized the squall line.

  17. Josephnor表示:

    deneme bonusu veren siteler yorumlar: casino en Г§ok kazandД±ran oyunlar – betboo plus

  18. Justinlib表示:

    http://pinup2025.com/# пин ап казино зеркало

  19. Justinlib表示:

    https://pinup2025.com/# пинап казино

  20. It was hosted by You Hee-yeol, Yuri of Girls’ Era, and Baekhyun of Exo.

發佈留言

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