为 div 创建线性透明渐变
我想为 div 创建一个线性透明渐变。有什么办法可以用 jquery 做到这一点吗?或者我应该使用其他库,例如 raphaeljs?我想实现如下效果:
I'd like to create a linear transparent gradient to a div. Is there any way to do that with jquery? Or should I use some other library like raphaeljs? I'd like to achieve an effect like the following:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
为什么不保持轻便并兼容浏览器呢?
Why not keep it light and browser compatible.
您可以使用 CSS3 来做到这一点:
例如
http://www.webdesignerwall.com/教程/cross-browser-css-gradient/
http://gradients.glrzad.com/
http://www.colorzilla.com/gradient-editor/
http://css-tricks.com/css3-gradients/
You can do it with CSS3:
E.g.
http://www.webdesignerwall.com/tutorials/cross-browser-css-gradient/
http://gradients.glrzad.com/
http://www.colorzilla.com/gradient-editor/
http://css-tricks.com/css3-gradients/
我使用 jquery 和 112 个 div 创建了它。一个父 div 包含 10 行文本 div,每个 div 更透明,一个背景 div 包含 100 个 div,每个 div 更透明。
http://jsfiddle.net/generalhenry/bDd5w/
I created it using jquery and 112 divs. A parent div for the ten lines of text divs each more transparent, and a background div with 100 divs each more transparent.
http://jsfiddle.net/generalhenry/bDd5w/
这里棘手的一点是示例中的渐变均匀地映射到文本和容器。正如很多人所展示的那样,将透明渐变映射到背景属性很容易,但这将使文本保持不变。
不幸的是,我认为没有任何直接的方法可以在不做出一些妥协的情况下获得您想要的渐变效果,根据您的需求,它们可能是值得的解决方案。为此,这里有两个示例,说明如何实现示例图像中所示的效果,均使用
。
1.伪造它
JSLint 上的演示。
这很简单,您将
<文本块上的 canvas>
元素,并绘制从完全透明到文本块下方背景颜色的渐变。它并不是真正透明的,因此它实际上不会显示下面的任何信息,但如果您尝试淡入淡出为纯色、预定颜色,那么这种方法效果很好。2.画布文本
JSLint 上的演示
这个示例更复杂,但完全复制了所示的透明效果在你的例子中。本质上,它完全抛弃了 HTML 文本块,只是将整个 shabang 绘制到
上。然而,它有一些缺点:
画布似乎不喜欢
换行文本,因此您必须指定
单独的线条。
画布文本的浏览器实现可能仍然有些模糊。
辅助功能和 用于在运行时将带有文本的常规 DOM 元素转换为此解决方案。
The tricky bit here is that the gradient in your example is evenly mapped to the text as well as the container. It's easy to map a transparent gradient to the background property, as lots of people have shown, but that will leave the text unchanged.
Unfortunately, I don't think there's any straightforward way to get the gradient effect that you want without making some compromises, depending on your needs they may be worthwhile solutions. So to that end, here are two examples of how to achieve the effect shown in your example image, both using the
<canvas>
.1. Faking it
Demo on JSLint.
This is simple, you position a
<canvas>
element over your text block and draw in a gradient from fully transparent to the color of the background below the text block. It's not really transparent, so it doesn't actually reveal any information below, but if you're trying to fade to a solid, predetermined color, then this works pretty well.2. Canvas text
Demo on JSLint
This example is more complicated, but fully replicates the transparent effect shown in your example. Essentially, it ditches the HTML text block entirely, and just draws the whole shabang onto the
<canvas>
. However, it has some downsides:The canvas doesn't seem to like
wrapping text, so you'd have to specify
individual lines.
Canvas text may still have somewhat murky browser implementations.
Accessibility & SEO, though you could easily write a jQuery plugin for transforming regular DOM elements with text into this solution at runtime.
不确定你到底在寻找什么,但看看 Gradienter jQuery 插件。
Not sure what exactly you are looking for but take a look at Gradienter jQuery plugin.
我使用Raphael js http://www.jsfiddle.net/pahnin/fsdnW/4/ 创建了这个
看看你是否喜欢它
** 编辑 **
我通过添加一个带有渐变的矩形并使其与 div 大小相同来创建它
I created this using Raphael js http://www.jsfiddle.net/pahnin/fsdnW/4/
checkout if u like it
** edit **
I created it by adding a rectagle with gradient and making it as same size as the div
正如 bArmageddon 指出的那样,公认的解决方案并没有解决问题——它只是淡化了背景。一个简单的解决方案是使用 :before 或 :after 在文本上添加渐变:
As bArmageddon pointed out, the accepted solution does not solve the problem - it just fades the background. A simple solution would be to use :before or :after to add the gradient over the text: