有关运动例子

1.分享菜单案例
在这里插入图片描述

/*
              鼠标移入
                  -100 => 0
              鼠标移出
                  0 => -100
            */
           var oDiv = document.getElementById("div1");
           oDiv.onmouseover = function(){
               startMove();

           }
           oDiv.onmouseout = function(){
            startMove2();

           }
           var timer = null;
           function startMove(){
            var oDiv = document.getElementById("div1");
            var speed = 5;
            clearInterval(timer);
            timer = setInterval(function(){
                //运动和停止
                if(oDiv.offsetLeft==0){
                    clearInterval(timer);
                }else{
                    oDiv.style.left = oDiv.offsetLeft + speed + "px";
                }

            },30);

           }

           function startMove2(){
            var oDiv = document.getElementById("div1");
            var speed = -5;
            clearInterval(timer);
            timer = setInterval(function(){
                //运动和停止
                if(oDiv.offsetLeft==-100){
                    clearInterval(timer);
                }else{
                    oDiv.style.left = oDiv.offsetLeft + speed + "px";
                }

            },30);

           }

相同代码合并

 /*
              鼠标移入
                  -100 => 0
              鼠标移出
                  0 => -100
            */
           var oDiv = document.getElementById("div1");
           oDiv.onmouseover = function(){
               startMove(0);

           }
           oDiv.onmouseout = function(){
            startMove(-100);

           }
           var timer = null;
           function startMove(iTag){
            var oDiv = document.getElementById("div1");
            var speed = 5;
            //判断当前值距离与目的值
            if(oDiv.offsetLeft>iTag){
                speed = -Math.abs(speed);
            }else {
                speed = Math.abs(speed);
            }
            clearInterval(timer);
            timer = setInterval(function(){
                //运动和停止
                if(oDiv.offsetLeft==iTag){
                    clearInterval(timer);
                }else{
                    oDiv.style.left = oDiv.offsetLeft + speed + "px";
                }

            },30);

           }

2、淡入淡出效果
在这里插入图片描述

var oImg = document.getElementById("img1");
           oImg.onmouseover = function(){
              
               //30 => 100
               startMove(100);
           }
           oImg.onmouseout = function(){
               //100 => 30
               startMove(30);
           }
           //设置一个中间变量为透明度的当前值
           var alpha = 30;
           function startMove(iTarget){
               var oImg = document.getElementById("img1");
               var timer = null;
               clearInterval(timer);
               var speed = 2;
               speed = iTarget > alpha ? Math.abs(speed):-Math.abs(speed);
               timer = setInterval(function(){
                   if(iTarget==alpha){
                       clearInterval(timer);
                   }else{
                       alpha += speed;
                       //重新设置透明度
                       oImg.style.opacity = alpha / 100;
                      oImg.style.filter = `alpha(opacity=${alpha})`;

                   }
                   

               },30)
           }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值