简体   繁体   English

线性渐变不起作用

[英]Linear Gradient not working

I am trying to add SVG gradient but it doesn't seem to add it, 我正在尝试添加SVG渐变,但似乎没有添加它,

Code

var svgContainer = d3.select("#myConta")
    .append("svg")
    .attr("id", "myContasvg")
    .attr("width", "100%")
    .attr("height", "100%")
    .attr("preserveAspectRatio", "none")
    .attr("viewBox", "0 0 " + width+ " " + height)
    .attr("fill", "#2E2E2E")
    .attr("float", "right")
    .attr("fill", "url(#svgLG1)")
    .append("g");

var gradient = svgContainer.append("svg:defs")
    .append("svg:linearGradient")
    .attr("id", "svgLG1")
    .attr("x1", "0")
    .attr("y1", "1")
    .attr("x2", "0.9967")
    .attr("y2", "0")
    .attr("spreadMethod", "reflect");

    gradient.append("svg:stop")
       .attr("offset", "0.0267")
       .attr("stop-color", "#0f0");

    gradient.append("svg:stop")
        .attr("offset", "0.4967")
        .attr("stop-color", "#ff0");

    gradient.append("svg:stop")
        .attr("offset", "1")
        .attr("stop-color", "#f00");

JSFiddle JSFiddle

I am able to make it work with CSS gradient ( fiddle ) but because of needs (IE9 support and calculating offset values by code) I need to use SVG. 我能够使其与CSS渐变一起使用( 小提琴 ),但是由于需要(IE9支持和通过代码计算偏移值),我需要使用SVG。

Edit 编辑

It's not duplicate 它不是重复的

在此处输入图片说明

<svg> is only a container element and fills applied to it don't render. <svg>只是一个容器元素,应用于它的填充不会呈现。 You have to apply your fill to a graphic element such as a <rect> , <circle> , <text> etc. 您必须将填充应用于图形元素,例如<rect><circle><text>等。

So therefore you need to create a <rect> as a background and add the fill url to that. 因此,您需要创建一个<rect>作为背景,并在其中添加填充网址。

Code I used for testing 我用于测试的代码

var mysvg = d3.select("#a1")
    .append("svg")
    .attr("id", "mysvg")
    .attr("width", "100%")
    .attr("height", "100%").attr("fill", "url(#svgLG1)");

var rect = mysvg.append("rect")
    .attr("x", "10")
    .attr("y", "20")
    .attr("width", "100%")
    .attr("height", "100%")
    .attr("stroke", "black")
   ;

var gradient = mysvg.append("svg:linearGradient")
    .attr("id", "svgLG1")
    .attr("x1", "0")
    .attr("y1", "1")
    .attr("x2", "1")
    .attr("y2", "0");
    gradient.append("svg:stop").attr("offset", "0").attr("stop-color", "#0f0");
    gradient.append("svg:stop").attr("offset", "0.4867").attr("stop-color", "#ff0");
    gradient.append("svg:stop").attr("offset", "1").attr("stop-color", "#f00");

http://jsfiddle.net/r55g9po2/17/ http://jsfiddle.net/r55g9po2/17/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM