通过CSS控制svg变化

8 篇文章 0 订阅

本文介绍使用css魔法控制svg变化,要想实现这一点只需要耐心看下去!

svg代码示例

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>svg</title>
  <style>

  </style>
</head>
<body>
  <svg width="100" height="86" xmlns="http://www.w3.org/2000/svg">
    <g>
      <title></title>
      <path id="svg_1" d="m39.073334,65.405621c-20.084052,-14.68342 -27.264901,-23.971887 -27.323097,-35.342652c-0.053071,-10.365845 8.811315,-20.370413 17.99769,-20.312718c4.587266,0.028868 14.427243,3.844067 17.911613,6.944866c1.755631,1.562362 2.587182,1.408298 6.495906,-1.203522c10.637898,-7.108256 21.025912,-7.257456 27.753647,-0.398585c10.75235,10.961918 8.794766,24.052449 -5.627388,37.630866c-7.667647,7.219075 -24.412104,20.026118 -26.182963,20.026118c-0.538848,0 -5.500278,-3.304975 -11.025407,-7.344376l0,0.000003z" stroke-width="1.5"
        fill="#fff"
        stroke="#000"
        />
    </g>
  </svg>
</body>
</html>
效果图

在这里插入图片描述

控制大小

svg原来的宽100、高86,我们尝试把它变成宽50px、高43px,下面是css的代码

svg {
    width: 50px;
    height: 43px;
}
效果图

在这里插入图片描述

怎么会这样?和想象的差距有点大!图像没有缩放,而是被裁减了。是时候请出svg的大神属性viewBox了,给svg标签增加属性viewBox="0,0,100,86"

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>svg</title>
  <style>
+	svg {
+        width: 200px;
+        height: 172px;
+    }
  </style>
</head>
<body>
-  <svg width="100" height="86" xmlns="http://www.w3.org/2000/svg">
+  <svg width="100" height="86" viewBox="0,0,100,86" xmlns="http://www.w3.org/2000/svg">
    <g>
      <title>心</title>
      <path id="svg_1" d="m39.073334,65.405621c-20.084052,-14.68342 -27.264901,-23.971887 -27.323097,-35.342652c-0.053071,-10.365845 8.811315,-20.370413 17.99769,-20.312718c4.587266,0.028868 14.427243,3.844067 17.911613,6.944866c1.755631,1.562362 2.587182,1.408298 6.495906,-1.203522c10.637898,-7.108256 21.025912,-7.257456 27.753647,-0.398585c10.75235,10.961918 8.794766,24.052449 -5.627388,37.630866c-7.667647,7.219075 -24.412104,20.026118 -26.182963,20.026118c-0.538848,0 -5.500278,-3.304975 -11.025407,-7.344376l0,0.000003z" stroke-width="1.5"
        fill="#fff"
        stroke="#000"
        />
    </g>
  </svg>
</body>
</html>
效果图

在这里插入图片描述

理解SVGviewBox

控制颜色

svg的填充是白色,我想修改成红色,整一个大红心,下面是css的代码

svg {
    width: 200px;
    height: 172px;
    fill: currentColor;
    color:red;
}
效果图

在这里插入图片描述

现实很残酷啊,什么变化也没有!但是你仔细看一下path标签里面有一个行内属性fill="#fff",那么问题就解决了,只需要把属性fill去掉。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>svg</title>
  <style>
+	svg {
+        width: 200px;
+        height: 172px;
+        fill: currentColor;
+        color:red;
+    }
  </style>
</head>
<body>
   <svg width="100" height="86" viewBox="0,0,100,86" xmlns="http://www.w3.org/2000/svg">
    <g>
      <title>心</title>
      <path id="svg_1" d="m39.073334,65.405621c-20.084052,-14.68342 -27.264901,-23.971887 -27.323097,-35.342652c-0.053071,-10.365845 8.811315,-20.370413 17.99769,-20.312718c4.587266,0.028868 14.427243,3.844067 17.911613,6.944866c1.755631,1.562362 2.587182,1.408298 6.495906,-1.203522c10.637898,-7.108256 21.025912,-7.257456 27.753647,-0.398585c10.75235,10.961918 8.794766,24.052449 -5.627388,37.630866c-7.667647,7.219075 -24.412104,20.026118 -26.182963,20.026118c-0.538848,0 -5.500278,-3.304975 -11.025407,-7.344376l0,0.000003z" stroke-width="1.5"
-        fill="#fff"
        stroke="#000"
        />
    </g>
  </svg>
