<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        .box{
            position:absolute;
            top:100px;
            left:0;
            width:100px;
            height:100px;
            background-color:skyblue;
        }

        input{
            height: 50px;
        }

    </style>
</head>
<body>
<br>
    <input type="button" value="开始将下面的元素向右移动" id="start">
    <input type="button" value="点击停止元素的移动" id="ttt">
    <div class="box" id="box">

    </div>

    <script>
        var start = document.getElementById("start");
        var box = document.getElementById("box");
        var ttt = document.getElementById("ttt");
        //点击开始按钮,让box向右运动
        //全局变量存储left的属性值,会每次发生变化
        //信号量,初始值必须与属性初始值保持一致
        var nowleft = 0;
        var temer;
        //定时器,制作运动过程
start.onclick = function(){
    //先清除定时器
        clearInterval(temer);
        //开启定时器
        temer = setInterval(function(){
            //信号量进行自加
            nowleft += 5;
            //将最新的nowleft的值赋值给元素的css属性
            box.style.left = nowleft + "px";


        },100);
        
    };

    ttt.onclick = function(){
        clearInterval(temer);
    };


    </script>
    
</body>
</html>