javascript - 向 SVG 圆添加线性渐变

标签 javascript html css svg

我正在尝试将从网上获得的渐变添加到我的 SVG 圈子中。这是我的渐变

background-image: linear-gradient(to top, #a18cd1 0%, #fbc2eb 100%);

我正在尝试将此渐变类型添加到我的圆圈中,而不是“fill="yellow”。我尝试在代码中插入渐变,但什么也没有。尝试搜索/尝试使用它,但最终一无所获。这里是我的代码

密码本 http://codepen.io/anon/pen/wdaMVR

    .circlesvg{
    			position: absolute;
    			left: 0;
    			top: 0;
    		}        
    			#firstCircle{
    			animation: fadeAndScale 33s ease-in infinite;
    			-ms-animation: fadeAndScale 33s ease-in infinite;
    			-webkit-animation: fadeAndScale 33s ease-in infinite;
    			-moz-animation: fadeAndScale 33s ease-in infinite;
    			-o-animation: fadeAndScale 33s ease-in infinite;
    			transition-timing-function: linear;
    			}
    			@keyframes fadeAndScale{
    			0%{
    			  z-index: 100;
    			  transform: scale(0);
    			  transform: translate(200px, 200px);
    			}
    			100%{
    			  z-index: 0;
    			  transform: scale(200);
    
    }
    		  }
<svg width="100%" height="100%" class="circlesvg">
  <circle id="firstCircle" cx="0" cy="0" r="40" fill="yellow"></circle>
</svg>

最佳答案

您必须使用 SVG 的 <linearGradient>元素,然后引用它作为填充:

 <defs>
    <linearGradient id="gradient">
      <stop offset="0%"  stop-color="#a18cd1"/>
      <stop offset="100%" stop-color="#fbc2eb"/>
    </linearGradient>
  </defs>
  <circle id="firstCircle" cx="0" cy="0" r="40" fill="url(#gradient)"></circle>

http://codepen.io/anon/pen/VbLaYy

关于javascript - 向 SVG 圆添加线性渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43431830/

相关文章:

javascript - Highcharts 将渐变映射到 y 轴?

css - 调整浏览器窗口大小时,Chrome 不会更改设备宽度

html - 如何移动此图标/按钮? CSS?

javascript - 使用 JQuery 选择最后添加的元素

javascript - 防止在单击子元素时触发其父级的单击事件

javascript - 为什么我的 jquery 函数在 ajax 加载的页面中的 div 上不起作用

html - css下拉菜单移动页面内容

javascript - 在javascript之后跳过html表中的第一行

javascript - 从 JavaScript 中提取 i18n 标记

javascript - 我可以使用 webpack 分别生成 CSS 和 JS 吗?