• css3动画添加间隔


    因项目需要,需要在元素上实现动画效果,并且需要有动画间隔。坑爹的是animation-delay只有在第一次动画开始的时候才起效。

    在网上找了很多方法,最终的方法基本都是改动画规则,比如

    @keyframes move{
           /*  此处从75%开始 */
          75%{ transform: translateX(0px);}
          100%{ transform: translateX(100px);}
    }

    但是项目的特殊性在于元素都是组件类型的,意思就是给每个元素写单个的动画效果是不现实的。因为动画效果是通用的,不能因为某个元素就更改。

    css的方法走不通,就只有走js了,我就想到了用定时器,核心思想就是根据动画时间和间隔时间对组件的style.animation属性进行更改

    代码如下

    function  getStyle (item) {
         // item解释:这是个包含组件的style信息的对象,属性就是style的各个属性,里面还有一个id,id就是我要设置的组件id
        
          let nowStyle = item.style;
          let nowId = item.id;
          let nowStyleObj = {}
          let nowAnimationStr = ‘‘
          nowAnimationStr = nowStyle.code + ‘ ‘ + nowStyle.duration + ‘s ‘ + nowStyle.timingFunction + ‘ ‘ + nowStyle.iterationCount + ‘ ‘ + nowStyle.direction;// 拼出animation属性字符串
          nowStyleObj = {
            animation: nowAnimationStr
          }
          if (nowStyle.interval) {//先判断是否需要间隔
            setTimeout(function () {
              document.getElementById(nowId).parentElement.style.animation = ‘‘
              setTimeout(function () {
                document.getElementById(nowId).parentElement.style.animation = nowAnimationStr
              }, nowStyle.interval * 1000)
            }, nowStyle.duration * 1000)
            setTimeout(function () {
              setInterval(function () {
                document.getElementById(nowId).parentElement.style.animation = ‘‘
                setTimeout(function () {
                  document.getElementById(nowId).parentElement.style.animation = nowAnimationStr
                }, nowStyle.interval * 1000)
              }, (nowStyle.duration + nowStyle.interval) * 1000)
            }, nowStyle.duration * 1000)
          }
          return nowStyleObj
        }

    代码如上,至于为什么定时器嵌套这么多,主要是为了第一次加载的时候展示正确的动画效果,如果对初次加载不在意的,可以直接使用setInterval那段代码就行。

  • 相关阅读:
    OnsenUI和AngularJS配合搭建混合应用基本步骤
    cordova plugin add cordova-plugin-dialogs cordova自定义插件
    Android题目
    Service服务
    SD卡 存储与读写
    Service服务生命流程 广播Broadcast
    Volley 网络请求 (常用)
    Jason 键值对 网络请求
    写文件
    aaa
  • 原文地址:https://www.cnblogs.com/ypppt/p/12868334.html
Copyright © 2020-2023  润新知