zoukankan      html  css  js  c++  java
  • 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那段代码就行。

    完结撒花,如有不对欢迎指教

  • 相关阅读:
    redhat 7.2 内网安装docker
    使用dockerfile 创建ubuntu ssh镜像
    docker 离线环境安装oracle
    redhat 6.6 、7、Centos7离线安装docker
    用命令行管理aws s3
    Anaconda介绍、安装及使用教程
    python2 编码问题万能钥匙
    从mongo数据库中导出数据的方法
    MongoDB学习第三篇 --- Insert操作
    MongoDB学习笔记(一)-Insert操作
  • 原文地址:https://www.cnblogs.com/LeoXnote/p/12736672.html
Copyright © 2011-2022 走看看