【发布时间】:2015-02-19 22:40:33
【问题描述】:
我的页面中间有一个文本表单。目的是使用 css 不透明度过渡通过淡化来切换背景图像。 (我会经常切换背景图片)
目前通过使用两层背景图像使其运行良好。为了在底层显示新图像,我们淡出顶层(不透明度 1 到 0)。为了在顶层显示新图像,我们在顶层淡入淡出(不透明度 0 到 1)。
问题是我的文本表单与顶层一起消失 - 我希望它保持可见。如何让它不受褪色的影响?
尝试解决这个问题:
将
#searchForm input或.formDiv的z-index 设置为999999,认为这会将表单放在层次结构的顶部,因此它不会受到下面的转换的影响。但是,没有用。将
#searchForm input或.formDiv的位置设置为绝对位置。来自http://www.w3schools.com/css/css_positioning.asp, “绝对定位的元素从正常流程中移除。文档和其他元素的行为就像绝对定位的元素不存在一样。”这篇 stackoverflow 帖子 CSS3 Alternating table rows opacity affects text as well as background 说子元素也受到不透明度的影响。我尝试将包含背景图像的 div inside 放置在
formDiv类中,这样它就不会是孩子了。但这会得到顶部图像覆盖的表格,即使没有不透明度。
function changeBackground(newUrl) {
//check which layer is currently activated
if ($('#background-image-top').hasClass('transparent')) {
//place new image over top layer
$('#background-image-top').css('background-image', 'url(' + newUrl + ')');
//fade in new image
$('#background-image-top').toggleClass('transparent');
} else {
//place new image over bottom layer
$('#background-image-bot').css('background-image', 'url(' + newUrl + ')');
//fade out old image
$('#background-image-top').toggleClass('transparent');
}
}
#background-image-top {
background-image: url("../images/default.jpg");
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
-webkit-background-size:cover;
-moz-background-size:cover;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out; }
#background-image-bot {
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
-webkit-background-size:cover;
-moz-background-size:cover;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;}
.transparent {
opacity: 0.25;}
.formDiv {
background-color: red;
max-width: 500px;
height: 50px;
margin-left: auto;
margin-right: auto;
margin-top: 35%;}
#searchForm input {
width: 300px;
height: 30px;
font-size: 18px;}
【问题讨论】:
-
不透明度会影响容器中的所有内容。你需要把它们分解成自己的。我假设这是问题所在,但我不知道您的 HTML 是什么样的。
-
尝试添加html!不知何故,它只是不会显示
-
只有 .transparent 有 opacity 属性,这甚至不在 HTML 中,如果你说顶层的不透明度改变了,那么你的表单和里面的东西当然会改变,因为它们都是在同一个容器中。
标签: javascript html css