用 CSS 和 D3 创作火焰动画

4 篇文章 0 订阅
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="flame">
    <!--<span></span>
    <span></span>
    <span></span>-->
</div>
</body>
</html>
        /*居中显示:*/
        body{
            margin: 0;
            display: flex;
            height: 100vh;
            align-items: center;   /*项目在交叉轴上如何对齐*/    /*水平的主轴(main axis)和垂直的交叉轴(cross axis)*/
            justify-content: center;      /*项目在主轴上的对齐方式*/
            background: linear-gradient(black,maroon);
            /*background: radial-gradient(blue,yellow);*/
            overflow: hidden;
        }
定义容器尺寸:
        .flame{
            width: 10em;
            height: 10em;
            font-size: 32px;
        }
        .flame{
            position: relative;
        }
        .flame span{
            position: absolute;
            width: 5em;
            height: 5em;
            background: radial-gradient(
            orangered 20%,
            rgba(255,69,0,0) 70%
            );
            border-radius: 50%;
            bottom: 0;
            left: calc((var(--n)-1)*5em/var(--particles));
        }
        /*用变量画出多个火苗,其中 --particles 是火苗的数量:*/
        /*.flame{
            --particles:3;
        }
        .flame span{
            left: calc((var(--n)-1)*5em/var(--particles));
        }
        .flame span:nth-child(1){
            --n:1;
        }
        .flame span:nth-child(2){
            --n:2;
        }
        .flame span:nth-child(3){
            --n:3;
        }*/
        /*修改混合模式,使火苗重叠的部分变亮:*/
        .flame span{
            mix-blend-mode: screen;
        }
        /*增加火焰升腾的动画效果:*/
        .flame span{
            animation: rise 1s ease-in infinite;
        }
        @keyframes rise {
            from{
                transform: translateY(0) scale(1);
                filter: opacity(0);
            }
            25%{
                filter: opacity(1);
            }
            to{
                transform: translateY(-10em) scale(0);
                filter: opacity(0);
            }
        }
        /*用变量设置火苗升起的延时时间,使火苗陆续升起:*/
        .flame span{
            animation-delay: calc(var(--rnd)*1s);
        }
        /*.flame span:nth-child(1){
            --n:1;
            --rnd:0.1234;
        }
        .flame span:nth-child(2){
            --n:2;
            --rnd:0.3456;
        }
        .flame span:nth-child(3){
            --n:3;
            --rnd:0.6789;
        }*/
<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
    const COUNT_OF_PARTICLES = 100;

/*用 d3 为火苗元素的 css 中的 --n 和 --rnd 变量赋值:*/

    d3.select('.flame')
        .style('--particles', COUNT_OF_PARTICLES)
        .selectAll('span')
        .data(d3.range(COUNT_OF_PARTICLES))
        .enter()
        .append('span')
        .style('--n', (d) => d + 1)
        .style('--rnd', () => Math.random());
</script>

linear-gradient() 函数用于创建一个线性渐变的 "图像"。

repeating-linear-gradient() 函数用于重复线性渐变:

语法:
<linear-gradient> = linear-gradient([ [ <angle> | to <side-or-corner> ] ,]? <color-stop>[, <color-stop>]+)
<side-or-corner> = [left | right] || [top | bottom]
<color-stop> = <color> [ <length> | <percentage> ]?
取值:
下述值用来表示渐变的方向,可以使用角度或者关键字来设置:
<angle>:用角度值指定渐变的方向(或角度)。
to left:设置渐变为从右到左。相当于: 270deg
to right:设置渐变从左到右。相当于: 90deg
to top:设置渐变从下到上。相当于: 0deg
to bottom:设置渐变从上到下。相当于: 180deg。这是默认值,等同于留空不写。
<color-stop> 用于指定渐变的起止颜色:
<color>:指定颜色。
<length>:用长度值指定起止色位置。不允许负值
<percentage>:用百分比指定起止色位置。
说明:
用线性渐变创建图像。
如果想创建以对角线方式渐变的图像,可以使用 to top left 这样的多关键字方式来实现。

从左上角到右下角的线性渐变:
  background: -webkit-linear-gradient(left top, red , blue); /* Safari 5.1 to 6.0 */
  background: -o-linear-gradient(bottom right, red, blue); /* Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(bottom right, red, blue); /* Firefox 3.6 to 15 */
  background: linear-gradient(to bottom right, red , blue); /* 标准语法 */
线性渐变指定一个角度:
  background: linear-gradient(180deg, red, blue); 
多个终止色:
  background: linear-gradient(to right, red,orange,yellow,green,blue,indigo,violet); 
使用了透明度:
  background: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0,1)); 

radial-gradient() 函数用径向渐变创建 "图像"。径向渐变由中心点定义。

repeating-radial-gradient() 函数用于重复径向渐变:

