From: http://hi.baidu.com/potami/item/644f4e3317c4b6dd6d15e988
HTML callCode:
<HTML>
<Head>
<Style>
Body {margin: 0px; overflow: hidden ;}
# Divbox {width: 400px; Height: 200px; overflow: hidden ;}
</Style>
<Script language = "JavaScript" src = "JS/gradient. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
Window. onload = function (){
Radialgradient (['body', '# ff00ff', '# 000000', '000000', 'mc']);
Radialgradient (['divbox', '# 00ff00',' # 000000', '000000', 'mc ']);
}
</SCRIPT>
</Head>
<Body>
<Div id = "divbox"> </div>
</Body>
</Html>
JS/gadient. js file code:
VaR AGT = navigator. useragent. tolowercase ();
Function radialgradient (arrayvalue ){
If (! Window. Opera & Agt. indexof ("MSIE ")! =-1) {radialgradient_msie (arrayvalue );}
Else if (window. Opera) {radialgradient_opera (arrayvalue );}
Else {radialgradient_css3 (arrayvalue );}
}
Function lineargradient (arrayvalue ){
If (! Window. Opera & Agt. indexof ("MSIE ")! =-1) {lineargradient_msie (arrayvalue );}
Else if (window. Opera) {lineargradient_opera (arrayvalue );}
Else {lineargradient_css3 (arrayvalue );}
}
// ------------- MSIE ----------------------------------------
Function radialgradient_msie (arrayvalue ){
VaR innercolor = arrayvalue [1];
VaR outercolor = arrayvalue [2];
VaR colorstop = arrayvalue [3];
VaR theelement = arrayvalue [0] = 'body '? Document. Body: Document. getelementbyid (arrayvalue [0]);
If (arrayvalue [4] = 'tl ') {var posx =-colorstop; var posy =-colorstop ;}
If (arrayvalue [4] = 'tc ') {var posx = theelement. offsetwidth/2-colorstop; var posy =-colorstop ;}
If (arrayvalue [4] = 'tr') {var posx = theelement. offsetwidth-colorstop; var posy =-colorstop ;}
If (arrayvalue [4] = 'ml') {var posx =-colorstop; var posy = theelement. offsetheight/2-colorstop ;}
If (arrayvalue [4] = 'mc ') {var posx = theelement. offsetwidth/2-colorstop; var posy = theelement. offsetheight/2-colorstop ;}
If (arrayvalue [4] = 'Mr ') {var posx = theelement. offsetwidth-colorstop; var posy = theelement. offsetheight/2-colorstop ;}
If (arrayvalue [4] = 'bl') {var posx =-colorstop; var posy = theelement. offsetheight/2-colorstop/2 ;}
If (arrayvalue [4] = 'bc') {var posx = theelement. offsetwidth/2-colorstop; var posy = theelement. offsetheight-colorstop ;}
If (arrayvalue [4] = 'br ') {var posx = theelement. offsetwidth-colorstop; var posy = theelement. offsetheight-colorstop ;}
var iepseudobg = document. createelement ('div ');
iepseudobg. style. width = colorstop * 2;
iepseudobg. style. height = colorstop * 2;
iepseudobg. style. position = 'abort';
iepseudobg. style. zindex = '-1';
iepseudobg. style. top = '0px ';
iepseudobg. style. left = '0px ';
iepseudobg. style. marginleft = posx;
iepseudobg. style. margintop = posy;
iepseudobg. style. backgroundcolor = innercolor;
iepseudobg. style. filter = 'progid: DXImageTransform. microsoft. alpha (opacity = 100, finishopacity = 0, style = 2) ';
Theelement. appendchild (iepseudobg );
Theelement. style. backgroundcolor = outercolor;
}
Function lineargradient_msie (arrayvalue ){
VaR theelement = arrayvalue [0] = 'body '? Document. Body: Document. getelementbyid (arrayvalue [0]);
VaR iepseudobg = Document. createelement ('div ');
Iepseudobg. style. Position = 'absolute ';
If (arrayvalue [4] = 'T '){
VaR gradienttype = 0; var flip = '';
Iepseudobg. style. width = theelement. offsetwidth;
Iepseudobg. style. Height = arrayvalue [3];
Iepseudobg. style. Top = '0px ';
Iepseudobg. style. Left = '0px ';
}
If (arrayvalue [4] = 'R '){
VaR gradienttype = 1; var flip = 'fliph ()';
Iepseudobg. style. width = arrayvalue [3];
Iepseudobg. style. Height = theelement. offsetheight;
Iepseudobg. style. Top = '0px ';
Iepseudobg. style. Right = '0px ';
}
If (arrayvalue [4] = 'B '){
VaR gradienttype = 0; var flip = 'flipv ()';
Iepseudobg. style. width = theelement. offsetwidth;
Iepseudobg. style. Height = arrayvalue [3];
Iepseudobg. style. Top = theelement. offsetheight-arrayvalue [3];
Iepseudobg. style. Left = '0px ';
}
If (arrayvalue [4] = 'l '){
VaR gradienttype = 1; var flip = '';
Iepseudobg. style. width = arrayvalue [3];
Iepseudobg. style. Height = theelement. offsetheight;
Iepseudobg. style. Top = '0px ';
Iepseudobg. style. Left = '0px ';
}
iepseudobg. style. zindex =-1;
iepseudobg. style. backgroundcolor = '# ffff';
theelement. appendchild (iepseudobg);
iepseudobg. style. filter = 'progid: DXImageTransform. microsoft. gradient (gradienttype = '+ gradienttype +', startcolorstr = '+ arrayvalue [1] +', endcolorstr = '+ arrayvalue [2] +') '+ flip;
theelement. style. backgroundcolor = arrayvalue [2];
}
// ------------- Opera -------------------------------------------
Function radialgradient_opera (arrayvalue ){
VaR theelement = arrayvalue [0] = 'body '? Document. Body: Document. getelementbyid (arrayvalue [0]);
VaR innercolor = arrayvalue [1];
VaR outercolor = arrayvalue [2];
VaR colorstop = arrayvalue [3] * 2;
If (arrayvalue [0] = 'body') {var thewidth = document. body. clientwidth; var theheight = document. body. clientheight;} else {var thewidth = document. getelementbyid (arrayvalue [0]). offsetwidth; var theheight = document. getelementbyid (arrayvalue [0]). offsetheight ;}
If (arrayvalue [4] = 'tl ') {var posx =-colorstop/2; var posy =-colorstop/2 ;}
If (arrayvalue [4] = 'tc ') {var posx = (thewidth-colorstop)/2; var posy =-colorstop/2 ;}
If (arrayvalue [4] = 'tr') {var posx = thewidth/2 + colorstop/2; var posy =-colorstop/2 ;}
If (arrayvalue [4] = 'ml') {var posx =-colorstop/2; var posy = (theheight-colorstop)/2 ;}
If (arrayvalue [4] = 'mc ') {var posx = (thewidth-colorstop)/2; var posy = (theheight-colorstop)/2 ;}
If (arrayvalue [4] = 'Mr ') {var posx = thewidth/2 + colorstop/2; var posy = (theheight-colorstop)/2 ;}
If (arrayvalue [4] = 'bl') {var posx =-colorstop/2; var posy = theheight/2 ;}
If (arrayvalue [4] = 'bc') {var posx = (thewidth-colorstop)/2; var posy = theheight/2 ;}
If (arrayvalue [4] = 'br ') {var posx = thewidth/2 + colorstop/2; var posy = theheight/2 ;}
VaR SVG = '< SVG xmlns = "http://www.w3.org/2000/svg" xmlns: xlink = "http://www.w3.org/1999/xlink" width = "'+ colorstop + 'px" Height = "' + colorstop + 'px"> <title> SVG opera background radial gradient </title> <metadata> author: reinhard v. d. waydbrink </metadata> <defs> <radialgradient id = "RG" Cx = "50%" Cy = "50%" r = "100%"> <Stop-color = "'+ innercolor + '"offset =" 0% "> </Stop> <Stop-color ="' + outercolor + '"offset =" 50% "> </Stop> </radialgradient> </defs> <rect style = "fill: URL (# RG); "width =" '+ colorstop + 'px "Height ="' + colorstop + 'px "/> </SVG> ';
VaR encodeddata = Window. btoa (SVG );
Theelement. style. background = 'url ("data: image/SVG + XML; base64, '+ encodeddata + '") no-repeat '+ outercolor + ''+ posx + 'px' + posy + 'px ';
}
Function lineargradient_opera (arrayvalue ){
VaR theelement = arrayvalue [0] = 'body '? Document. Body: Document. getelementbyid (arrayvalue [0]);
VaR innercolor = arrayvalue [1];
VaR outercolor = arrayvalue [2];
VaR colorstop = arrayvalue [3] * 2;
If (arrayvalue [0] = 'body') {var thewidth = document. body. clientwidth; var theheight = document. body. clientheight;} else {var thewidth = document. getelementbyid (arrayvalue [0]). offsetwidth; var theheight = document. getelementbyid (arrayvalue [0]). offsetheight ;}
If (arrayvalue [4] = "T") {var X1 = '000000'; var Y1 = '0% '; var X2 = '0% '; vaR y2 = '000000';} else {var X1 = '0% '; var Y1 = '000000'; var X2 = '000000 '; vaR y2 = '0% ';}
VaR SVG = '< SVG xmlns = "http://www.w3.org/2000/svg" xmlns: xlink = "http://www.w3.org/1999/xlink" width = "'+ thewidth + 'px" Height = "' + theheight + 'px"> <title> SVG opera background linear gradient </title> <metadata> author: reinhard v. d. waydbrink </metadata> <defs> <lineargradient id = "LG" x = "'+ X1 +'" Y = "'+ Y1 +'" X2 = "'+ X2 + '"y2 ="' + y2 + '"> <Stop-color ="' + innercolor + '"offset =" 0% "> </Stop> <Stop-color = "'+ outercolor +'" offset = "100%"> </Stop> </lineargradient> </defs> <rect style = "fill: URL (# LG); "width =" '+ thewidth + 'px "Height ="' + theheight + 'px "/> </SVG> ';
VaR encodeddata = Window. btoa (SVG );
Theelement. style. Background = 'url ("data: image/SVG + XML; base64, '+ encodeddata +'") No-repeat '+ outercolor + '0px 0px ';
}
// ------- Firefox, chrome, Safari --------------------------------
Function radialgradient_css3 (arrayvalue ){
VaR theelement = arrayvalue [0] = 'body '? Document. Body: Document. getelementbyid (arrayvalue [0]);
If (arrayvalue [4] = 'tl ') {var position = 'left top ';}
If (arrayvalue [4] = 'tc ') {var position = 'center top ';}
If (arrayvalue [4] = 'tr') {var position = 'right top ';}
If (arrayvalue [4] = 'ml') {var position = 'left Center ';}
If (arrayvalue [4] = 'mc ') {var position = 'center Center ';}
If (arrayvalue [4] = 'Mr ') {var position = 'right Center ';}
If (arrayvalue [4] = 'bl') {var position = 'left bottom ';}
If (arrayvalue [4] = 'bc') {var position = 'center bottom ';}
If (arrayvalue [4] = 'br ') {var position = 'right bottom ';}
Theelement. style. background = '-WebKit-gradient (radial,' + Position + ', 0,' + Position + ',' + arrayvalue [3] + ', from ('+ arrayvalue [1] +'), to ('+ arrayvalue [2] + '))';
Theelement. style. background = '-moz-radial-gradient (' + Position + ', circle,' + arrayvalue [1] + ', '+ arrayvalue [2] + ''+ arrayvalue [3] + 'px )';
Arrayvalue [0] = 'body '? Theelement. style. Height = '000000': 0;
}
Function lineargradient_css3 (arrayvalue ){
VaR theelement = arrayvalue [0] = 'body '? Document. Body: Document. getelementbyid (arrayvalue [0]);
If (arrayvalue [4] = 'T '){
VaR directionmoz = 'top ';
VaR directionwebkit = 'left top, left bottom ';
VaR colorstop = (arrayvalue [3] * 100)/theelement. offsetheight;
}
If (arrayvalue [4] = 'R '){
VaR directionmoz = 'right ';
VaR directionwebkit = 'right top, Left top ';
VaR colorstop = (arrayvalue [3] * 100)/theelement. offsetwidth;
}
If (arrayvalue [4] = 'B '){
VaR directionmoz = 'bottom ';
VaR directionwebkit = 'left bottom, left top ';
VaR colorstop = (arrayvalue [3] * 100)/theelement. offsetheight;
}
If (arrayvalue [4] = 'l '){
VaR directionmoz = 'left ';
VaR directionwebkit = 'left top, right top ';
VaR colorstop = (arrayvalue [3] * 100)/theelement. offsetwidth;
}
theelement. style. background = '-WebKit-gradient (linear,' + direwebwebkit + ', color-stop (0, '+ Arrayvalue [1] +'), color-stop ('+ colorstop +' %, '+ arrayvalue [2] +') ';
theelement. style. background = '-moz-linear-gradient (' + directionmoz + ',' + arrayvalue [1] + ',' + arrayvalue [2] + ''+ colorstop + '%) ';
arrayvalue [0] = 'body '? Theelement. style. Height = '000000': 0;
}