angular--setInterval() 方法实现淡入淡出

本文介绍了如何在Angular应用中使用setInterval()方法来创建动态的淡入淡出效果,通过定时改变元素的透明度,实现平滑的动画过渡。
$scope.mTouch=function(c){
  $scope.showMiddle=true;
  $scope.hint=c;
  $scope.testc=c;
  var timer1=null;
  var timer2=null;
  var alpha=0;//透明度默认0
  var speed=5;
  var oDiv=document.getElementById("div1");
  clearInterval(timer1);
  clearInterval(timer2);
  oDiv.style.opacity=0;

  // 显示字母
  timer1=setInterval(function(){
      if(alpha<100){
          console.log('ccc');
          alpha+=speed;
          oDiv.style.filter="alpha(opacity:"+alpha+")";
          oDiv.style.opacity=alpha/100;
      }else if(alpha===100){
          console.log('aaa');
          clearInterval(timer1);
          $timeout(function(){
              timer2=setInterval(function(){
                  if(alpha>0){
                      speed=5;
                      console.log('ddd');
                      alpha-=speed;
                      oDiv.style.filter="alpha(opacity:"+alpha+")";
                      oDiv.style.opacity=alpha/100;
                  }else if(alpha<=0){
                      console.log('bbb');
                      $scope.showMiddle=false;
                      clearInterval(timer2);
                  }
              },30);
          },100);

      }
  },30);
  }
 <div style="
    position: fixed;
    left: 35%;
    top: 32%;
    width: 95px;
    height: 70px;
    background: rgb(221, 221, 221);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 40px;
    font-weight: 700;
    color: rgb(136, 136, 136);
    opacity: 0;
    border-radius: 10px;
" id="div1" ng-show="showMiddle">
       <!--ng-show="showMiddle">-->
    {{hint}}
  </div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值