js实现匀速运动及透明度动画

1.html代码

<body>
  <div id="container">
      <span id="btn"></span>
  </div>
</body>

2.css样式

*{
    margin: 0;
    padding: 0;
}
#container{
    height: 200px;
    width: 200px;
    position: relative;
    left: -200px;
    background-color: #6effcd;
}
#btn{
    height: 60px;
    width: 30px;
    position: absolute;
    left: 200px;
    top: 75px;
    background-color: #73eeff;
}

3.js代码

window.onload = function(){
var container=document.getElementById('container');
var btn=document.getElementById('btn');
//实现透明度动画
 btn.onmouseover = function(){
        moveStart(10,100);
    };
 btn.onmouseout = function(){
        moveStart(-10,30);
    }
//实现匀速移动
btn.onclick = function(){
  if(container.offsetLeft==-200)
  {
      moveOut();
  }
  else{
      moveBack();
  }
};
};
var timer=null;//控制container匀速移动的timer
function moveOut(){
  clearInterval(timer);
  timer=setInterval(function(){
  if(container.offsetLeft == 0)
  {
      clearInterval(timer);
  }
  else{
      container.style.left=container.offsetLeft+10+'px';
  }
  },30);
}

function moveBack(){
  clearInterval(timer);
  timer=setInterval(function(){
  if(container.offsetLeft == -200)
  {
      clearInterval(timer);
  }
  else{
      container.style.left=container.offsetLeft-10+'px';
  }
  },30);
}
var alpha = 50;
var timer1=null;//控制btn透明度动画的timer
function moveStart(oSpeed,oTarget){
    clearInterval(timer1);
    timer1 = setInterval(function(){
    if(oTarget == alpha){
      clearInterval(timer1);
     }
    else{
      alpha += oSpeed;
      btn.style.filter = 'alpha(opacity:'+alpha+');'
      btn.style.opacity = alpha/100.0;
     }
    },30);
}

 

转载于:https://www.cnblogs.com/yingzi1028/p/5313573.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值