js用setInterval()做缓冲运动,第一个缓冲能实现效果,第二个为什么还没到达设定条件就已经停止计时了?

js用setInterval()做缓冲运动,第一个缓冲能实现效果,第二个为什么还没到达设定条件就已经停止计时了?

做一个侧边栏的缓冲效果,从左边进入和离开,但是离开的时候感觉没有在及时,LeftBar.style.left直接跳到-300px.

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)

<!DOCTYPE html>
<html lang="en" style="overflow-x: hidden">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    
    <meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<style>
    html,body{margin: 0;padding: 0;overflow-x: hidden}
    .nav-container{width: 100%;height: 90px;background-color: #0abfbf;position: relative}
    .nav-container #tab-bar{width: 60px;height: 60px;border: 1px #2b2c31 solid;text-align: center;position: absolute;display:flex;left: 10px;margin-top: 10px}
    .nav-container #tab-bar span{width: 80%;height: 4px; background-color: #2b2c31;margin-top: 16px;
        display:block;position: absolute;margin-left: 10%;}
    .nav-container #tab-bar .tab-bar-1{top: 0} .nav-container #tab-bar .tab-bar-2{top: 9px} .nav-container #tab-bar .tab-bar-3{top: 18px}

    .left-bar{width: 300px;border: 1px solid #0c6492;display: block;top: 0;left: -300px;right: auto;position: fixed;overflow-y:auto}
    .bar-close{width: 60px;height: 60px;position: relative;right: -220px}
    .bar-close .bar-left, .bar-close .bar-right{position: absolute;width: 80%;margin-left: 10%;height: 3px;background: black}
    .bar-close .bar-left{transform: rotate(45deg);top: 5px}  .bar-close .bar-right{transform: rotate(315deg);top: 5px}
    body{left: 0px;display: block;overflow-y: auto;height: 3000px}
</style>
<body>
<nav class="nav-container">
    <div id="tab-bar">
        <span class="tab-bar-1"></span>
        <span class="tab-bar-2"></span>
        <span class="tab-bar-3"></span>
    </div>
</nav>

<div class="left-bar">
    <ul>
        <li>首页</li>
        <li>Dating Site</li>
        <li>Dating Tips</li>
        <li>Blogs</li>
        <li>Forum</li>
        <li>Sign Up</li>
    </ul>
    <div class="bar-close">
        <span class="bar-left"></span>
        <span class="bar-right"></span>
    </div>
</div>
</body>
<script>


    var LeftBar =document.getElementsByClassName('left-bar')[0]
    var BoDy = document.body

    var  TabBar = document.getElementById('tab-bar')
    var BarClose = document.getElementsByClassName('bar-close')[0]


    TabBar.onclick = function () {

        var timer = setInterval(function () {
            console.log(LeftBar.offsetLeft)
            if(LeftBar.style.left>=0 +"px"){
                clearInterval(timer)
                LeftBar.style.left = 0 + "px"
            }
            else {
                LeftBar.style.left = (2+LeftBar.offsetLeft)/2 + "px"
                BoDy.style.marginLeft = (600+LeftBar.offsetLeft)/2 +"px"
            }

        },100)

    }

    BarClose.onclick = function () {

        var timer = setInterval(function () {
            //console.log(LeftBar.offsetLeft)
            if(LeftBar.style.left<=-300 + "px"){
                clearInterval(timer)
                LeftBar.style.left = -300 + "px"
            }
            else {
                LeftBar.style.left = (-4+LeftBar.offsetLeft)/2 + "px"
                BoDy.style.marginLeft =(2+LeftBar.offsetLeft)/2 +"px"
                console.log(LeftBar.offsetLeft)
            }

        },100)

    }
</script>
</html>

怎样才能实现侧边栏进入和离开的时候都有缓冲运动呢?

阅读 2.1k
4 个回答

两个函数过程相反,应该做逆运算,同时,应该注意边界值。

BarClose.onclick = function () {

    let timer2 = setInterval(function () {     
        if(parseInt(LeftBar.style.left)<=-300 ){
            clearInterval(timer2)
            LeftBar.style.left = -300 + "px"
        }
        else {
            LeftBar.style.left = (-2+LeftBar.offsetLeft)*2 + "px"
            BoDy.style.marginLeft =(6+LeftBar.offsetLeft)*2 +"px"
            console.log(LeftBar.offsetLeft)
        }

    },100)

}

console.log(LeftBar.style.left <= -300 + "px", LeftBar.style.left, LeftBar.style.left <= -300)
可以看到字符串比较,一上来就退出了.测试地址

clipboard.png

简化了一下代码,道理相同,用transition即可有缓冲效果,对body及.left-bar加transition

<!DOCTYPE html>
<html lang="en" style="overflow-x: hidden">
<head>

<meta charset="UTF-8">
<title>Title</title>

<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<style>

html,body{margin: 0;padding: 0;overflow-x: hidden;transition:margin-left 2s ease;}
.nav-container{width: 100%;height: 90px;background-color: #0abfbf;position: relative}
.nav-container #tab-bar{width: 60px;height: 60px;border: 1px #2b2c31 solid;text-align: center;position: absolute;display:flex;left: 10px;margin-top: 10px}
.nav-container #tab-bar span{width: 80%;height: 4px; background-color: #2b2c31;margin-top: 16px;
    display:block;position: absolute;margin-left: 10%;}
.nav-container #tab-bar .tab-bar-1{top: 0} .nav-container #tab-bar .tab-bar-2{top: 9px} .nav-container #tab-bar .tab-bar-3{top: 18px}

.left-bar{width: 300px;border: 1px solid #0c6492;display: block;top: 0;left: -300px;right: auto;position: fixed;overflow-y:auto;transition:left 2s ease;}
.bar-close{width: 60px;height: 60px;position: relative;right: -220px}
.bar-close .bar-left, .bar-close .bar-right{position: absolute;width: 80%;margin-left: 10%;height: 3px;background: black}
.bar-close .bar-left{transform: rotate(45deg);top: 5px}  .bar-close .bar-right{transform: rotate(315deg);top: 5px}
body{left: 0px;display: block;overflow-y: auto;height: 3000px}
</style>
<body>
<nav class="nav-container">

<div id="tab-bar">
    <span class="tab-bar-1"></span>
    <span class="tab-bar-2"></span>
    <span class="tab-bar-3"></span>
</div>
</nav>

<div class="left-bar">

<ul>
    <li>首页</li>
    <li>Dating Site</li>
    <li>Dating Tips</li>
    <li>Blogs</li>
    <li>Forum</li>
    <li>Sign Up</li>
</ul>
<div class="bar-close">
    <span class="bar-left"></span>
    <span class="bar-right"></span>
</div>
</div>
</body>
<script>

var LeftBar =document.getElementsByClassName('left-bar')[0]
var BoDy = document.body

var  TabBar = document.getElementById('tab-bar')
var BarClose = document.getElementsByClassName('bar-close')[0]


TabBar.onclick = function () {

   
            BoDy.style.marginLeft = "300px"
      LeftBar.style.left="0px"



}

BarClose.onclick = function () {
  BoDy.style.marginLeft = "0px"
LeftBar.style.left="-300px"
 
}
</script>
</html>

为什么 为什么要这种来实现---
CSS3过渡、动画;JS的animate函数不能满足你的需要吗?

setInterval与反复使用setTimeout有所不同。
setInterval是指定间隔将函数推入等待队列。
按你的写法每100ms推一次,并不关心前一次推入的函数是否执行,所以你应该使用setTimeout,函数执行时设置新的定时器。
等待执行队列需要等到主线程的逻辑执行完成后才会来访问,如果访问时已有多个setInterval推入的函数,那这些函数是一次性执行的,看起来并没有间隔。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题