语法:
<radial-gradient> = radial-gradient([ [ <shape> || <size> ] [ at <position> ]? , | at <position>, ]?<color-stop>[ , <color-stop> ]+)
<position> = [ <length>① | <percentage>① | left | center① | right ]? [ <length>② | <percentage>② | top | center② | bottom ]?
<shape> = circle | ellipse
<size> = <extent-keyword> | [ <circle-size> || <ellipse-size> ]
<extent-keyword> = closest-side | closest-corner | farthest-side | farthest-corner
<circle-size> = <length>
<ellipse-size> = [ <length> | <percentage> ]{2}
<shape-size> = <length> | <percentage>
<color-stop> = <color> [ <length> | <percentage> ]?
取值:
<position> 确定圆心的位置。如果提供2个参数,第一个表示横坐标,第二个表示纵坐标;如果只提供一个,第二值默认为50%,即center
<percentage>①:
用百分比指定径向渐变圆心的横坐标值。可以为负值。
<length>①:
用长度值指定径向渐变圆心的横坐标值。可以为负值。
left:
设置左边为径向渐变圆心的横坐标值。
center①:
设置中间为径向渐变圆心的横坐标值。
right:
设置右边为径向渐变圆心的横坐标值。
<percentage>②:用百分比指定径向渐变圆心的纵坐标值。可以为负值。
<length>②:用长度值指定径向渐变圆心的纵坐标值。可以为负值。
top:设置顶部为径向渐变圆心的纵坐标值。
center②:设置中间为径向渐变圆心的纵坐标值。
bottom:设置底部为径向渐变圆心的纵坐标值。
<shape> 确定圆的类型
circle:指定圆形的径向渐变
ellipse:指定椭圆形的径向渐变。
<extent-keyword> circle | ellipse 都接受该值作为 size
closest-side:
指定径向渐变的半径长度为从圆心到离圆心最近的边
closest-corner:
指定径向渐变的半径长度为从圆心到离圆心最近的角
farthest-side:
指定径向渐变的半径长度为从圆心到离圆心最远的边
farthest-corner:
指定径向渐变的半径长度为从圆心到离圆心最远的角
<circle-size> circle 接受该值作为 size
<length>:
用长度值指定正圆径向渐变的半径长度。不允许负值。
<ellipse-size> ellipse 接受该值作为 size
<length>:
用长度值指定椭圆径向渐变的横向或纵向半径长度。不允许负值。
<percentage>:用百分比指定椭圆径向渐变的横向或纵向半径长度。不允许负值。
<color-stop> 用于指定渐变的起止颜色:
<color>:指定颜色。
<length>:用长度值指定起止色位置。不允许负值
<percentage>:用百分比指定起止色位置。不允许负值

颜色结点不均匀分布:
  background: radial-gradient(red 5%, green 15%, blue 60%); 
圆形径向渐变:
  background: radial-gradient(circle, red, yellow, green); 
不同尺寸大小关键字的使用:
  background: radial-gradient(closest-side at 60% 55%,blue,green,yellow,black);
  background: radial-gradient(farthest-side at 60% 55%,blue,green,yellow,black);

calc() 函数用于动态计算长度值。

需要注意的是,运算符前后都需要保留一个空格,例如:width: calc(100% - 10px);
任何长度值都可以使用calc()函数进行计算;
calc()函数支持 "+", "-", "*", "/" 运算;
calc()函数使用标准的数学运算优先级规则;

animation 属性是一个简写属性,用于设置六个动画属性:
animation-name
animation-duration
animation-timing-function
animation-delay
animation-iteration-count
animation-direction
注释:请始终规定 animation-duration 属性,否则时长为 0,就不会播放动画了。
语法

animation: name duration timing-function delay iteration-count direction;

animation-name    规定需要绑定到选择器的 keyframe 名称。。
animation-duration    规定完成动画所花费的时间,以秒或毫秒计。
animation-timing-function    规定动画的速度曲线。
animation-delay    规定在动画开始之前的延迟。
animation-iteration-count    规定动画应该播放的次数。
animation-direction    规定是否应该轮流反向播放动画。

animation-timing-function 使用名为三次贝塞尔(Cubic Bezier)函数的数学函数,来生成速度曲线。您能够在该函数中使用自己的值,也可以预定义的值:
linear    动画从头到尾的速度是相同的。   
ease    默认。动画以低速开始,然后加快,在结束前变慢。    
ease-in    动画以低速开始。    
ease-out    动画以低速结束。   
ease-in-out    动画以低速开始和结束。    
cubic-bezier(n,n,n,n)    在 cubic-bezier 函数中自己的值。可能的值是从 0 到 1 的数值。
例:.div1 { animation-timing-function: cubic-bezier(0,0,1,1); }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue提供了过渡动画的内置支持,可以通过Vue的transition组件来实现过渡动画。在transition组件中,可以定义进入动画、离开动画和过渡模式等。同时,CSS也提供了丰富的动画效果,可以通过CSS的transition属性来实现过渡动画。 下面是一个使用Vue和CSS实现过渡动画的示例: ```html <template> <div> <button @click="show = !show">Toggle</button> <transition name="fade"> <p v-if="show">Hello, World!</p> </transition> </div> </template> <style> .fade-enter-active, .fade-leave-active { transition: opacity 0.5s; } .fade-enter, .fade-leave-to { opacity: 0; } </style> <script> export default { data() { return { show: false }; } }; </script> ``` 在上面的示例中,我们定义了一个按钮和一个包裹着文本的transition组件。文本的显示与隐藏通过控制show属性来实现。同时,我们也定义了一个名为"fade"的transition,它定义了进入和离开动画的方式。 在CSS中,我们定义了两个类名为"fade-enter-active"和"fade-leave-active",它们分别表示进入和离开动画的状态。这里我们使用了CSS的transition属性,它指定了过渡的属性和时长。在这个例子中,我们使用opacity属性和0.5秒的时长来实现淡入淡出的效果。 同时,我们也定义了类名为"fade-enter"和"fade-leave-to",它们分别表示进入和离开动画的初始和结束状态。在这个例子中,我们将opacity属性的值设置为0,表示文本初始时是透明的,离开时也是透明的。 通过这样的方式,我们就可以使用Vue和CSS实现过渡动画了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值