</body>
</html>
效果图

在这里插入图片描述

其实还有另一种方法,使用fill="currentColor"

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>svg</title>
  <style>
+    svg {
+      width: 200px;
+      height: 172px;
+      color:red;
+    }
  </style>
</head>
<body>
  <svg width="100" height="86" viewBox="0,0,100,86" xmlns="http://www.w3.org/2000/svg">
    <g>
      <title>心</title>
      <path id="svg_1" d="m39.073334,65.405621c-20.084052,-14.68342 -27.264901,-23.971887 -27.323097,-35.342652c-0.053071,-10.365845 8.811315,-20.370413 17.99769,-20.312718c4.587266,0.028868 14.427243,3.844067 17.911613,6.944866c1.755631,1.562362 2.587182,1.408298 6.495906,-1.203522c10.637898,-7.108256 21.025912,-7.257456 27.753647,-0.398585c10.75235,10.961918 8.794766,24.052449 -5.627388,37.630866c-7.667647,7.219075 -24.412104,20.026118 -26.182963,20.026118c-0.538848,0 -5.500278,-3.304975 -11.025407,-7.344376l0,0.000003z" stroke-width="1.5"
-       fill="#fff"
+		fill="currentColor"
        stroke="#000"
        />
    </g>
  </svg>
</body>
</html>

虽然控制了填充,但是边框并没有改变,如果想同时控制边框只要参考上面的修改stroke属性就可以。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>svg</title>
  <style>
+    svg {
+      width: 200px;
+      height: 172px;
+      color:red;
+    }
  </style>
</head>
<body>
  <svg width="100" height="86" viewBox="0,0,100,86" xmlns="http://www.w3.org/2000/svg">
    <g>
      <title>心</title>
      <path id="svg_1" d="m39.073334,65.405621c-20.084052,-14.68342 -27.264901,-23.971887 -27.323097,-35.342652c-0.053071,-10.365845 8.811315,-20.370413 17.99769,-20.312718c4.587266,0.028868 14.427243,3.844067 17.911613,6.944866c1.755631,1.562362 2.587182,1.408298 6.495906,-1.203522c10.637898,-7.108256 21.025912,-7.257456 27.753647,-0.398585c10.75235,10.961918 8.794766,24.052449 -5.627388,37.630866c-7.667647,7.219075 -24.412104,20.026118 -26.182963,20.026118c-0.538848,0 -5.500278,-3.304975 -11.025407,-7.344376l0,0.000003z" stroke-width="1.5"
-       fill="#fff"
+		fill="currentColor"
-       stroke="#000"
+		stroke="currentColor"
        />
    </g>
  </svg>
</body>
</html>
效果图

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在Vue中不使用任何JavaScript和CSS代码来控制SVG绘制的动画的开始和结束,可以使用Vue的过渡和动画钩子函数来实现。 以下是一个使用Vue过渡和动画钩子函数来控制SVG动画的示例: ```html <template> <div> <svg> <circle ref="myCircle" cx="50" cy="50" r="20" :class="{ 'animation': animate }" @click="toggleAnimation" ></circle> </svg> </div> </template> <script> export default { data() { return { animate: false }; }, methods: { toggleAnimation() { this.animate = !this.animate; } }, mounted() { this.$refs.myCircle.addEventListener('transitionend', () => { // 动画结束时触发的逻辑 if (!this.animate) { this.$refs.myCircle.style.animation = ''; } }); }, watch: { animate(value) { if (value) { this.$refs.myCircle.style.animation = 'myAnimation 1s infinite'; } else { this.$refs.myCircle.style.animation = 'none'; } } } }; </script> ``` 在上述代码中,我们通过`ref`属性给圆形元素绑定了一个引用名为`myCircle`。使用`class`属性来动态控制是否添加`animation`类名,并在点击圆形元素时触发`toggleAnimation`方法来切换动画的开始和暂停。 通过`mounted`钩子函数,我们为SVG元素的`transitionend`事件添加了一个监听器,当动画结束时会执行相应的逻辑。在这里,我们根据`animate`的值来决定是否将`style.animation`设置为空字符串或`none`,以实现动画的开始和暂停。 通过`watch`属性监听`animate`的变化,当`animate`的值改变时,会根据其值来动态修改SVG元素的`style.animation`属性,从而开始或停止动画。 这样,你可以在Vue中通过过渡和动画钩子函数来控制SVG绘制的动画的开始和结束,而不需要编写额外的JavaScript和CSS代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值