javascript

该篇博客通过JavaScript详细讲解了如何实现小鸟从左往右、从右往左飞行的效果,包括限制飞行范围、解决速度过快问题。同时,博主对代码进行了封装,创建了通用的移动函数。文章涵盖了定时器的使用、事件处理和CSS定位等前端开发技术。
摘要由CSDN通过智能技术生成

2.小鸟起飞案例

==js中如果涉及到元素移动 建议大家使用定位==

2.1开始起飞(从左往右飞)

  • 实现步骤

    • 点击按钮 改变img的left值

    • left值 = 在当前left值的基础上 + 10

    • 开启定时器 自动移动图片位置

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./common.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
​
        img {
            width: 200px;
            height: 200px;
            position: absolute;
            left: 0px;
        }
    </style>
</head>
​
<body>
    <button>开始飞</button>
    <br>
    <img src="./img/right.gif" alt="">
    <script>
        // 1.点击一下  向右移动10px
        var btn = document.getElementsByTagName("button")[0];
        var oImg = document.getElementsByTagName("img")[0];
        // 2.点击事件
        btn.onclick = function () {
​
            setInterval(function () {
                // 在left值的基础上+10    先获取到left值
                var current = parseInt(getStyle(oImg, "left")) + 10
                oImg.style.left = current + "px";
            }, 50)
​
        }
    </script>
​
</body>
  • 问题1 一直飞 飞出屏幕

    • 解决: 限制飞的范围 当飞到1000px的手 停止定时器

// 2.点击事件
        btn.onclick = function () {
​
           var timer =  setInterval(function () {
                // 在left值的基础上+10    先获取到left值
                var current = parseInt(getStyle(oImg, "left")) + 10
                if(current>=1000){
                    current = 1000;
                    // 停止定时器
                    clearInterval(timer)
                }
                console.log(current);
                oImg.style.left = current + "px";
            }, 50)
​
        }
  • 问题2 多次点击 速度越来愉快

    • 分析原因: 每次点击都会定义一个定时器 多次点击会导致定时器累加 会累加执行

    • 解决方法:每次点击的时候 先把原先的定时器清除掉

<script>
        // 1.点击一下  向右移动10px
        var btn = document.getElementsByTagName("button")[0];
        var oImg = document.getElementsByTagName("img")[0];
        
        var timer;
        // 2.点击事件
        btn.onclick = function () {
            clearInterval(timer);// timer这里存储的就是定时器id  先把原先的定时器清除掉
            timer =  setInterval(function () {
                // 在left值的基础上+10    先获取到left值
                var current = parseInt(getStyle(oImg, "left")) + 10
                if(current>=1000){
                    current = 1000;
                    // 停止定时器
                    clearInterval(timer)
                }
                console.log(current);
                oImg.style.left = current + "px";
            }, 50)
        }
    </script>

2.2来回飞(从右往左飞)

  • 实现步骤

    • 在left值的基础上 -10

  • 问题1:解决目标值的问题 飞到0就停止

 // 2.往回飞
        btn[1].onclick = function () {
            // 改变图片的路径
            oImg.src = "./img/left.gif";
          
            timer =  setInterval(function () {
                // 在当前left值的基础上-10
                var current = parseInt(getStyle(oImg, "left")) - 10;
                //判断目标值
                if(current<=0){
                    current=0;
                    clearInterval(timer);
                }
                oImg.style.left = current + "px"
            }, 50)
​
        }

  • 问题1 如果点击开始起飞 但是飞到一半就返回 和越飞越快的问题

  • 解决方法 让开始起飞和结束起飞共用一个定时器 每次点击的时候 都清除定时器

<script>
        // 1.点击一下  向右移动10px
        var btn = document.getElementsByTagName("button");
        var oImg = document.getElementsByTagName("img")[0];
​
        var timer;
        // 2.开始飞
        btn[0].onclick = function () {
            // 改变图片路径
            oImg.src = "./img/right.gif";
            clearInterval(timer);// timer这里存储的就是定时器id  先把原先的定时器清除掉
            timer = setInterval(function () {
                // 在left值的基础上+10    先获取到left值
                var current = parseInt(getStyle(oImg, "left")) + 10
                if (current >= 1000) {
                    current = 1000;
                    // 停止定时器
                    clearInterval(timer)
                }
                console.log(current);
                oImg.style.left = current + "px";
            }, 50)
        }
​
        // 2.往回飞
        btn[1].onclick = function () {
            // 改变图片的路径
            oImg.src = "./img/left.gif";
            clearInterval(timer);
            timer =  setInterval(function () {
                // 在当前left值的基础上-10
                var current = parseInt(getStyle(oImg, "left")) - 10
                if(current<=0){
                    current=0;
                    clearInterval(timer);
                }
                oImg.style.left = current + "px"
            }, 50)
​
        }
    </script>

2.3小鸟起飞函数封装

2.3.1基础封装

<body>
    <button>开始飞</button>
    <button>往回飞</button>
    <br>
    <img src="./img/right.gif" alt="">
    <script>
        // 1.点击一下  向右移动10px
        var btn = document.getElementsByTagName("button");
        var oImg = document.getElementsByTagName("img")[0];
​
        var timer;
        // 2.开始飞
        btn[0].onclick = function () {
            // 改变图片路径
            oImg.src = "./img/right.gif";
            move(oImg, "left", 10, 1000)
​
        }
​
        // 2.往回飞
        btn[1].onclick = function () {
            // 改变图片的路径
            oImg.src = "./img/left.gif";
            move(oImg, "left", -10, 0)
        }
        // 3.基础封装
        /* 
                封装的步骤
                    1.先声明一个函数  将主要代码放入到函数中
                    2.找函数中可变的值   作为函数参数代入到函数中
                    3.调用调试
        */
​
        function move(elem, attr, step, target) {
            /* 
               elem  操作的元素
               attr  属性
               step  步长  可以是正数  也可以是负数   +  10   + -10
               target  目标值
            */
            clearInterval(timer);// timer这里存储的就是定时器id  先把原先的定时器清除掉
            timer = setInterval(function () {
                // 在left值的基础上+10    先获取到left值
                var current = parseInt(getStyle(elem, attr)) + step
​
                if(current>=target){
                  current = target;
                    // 停止定时器
                    clearInterval(timer)
                }
                console.log(current);
                elem.style[attr] = current + "px";
            }, 50)
        }
    </script>
​
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值