Home  >  Article  >  Web Front-end  >  javascript linear gradient 2_javascript skills

javascript linear gradient 2_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:44:271016browse

Let’s start with IE, which is the largest user group. If this part cannot be developed, it can basically be said that there is no need to do it. Although IE has a Gradient filter, its implementation is very weak compared to other browsers. It does not have multiple gradients (stop-color), cannot implement angle gradients, and often fails. My idea is this, if there is a DIV with text, to achieve multiple linear gradients, we must first take out the text inside it, and then put several DIVs in it, as many as there are multiple ones, and then gradient them . If it's a vertical gradient, it's easy to do. You don't have to do anything, just set its filter and height. If it's horizontal, make it float or position it absolutely, put it in place, and set its filter and width. But gradient filters actually fail after being floated or positioned, which is unheard of when using transparent filters. There is no other way, so I sacrifice the ancient artifact table. But when setting the length and width, it is not useful to use style to set it. You must use DOM attributes. The gradient is responsible for its td element. In order to remove the space between the table element and the td element, and between the td element and its content, we also have to use cellPadding and cellSpacing.


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

Safari, chrome and opera are planned All implemented using SVG. In order to reduce the length of the function, two auxiliary functions were specially created. The code is as follows:


var createSVG = function(tag){
return document.createElementNS("http://www.w3.org/2000/svg",tag);
};
var attr= function(node,bag){
for(var i in bag ){
if(bag.hasOwnProperty(i))
node.setAttribute(i,bag[i])
}
};
var COSgradient = function(entity,stops,width ,height,type){
var svg = createSVG("svg");
attr(svg,{width:width "px",height:height "px"})
entity.appendChild(svg );
.
var defs = createSVG("defs");
svg.appendChild(defs);
var linearGradient = createSVG("linearGradient");
defs.appendChild(linearGradient );
attr(linearGradient,{id:"nasami",x1:"0%",y1:"0%"})
if(type){
attr(linearGradient,{x2:" 100%",y2:"0%"})
}else{
attr(linearGradient,{x2:"0%",y2:"100%"})
}
for( var i=0,j=0,l=stops.length;ivar offset = stops[i].split(",")[0] "%",
color = stops[i].split(",")[1],
stop = createSVG("stop");
attr(stop,{offset:offset,"stop-color":color} );
linearGradient.appendChild(stop);
}
var rect = createSVG("rect");
svg.appendChild(rect);
attr(rect,{x:" 0px",y:"0px",width:width "px",height:height "px",fill:"url(#nasami)"});
}


firefox Use its private properties: The code is as follows:


var FFgradient= function(entity, stops,width,height,type){
var cssText = ";background: -moz-linear-gradient("
cssText = type? "top,bottom," :"left,right,";
.
for(var i=0,j=0,l=stops.length;ivar offset = stops[i].split(",")[0 ] "%",
color = stops[i].split(",")[1];
cssText = "color-stop(" [offset,color] "),"
}
cssText = cssText.replace(/,$/,"") ") no-repeat;";
entity.style.cssText = cssText "width:" width "px;height:" height "px;"
}

不过今天研磨一下,发现firefox还是支持SVG的线性渐变的,因此纠正我原来的观点。上面的函数只是作用一种实现手段放在这里,它并没有整合到我最终的版本中(虽然它比SVG实现短很多。)这样一来,在老一点版本的firefox中我们也能实现线性渐变了。
下面这个运行框里的渐变效果可在所有主流浏览器中正常运作。

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

再把它做成类。扼要说明一下:它的第一个参数为IE,第二个为哈希。哈希中的各参数都为必选的,width,height的单位为px;type为0或者1,0代表垂直,1为水平;color-stop代表渐变体,由一个字符串数组构成,每个字符串都是由数字加逗号加颜色值组成,数字表代偏移量,单位为%,颜色值可以是red,green等名词,也可以是六位或三位的哈希值。渐变体至少要有一个。
复制代码 代码如下:

new Gradient("gradient",{width:800,height:100,type:0,"color-stop":["0,red",
3."16,orange","32,yellow","48,green","64,blue","80,indigo","100,violet"]})


[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn