6

我可以在 html 正文中成功完成此工作,例如...

<div id="myContainer" style="float: left; background-color: Blue; height: 100px;
            width: 100px;">
            <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%">
                <defs>
                    <lineargradient id="myLinearGradient" x1="0%" x2="0%" y1="0%" y2="100%">
                        <stop id="start" offset="50%" style="stop-color: White; stop-opacity: 1" />
                        <stop id="stop" offset="60%" style="stop-color: #99cd9f; stop-opacity: 1" />
                    </lineargradient>
                </defs>
                <circle cx="50px" cy="50px" r="50px" fill="url(#myLinearGradient)" />
            </svg>
        </div>

但是,我需要使用 javascript 动态创建它。只创建圆圈效果很好,当我将圆圈的填充指向线性渐变时它会失败 - 我只是得到一个黑色圆圈。

我想我没有正确设置 stop 'style' 属性。我尝试了另一种方法无济于事,请参见下文...

我正在使用 Chrome,在此先感谢!

在正文标签内:

    <body>
<div style="float: left; background-color: Blue; height: 100px;
                width: 100px;">
                <svg id="Svg1" xmlns="http://www.w3.org/2000/svg">
                    <defs id="mydefs">
                    </defs>
                </svg>
            </div>
</body>

My script:
    <script>

                // lineargradient 
                var myLinearGradient = document.createElementNS("http://www.w3.org/2000/svg", "lineargradient");
                myLinearGradient.setAttribute("id", "myLGID");
                myLinearGradient.setAttribute("x1", "0%");
                myLinearGradient.setAttribute("x2", "0%");
                myLinearGradient.setAttribute("y1", "0%");
                myLinearGradient.setAttribute("y2", "100%");

                document.getElementById("mydefs").appendChild(myLinearGradient);

                //stops
                var stop1 = document.createElementNS("http://www.w3.org/2000/svg", "stop");
                stop1.setAttribute("id", "myStop1");
                stop1.setAttribute("offset", "70%");
                //stop1.setAttribute("style", "stop-color: White; stop-opacity: 1");
                stop1.setAttribute("stop-color", "White");
                document.getElementById("mydefs").appendChild(stop1);

                var stop2 = document.createElementNS("http://www.w3.org/2000/svg", "stop");
                stop2.setAttribute("id", "myStop2");
                stop2.setAttribute("offset", "80%");
                //stop2.setAttribute("style", "stop-color: #99cd9f; stop-opacity: 1");
                stop2.setAttribute("stop-color", "#99cd9f");
                document.getElementById("mydefs").appendChild(stop2);

                // Circle
                var myCircle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
                myCircle.setAttribute("id", "idCircle");
                myCircle.setAttribute("cx", "50px");
                myCircle.setAttribute("cy", "50px");
                myCircle.setAttribute("r", "50px");

                myCircle.setAttribute("fill", "url(#myLGID)");

                document.getElementById("Svg1").appendChild(myCircle);
            </script>
4

1 回答 1

6

两件事情:

  • 线性渐变的元素名称是linearGradient, not lineargradient
  • 您必须将停靠点附加到linearGradient元素,而不是defs元素。

有关MIT 许可的示例,请参阅此codepen

// Store the SVG namespace for easy reuse.
var svgns = 'http://www.w3.org/2000/svg';

// Create <svg>, <defs>, <linearGradient> and <rect> elements using createElementNS to apply the SVG namespace.
// (https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS)
var svg = document.createElementNS(svgns, 'svg');
var defs = document.createElementNS(svgns, 'defs');
var gradient = document.createElementNS(svgns, 'linearGradient');
var rect = document.createElementNS(svgns, 'rect');

// Store an array of stop information for the <linearGradient>
var stops = [
    {
        "color": "#2121E5",
        "offset": "0%"
    },{
        "color": "#206DFF",
        "offset": "100%"
    }
];

// Parses an array of stop information and appends <stop> elements to the <linearGradient>
for (var i = 0, length = stops.length; i < length; i++) {

    // Create a <stop> element and set its offset based on the position of the for loop.
    var stop = document.createElementNS(svgns, 'stop');
    stop.setAttribute('offset', stops[i].offset);
    stop.setAttribute('stop-color', stops[i].color);

    // Add the stop to the <lineargradient> element.
    gradient.appendChild(stop);

}

// Apply the <lineargradient> to <defs>
gradient.id = 'Gradient';
gradient.setAttribute('x1', '0');
gradient.setAttribute('x2', '0');
gradient.setAttribute('y1', '0');
gradient.setAttribute('y2', '1');
defs.appendChild(gradient);

// Setup the <rect> element.
rect.setAttribute('fill', 'url(#Gradient)');
rect.setAttribute('width', '100%');
rect.setAttribute('height', '100%');

// Assign an id, classname, width and height
svg.setAttribute('width', '100%');
svg.setAttribute('height', '100%')
svg.setAttribute('version', '1.1');
svg.setAttribute('xmlns', svgns);

// Add the <defs> and <rect> elements to <svg>
svg.appendChild(defs);
svg.appendChild(rect);

// Add the <svg> element to <body>
document.body.appendChild(svg);
于 2012-12-07T10:24:09.337 回答