[css]旋转流光效果

实现一个矩形的旋转流光边框效果。

需要使用css属性梯度渐变:链接: conic-gradient,他指的是圆锥形变化的梯度。

html 复制代码
// html
 <div class="demo">
 </div>
css 复制代码
// css
body {
   width: 100%;
   height: 100%;
   background-color: black;
}

.demo {
   width: 400px;
   height: 200px;
   background-image: conic-gradient(from var(--border-gradient-angle) at 50% 50%, transparent, #70ffaf 14%, transparent 17%);
   background-size: contain;
}

@property --border-gradient-angle {
  syntax: "<angle>";
  inherits: true;
  initial-value: 0turn;
}

上面的代码,指的是从第 0 转位置(中间垂直向上)开始,在中心点位置放置渐变,效果如下:

给上面效果加上动画:

css 复制代码
.demo {
    width: 400px;
    height: 200px;
    background-image: conic-gradient(from var(--border-gradient-angle) at 50% 50%, transparent, #70ffaf 14%, transparent 17%);
    background-size: contain;
    animation: buttonBorderSpin 9s linear infinite 0ms;
}
@keyframes buttonBorderSpin {
    0% {
        --border-gradient-angle: 0turn;
    }

    100% {
        --border-gradient-angle: 1turn;
    }
}
@property --border-gradient-angle {
  syntax: "<angle>";
  inherits: true;
  initial-value: 0turn;
}

添加上述代码之后,可以获得一个线性旋转,旋转一整周(1turn)并周期循环的动画。

之后对这个div进行遮挡,在其中添加一个div:

html 复制代码
 <div class="demo">
     <div class="demo-content">
        旋转流光
    </div>
 </div>
css 复制代码
.demo {
    ...
    padding: 1px;
}
 .demo-content {
     width: 100%;
     height: 100%;
     background-color: black;
     color: white;
 }

遮挡后即可看到绕边框一周的旋转流光效果。

改变旋转点还可以获得平移流光效果

css 复制代码
background-image: conic-gradient(from var(--border-gradient-angle) at 30% -30%, transparent 25%, #70ffaf 75%, transparent);
相关推荐
RFCEO20 小时前
前端编程 课程十六、:CSS 盒子模型
css·前端基础课程·css盒子模型·css盒子模型的组成·精准控制元素的大小和位置·css布局的基石·内边距(padding)
夏幻灵1 天前
CSS三大特性:层叠、继承与优先级解析
前端·css
会编程的土豆2 天前
新手前端小细节
前端·css·html·项目
珹洺2 天前
Bootstrap-HTML(二)深入探索容器,网格系统和排版
前端·css·bootstrap·html·dubbo
BillKu2 天前
VS Code HTML CSS Support 插件详解
前端·css·html
1024小神2 天前
用css的clip-path裁剪不规则形状的图片展示
前端·css
GGGG寄了2 天前
CSS——文字控制属性
前端·javascript·css·html
HWL56792 天前
在网页中实现WebM格式视频自动循环播放
前端·css·html·excel·音视频
HWL56792 天前
防止移动设备自动全屏播放视频,让视频在页面内嵌位置正常播放
前端·css·音视频
小小测试开发3 天前
UI自动化测试:CSS定位方式超详细解析(附实战示例)
css·ui·tensorflow