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

  1. Barrybib表示:

    Cheap Viagra 100mg: buy viagra online – viagra without prescription

  2. Diplomi_blpi表示:

    купить дипломы специалиста купить дипломы специалиста .

  3. Donaldabula表示:

    cheap ed meds online cheapest ed pills cheapest online ed meds

  4. In 1894 it was found that the crew had secretly paid previously Indiana Normal (now Indiana University of Pennsylvania) player, Lawson Fiscus, to play soccer and retained his providers on salary.

  5. вывод из запоя ростов [url=www.vyvod-iz-zapoya-rostov237.ru]вывод из запоя ростов[/url] .

  6. Barrybib表示:

    sildenafil online: Fast Pills For Men – Cheap generic Viagra online

  7. RogerFaf表示:

    http://fastpillseasy.com/# ed doctor online

  8. BrettTIT表示:

    Cheap generic Viagra online: FastPillsForMen.com – buy Viagra over the counter

  9. RogerFaf表示:

    http://fastpillsformen.com/# Viagra Tablet price

  10. Stocks with more liquidity are accepted in the stock markets and guarantees you additional returns.

  11. Donaldabula表示:

    Buy Tadalafil 10mg MaxPillsForMen.com buy cialis pill

  12. вывод из запоя на дому в ростове вывод из запоя на дому в ростове .

  13. As more and more children use the Internet to do homework, play games, and chat with friends, it becomes more important for parents to set rules and limitations to protect their children.

  14. вывод из запоя на дому ростов-на-дону вывод из запоя на дому ростов-на-дону .

  15. レズ mmd表示:

    In case you keep away from the kitchen in the sweltering summer season heat, make use of cupcake liners in a unique means with this DIY that won’t have you breaking a sweat.

  16. BrettTIT表示:

    cheap ed drugs: ed doctor online – where to buy erectile dysfunction pills

  17. Effectively, if you are a pc kind, you are going to wish the following outrageously expensive books.

  18. BrettTIT表示:

    ed medication online: fast pills easy – buying erectile dysfunction pills online

  19. Barrybib表示:

    ed med online: cheapest erectile dysfunction pills – cheapest ed treatment

  20. Barrybib表示:

    Generic Cialis without a doctor prescription: buy cialis online – Buy Tadalafil 5mg

  21. Donaldabula表示:

    online ed pills cheap ed treatment ed medications online

發佈留言

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