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

  1. KennethPah表示:

    buy cipro without rx ciprofloxacin generic buy ciprofloxacin

  2. SamuelBix表示:

    generic amoxicillin online: amoxicillin 500 capsule – where can i buy amoxicillin over the counter

  3. белое вино купить в москве белое вино купить в москве .

  4. RobertJar表示:

    buy cheap prednisone: prednisone 500 mg tablet – iv prednisone

  5. SamuelBix表示:

    prednisone prescription drug: over the counter prednisone cheap – 5 mg prednisone daily

  6. StephenJem表示:

    http://canadianpharmi.com/# erectile dysfunction natural remedies

  7. MatthewHen表示:

    cheapest ed pills Best Canadian online pharmacy soma therapy ed

  8. Ernestzow表示:

    cheapest online pharmacy india: indiapharmi – india pharmacy

  9. Jeffreyvop表示:

    india pharmacy: India pharmacy international – world pharmacy india

  10. кухни по индивидуальным размерам [url=https://mirmebeli777.ru/]кухни по индивидуальным размерам[/url] .

  11. Willardvem表示:

    https://indiapharmi.com/# world pharmacy india

  12. Willardvem表示:

    http://mexicanpharmi.com/# п»їbest mexican online pharmacies

  13. Ernestzow表示:

    mexican pharmaceuticals online: Best online Mexican pharmacy – mexican online pharmacies prescription drugs

  14. Предлагаем услуги профессиональных инженеров офицальной мастерской.
    Еслли вы искали ремонт телевизоров lg рядом, можете посмотреть на сайте: ремонт телевизоров lg адреса
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  15. MatthewHen表示:

    viagra without a doctor prescription Cheapest drug prices Canada ed devices

  16. MatthewHen表示:

    meds online without doctor prescription Canada Pharmacy carprofen without vet prescription

  17. Velas_bySt表示:

    Создайте уютную атмосферу с помощью велас ароматических, советы по выбору аромата, оригинальный подарок – велас ароматическая свеча
    fragancias de velas aromaticas fragancias de velas aromaticas .

  18. 888b表示:

    This is my first time i visit here. I found so many helpful stuff in your website especially its discussion. From the tons of responses on your posts, I guess I am not the only one having all the enjoyment here! keep up the excellent work

  19. MatthewHen表示:

    mexican rx online Best online Mexican pharmacy medicine in mexico pharmacies

  20. StephenJem表示:

    https://mexicanpharmi.com/# mexican online pharmacies prescription drugs

  21. покупку франшизы franshizy32.ru .

發佈留言

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