鍍金池/ 問(wèn)答/網(wǎng)絡(luò)安全  HTML/ 瀏覽器對(duì)非當(dāng)前標(biāo)簽頁(yè)元素的控制問(wèn)題。

瀏覽器對(duì)非當(dāng)前標(biāo)簽頁(yè)元素的控制問(wèn)題。

如下代碼,如果一直置于當(dāng)前標(biāo)簽頁(yè),則一切正常。
但是途中只要切換到別的標(biāo)簽頁(yè)停留數(shù)秒,再切換回來(lái)就崩潰了。
具體什么原理?
(當(dāng)然我知道有很多別的方法可以避免這種情況,不過(guò)很想了解這類問(wèn)題的機(jī)制)

<!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>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        a {
            text-decoration: none;
        }

        .wrapper {
            padding: 10px;
            border: 1px solid #000;
            width: 300px;
            margin: 0 auto;
        }

        .container {
            height: 200px;
            position: relative;
            font-size: 0px;
            overflow: hidden;
        }

        .banner {
            position: absolute;
            width: 1800px;
        }

        .banner a {
            display: inline-block;
            width: 300px;
            height: 200px;
            font-size: 50px;
        }

        .banner a:nth-child(1) {
            background-color: rgb(219, 106, 106);
        }

        .banner a:nth-child(2) {
            background-color: rgb(108, 223, 73);
        }

        .banner a:nth-child(3) {
            background-color: rgb(194, 57, 212);
        }

        .banner a:nth-child(4) {
            background-color: rgb(219, 106, 106);
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <div class="container">
            <div class="banner">
                <a href="#">1</a>
                <a href="#">2</a>
                <a href="#">3</a>
                <a href="#">3(2)</a>
            </div>
        </div>
    </div>
</body>
<script>
    let banner = document.getElementsByClassName('banner')[0],
        unitWidth = 300,
        unitNum = 3

    let move = function (e, ds, callback) {
        e.move = setInterval(function () {
            if (ds > 0) {
                e.style.left = e.offsetLeft + 10 + 'px'
                ds -= 10
            } else if (ds < 0) {
                e.style.left = e.offsetLeft - 10 + 'px'
                ds += 10
            } else {
                clearInterval(e.move)
                callback()
            }
        }, 20)
    }

    let autoStart = function () {
        banner.auto = setInterval(function () {
            move(banner, -300, function () {
                if (banner.offsetLeft === -unitWidth * unitNum) {
                    banner.style.left = 0
                }
            })
        }, 1600)
    }

    autoStart()
</script>

</html>
回答
編輯回答
墨染殤

http://www.it1352.com/341638....

這是一個(gè)瀏覽器認(rèn)為的優(yōu)化的。解決方案是Web Workers?處理這些數(shù)據(jù)

2017年10月2日 13:43
編輯回答
情皺

window.onblur 切換頁(yè)面時(shí)把定時(shí)器清了

2017年10月6日 11:13
編輯回答
久愛(ài)她

幫你優(yōu)化了下,你看下代碼

let banner = document.getElementsByClassName('banner')[0],
        unitWidth = 300,
        unitNum = 3
    var ods;
    let move = function (e, ds, callback) {
        if(ods && ods!=0)return;
        clearTimeout(e.move);
        ods=ds;
        e.move = setInterval(function () {
            if (ods > 0) {
                e.style.left = e.offsetLeft + 10 + 'px'
                ods -= 10
            } else if (ods < 0) {
                e.style.left = e.offsetLeft - 10 + 'px'
                ods += 10
            } else {
                callback();
                clearInterval(e.move);
            }
            // console.log(ds);
        }, 30)
    }

    let autoStart = function () {
        clearInterval(banner.auto);
        // if(Math.abs(banner.offsetLeft)%300!=0)return;
        banner.auto = setInterval(function () {
            move(banner, -300, function () {
                if (banner.offsetLeft <= (-unitWidth * unitNum)) {
                    banner.style.left = 0;
                }
            })
        }, 1600)
    }

    autoStart()

現(xiàn)在JQ或者CSS3已經(jīng)很方便使用了,如果你要快捷開發(fā),就少用點(diǎn)原生JS吧
如果是要理解原理 是沒(méi)什么問(wèn)題

期望你早日解決問(wèn)題~

2018年6月18日 23:21