<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>svg线性渐变</title>
<style>
#linear{
border: 1px solid red;
width: 205px;
height: 210px;
}
</style>
<script>
function createLinerGradient(){
var root=document.getElementByIdx_x("linear");
var SVG="http://www.w3.org/2000/svg";
//创建我们的svg元素
var svg=document_createElement_xNS(SVG,"svg");
svg.setAttribute("width","205");
svg.setAttribute("height","210");
//创建我们的defs元素
var defs=document_createElement_xNS(SVG,"defs");
//创建linearGradient元素
var linearGradient=document_createElement_xNS(SVG,"linearGradient");
linearGradient.setAttribute("id","colorGradient");
//定义颜色的切换点
var stop1_1=document_createElement_xNS(SVG,"stop");
var stop1_2=document_createElement_xNS(SVG,"stop");
stop1_1.setAttribute("offset","0%");
stop1_1.setAttribute("stop-color","#0000ff");
stop1_2.setAttribute("offset","100%");
stop1_2.setAttribute("stop-color","#00ffff");
linearGradient.a(stop1_1);
linearGradient.a(stop1_2);
defs.a(linearGradient);
var alphaGradient=document_createElement_xNS(SVG,"linearGradient");
alphaGradient.setAttribute("id","alphaGradient");
alphaGradient.setAttribute("x1","0");
alphaGradient.setAttribute("x2","0");
alphaGradient.setAttribute("y1","0");
alphaGradient.setAttribute("y2","100%");
//定义颜色的切换点
var stop2_1=document_createElement_xNS(SVG,"stop");
var stop2_2=document_createElement_xNS(SVG,"stop");
stop2_1.setAttribute("offset","0%");
stop2_1.setAttribute("stop-color","#0000ff");
stop2_1.setAttribute("stop-opacity","1");
stop2_2.setAttribute("offset","100%");
stop2_2.setAttribute("stop-color","#00ffff");
stop2_2.setAttribute("stop-opacity","0");
alphaGradient.a(stop2_1);
alphaGradient.a(stop2_2);
defs.a(alphaGradient);
var rect1=document_createElement_xNS(SVG,"rect");
rect1.setAttribute("x",0);
rect1.setAttribute("y",0);
rect1.setAttribute("ry",20);
rect1.setAttribute("width",100);
rect1.setAttribute("height",200);
rect1.setAttribute("fill","url(#colorGradient)");
var rect2=document_createElement_xNS(SVG,"rect");
rect2.setAttribute("x",100);
rect2.setAttribute("y",0);
rect2.setAttribute("ry",20);
rect2.setAttribute("width",100);
rect2.setAttribute("height",200);
rect2.setAttribute("fill","url(#alphaGradient)");
svg.a(defs);
svg.a(rect1);
svg.a(rect2);
root.a(svg);
}
</script>
</head>
<body οnlοad="createLinerGradient()">
<h3>svgdom线性渐变</h3>
<div id="linear"></div>
</body>
</html>