ES5新增的迭代函数

一  遍历分为两步

1.循环

 2.循环中执行相应功能

  let arr = [5,6,7,4,8,3,9];

    for(let i=0; i<arr.length; i++){
        arr[i] += 10;
        console.log(arr[i]);
    }

数组迭代函数:帮我们把循环实现了,我们只需要写功能

二 \方法

forEach

    功能:遍历数组

    参数:arr.forEach(回调函数)

    回调函数: function 回调函数(元素的数值,[元素的下标],[数组名])

    返回值:无

map

    功能:遍历数组

    参数:arr.map(回调函数)

    回调函数: function 回调函数(元素的数值,[元素的下标],[数组名])

    返回值:在回调函数添加return,返回一个新数组

 filter

    功能:过滤,根据回调函数返回的布尔值,决定元素是否被存入新的数组

    参数arr.filter(回调函数)

    回调函数: function 回调函数(元素的数值,[元素的下标],[数组名])

    返回值:根据回调函数返回的布尔值,决定元素是否被存入新的数组

三   `匀速运动

 运动原理:

    通过定时器改变元素的left或者top(width,heigth)

    1.先清空定时器,避免运动混乱

    2.计算速度,叠加位移(宽高)

    3.达到条件终止定时器

<style>
        *{
            margin: 0;
            padding: 0;
        }
        #box{
            width: 100px;
            height: 100px;
            position: absolute;
            background-color: hotpink;
            top:100px;
        }
    </style>
</head>
<body>
    <button>启动</button>
    <div id="box"></div>
</body>
</html>
<script>
    let oBox = document.querySelector("#box");
    let oBtn = document.querySelector("button");
    let time;
    oBtn.onclick = function(){
        clearInterval(time);
        time = setInterval(() => {
            oBox.style.left = oBox.offsetLeft + 5 + "px";
            if(oBox.offsetLeft == 800){
                clearInterval(time);
            }
        }, 50);
    }   
</script>

四,.运动函数的封装

<style>
        *{
            margin: 0;
            padding: 0;
        }
        #box{
            width: 100px;
            height: 100px;
            position: absolute;
            background-color: hotpink;
            top:100px;
        }
        #box1{
            width: 100px;
            height: 100px;
            position: absolute;
            background-color: skyblue;
            top:300px;
        }
    </style>
</head>
<body>
    <button>启动</button>
    <div id="box"></div>
    <div id="box1"></div>
</body>
</html>
<script>
    let oBox = document.querySelector("#box");
    let oBox1 = document.querySelector("#box1");
    let oBtn = document.querySelector("button");
    let time;
    //封装函数
    function startMove(obj){
        clearInterval(time);
        time = setInterval(() => {
            obj.style.left = obj.offsetLeft + 5 + "px";
            if(obj.offsetLeft == 800){
                clearInterval(time);
            }
        }, 50);
    }
//调用函数
    oBtn.onclick = function(){
        startMove(oBox1);
    }   
</script>

五~速往返运动

<style>
        *{
            margin: 0;
            padding: 0;
        }
        #box{
            width: 100px;
            height: 100px;
            position: absolute;
            background-color: hotpink;
            top:100px;
            left:400px;
        }
    </style>
</head>
<body>
    <button><<<<<<<</button>
    <button>>>>>>>></button>
    <div id="box"></div>
</body>
</html>
<script>
    let oBox = document.querySelector("#box");
    let oBtn = document.querySelectorAll("button");
    let time;
//函数的封装
    function startMove(obj,target){
        clearInterval(time);
        time = setInterval(()=>{
            let speed = target>obj.offsetLeft?5:-5;
            obj.style.left = obj.offsetLeft + speed + "px";
            if(obj.offsetLeft == target){
                clearInterval(time);
            }
        },50)
    }
    //函数的调用
    oBtn[0].onclick = function(){
        startMove(oBox,0);
    }   
    //函数的调用
    oBtn[1].onclick = function(){
        startMove(oBox,800);
    }  
</script>

六 `透明运动函数

<style>
        #box{
            width: 100px;
            height: 100px;
            position: absolute;
            background-color: red;
            top:100px;
            left:200px;
            opacity: 0.5;
        }
    </style>
</head>
<body>
    <div id="box"></div>
</body>
</html>
<script>
    let oBox = document.querySelector("#box");
    let time;
    //函数的封装
    function startMove(obj,target){
        clearInterval(time);
        time = setInterval(()=>{
            let speed = target > getComputedStyle(obj,false)["opacity"]?0.01:-0.01;
            obj.style.opacity = getComputedStyle(obj,false)["opacity"]/1 + speed;
            if(getComputedStyle(obj,false)["opacity"] == target){
                clearInterval(time);
            }
        },50);
    }
    //函数的调用
    oBox.onmouseover = function(){
        startMove(oBox,1);
    }
//函数的调用
    oBox.onmouseout = function(){
        startMove(oBox,0);
    }
</script>

七 缓冲运动

<style>
        *{
            margin: 0;
            padding: 0;
        }
        #box{
            width: 100px;
            height: 100px;
            position: absolute;
            background-color: hotpink;
            top:100px;
        }
    </style>
</head>
<body>
    <button>启动</button>
    <div id="box"></div>
</body>
</html>
<script>
    let oBox = document.querySelector("#box");
    let oBtn = document.querySelector("button");
    let time;
    //函数的封装
    function startMove(obj,target){
        clearInterval(time);
        time = setInterval(() => {
            //核心算法
            let speed = (target-obj.offsetLeft)/10;
            speed = Math.ceil(speed);
            obj.style.left = obj.offsetLeft + speed + "px";
            if(obj.offsetLeft == target){
                clearInterval(time);
            }
        }, 50);
    }
//函数的调用
    oBtn.onclick = function(){
        startMove(oBox,800);
    }   
</script>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是打工人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值