首页 > 语言 > JavaScript > 正文

javascript 线性渐变二

2024-05-06 14:13:27
字体:
来源:转载
供稿:网友
先来IE的,这是最大的用户群,如果这部分开发不出来,基本可以说不用做了。IE虽然有Gradient滤镜,但对比其他浏览器的实现特弱,没有多重渐变(stop-color),不能实现角度渐变,而且还经常失效。我的思路是这样,假如有一个带文本的DIV,要实现多重线性渐变,我们首先得把它里面的文本取出来,然后里面放几个DIV,有几重就放几个,然后把它们渐变。如果是垂直渐变,这好办,什么也不用做,只需设置其滤镜与各个高就行了。如果水平,就让其浮动或绝对定位,放到适当的位置,设置其滤镜与宽。但渐变滤镜竟然会在浮动后或定位后失效,这是在使用透明滤镜时闻所未闻的。没有办法,祭出上古神器table。但设置长与宽时,使用style来设置是不顶用的,一定要用DOM属性。渐变则由它的td元素负责。为了去除table元素与td元素之间,td元素与其内容之间的空白,我们还得用到cellPadding与cellSpacing。

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
safari,chrome与opera打算都用SVG实现。为了减少函数的长度,特意搞了两个辅助函数。
代码如下:
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;i<l;i++,j++){
var 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);
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选