有这样一个场景:一个圆形容器,最外层容器的背景为圆弧,现在要将最外层的圆弧进行旋转,保证里面的容器里面的内容不进行旋转,接下来将跟大家分享一种解决方案,先看下最终实现的效果:

 

实现思路

最外层div设置边框倒角百分之50,溢出隐藏 设置最外层背景为圆弧的背景图 定义外层旋转动画,旋转度数为正数 定义内层旋转动画,旋转度数为负数 启动动画,开始旋转 外层为正数旋转,内层为负数旋转,刚好抵消,理想效果实现

实现过程

dom结构部分:布局外层div和内层div

load-panel为外层div,

headPortrait-img-panel

为内层div,

loadWhirl

为外层旋转动画,

avatarRotation

为内层旋转动画。

<!--头像区域--><div class="headPortrait-panel">    <!--加载层-->    <div class="load-panel loadWhirl">        <!--头像显示层-->        <div class="headPortrait-img-panel avatarRotation">            <img src="../assets/img/login/LoginWindow_BigDefaultHeadImage@2x.png"/>        </div>    </div></div>

css部分:对样式进行布局,实现旋转动画逻辑。

  /*头像区域*/  .headPortrait-panel{    width: 100%;    height: 200px;    display: flex;    justify-content: center;    align-items: center;    margin-top: 50px;    /*加载层*/    .load-panel{      width: 240px;      height: 240px;      border-radius: 50%;      display: flex;      justify-content: center;      align-items: center;      background: url("../img/login/loading-circle@2x.png");      img{        width: 100%;        height: 100%;      }      // 头像旋转动画      .avatarRotation{        animation: internalAvatar 3s linear;        // 动画无限循环        animation-iteration-count:infinite;      }      /*头像显示层*/      .headPortrait-img-panel{        width: 200px;        height: 200px;        border-radius: 50%;        overflow: hidden;        border: solid 1px #ebeced;        img{          width: 100%;          height: 100%;        }      }    }    // 外部旋转动画    .loadWhirl{      animation: externalHalo 3s linear;      // 动画无限循环      animation-iteration-count:infinite;    }  }  // 定义外部光环旋转动画  @keyframes externalHalo {    0%{      transform: rotate(0deg);    }    25%{      transform: rotate(90deg);    }    50%{      transform: rotate(180deg);    }    100%{      transform: rotate(360deg);    }  }  // 定义内部头像旋转动画  @keyframes internalAvatar {    0%{      transform: rotate(0deg);    }    25%{      transform: rotate(-90deg);    }    50%{      transform: rotate(-180deg);    }    100%{      transform: rotate(-360deg);    }  }

项目地址

上述代码地址:chat-system

项目克隆到本地后,访问 http://localhost:8020/login 即可查看效果
本文实现效果文件路径:src/views/login.vue

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。