Askthecssguy.com 上的一篇文章展示了如何使用固定背景更改/反转滚动图像:http://askthecssguy.com/articles/mike-asks-the-css-guy-about-a-scrolling-trick-with-background-images/
我的目标进一步推进这个概念让图像浮动在其他元素(在本例中为图像)上。
您可以在此处查看结果: http://playground.iamkeir.com/invert-logo/v2 /
但是,我的实现使用了多余的元素,因此,我想知道是否有人有其他方法来实现此目的的想法/建议?
JavaScript 当然是一个选择,但我担心它不够简洁/优雅。有人还建议使用 Canvas。
欢迎任何想法!谢谢。
An article over on askthecssguy.com shows how to change/invert an image on scroll using fixed backgrounds: http://askthecssguy.com/articles/mike-asks-the-css-guy-about-a-scrolling-trick-with-background-images/
My goal takes this concept further by having the image float over other elements (in this case images).
You can see the result here: http://playground.iamkeir.com/invert-logo/v2/
However, my implementation uses superfluous elements and, so, I was wondering if anyone had any ideas/suggestions of another way to achieve this?
Javascript is certainly an option but I worry it would not be lean/elegant. Someone also suggested using Canvas.
Any ideas welcomed! Thank you.
发布评论
评论(2)
您可以通过使用 :after CSS 伪元素来避免额外的标记。因此,您的最终标记将如下所示:
更改后的 CSS 将是:
请注意,那里有
.after
类。这是为了让它在 IE<8 中工作,遗憾的是,它需要使用表达式:虽然通常不鼓励使用表达式,但这个不应该对性能产生太大影响,因为它仅被完全评估一次,并且当元素创建后,条件返回 false。
不过,有一个陷阱。如果 IE8 在 IE8/IE8 模式下工作,伪元素将位于图像下,除非您为后者设置负
z-index
,这并不总是可以接受的。您可以在此处查看工作示例。
You can avoid extra markup by using :after CSS pseudo element. Thus, your final markup will look like:
And the altered CSS will be:
Notice that there is
.after
class there. This is to make it work in IE<8, which, sadly, requires to use an expression:While using expressions is generally discouraged, this one shouldn't affect the performance too much, since it is fully evaluated only once, and when the element is created, the condition returns false.
There is one pitfall, though. If IE8 works in IE8/IE8 mode, the pseudo-elements will be under the images, unless you set negative
z-index
for the latter, which isn't always acceptable.You can look at working example here.
使用当前代码,您想要做的事情是完全可能的,您只需要使用绝对定位来移动内容。例如使用测试页面 http://askthecssguy.com/examples/fixedBackgroundImages/example01.html 你可以修改 .header 类并使其像这样。
这样
做将使文本漂浮在图像上。更进一步,您可以将透明 PNG 插入到它自己的 DIV 中,并将其浮动到页面上的任何位置并保持其位置固定,而不是使用背景图像。您可以查看http://www.w3schools.com/css/css_positioning.asp一些例子。
希望有帮助!
维吉尔
what you're trying to do is totally possible using the current code you just need to use absolute positioning to move the content around. For example using the test page http://askthecssguy.com/examples/fixedBackgroundImages/example01.html you can modify the .header class and make it like this.
}
Doing this will make the text float over the images. Going a step further instead of using a background image you could insert a transparent PNG into it's own DIV and float it over any position on the page and keep it's position fixed. You can checkout http://www.w3schools.com/css/css_positioning.asp for some examples.
Hope that helps!
Virgil