CSS的不透明度也可以轻松影响其他元素

Yan Yi

我的页面中间有一个文本形式。目的是使用css不透明度过渡通过淡入淡出切换背景图像。(我会经常切换背景图片)

目前,通过使用两层背景图像,它可以很好地工作。要在底层显示新图像,请淡出顶层(不透明度1到0)。要在顶层显示新图像,请在顶层淡入淡出(不透明度0到1)。

问题是我的文本表单随着顶层淡入淡出-我希望它保持可见状态。如何使其不受褪色影响?

尝试解决此问题:

  1. 将z-index设置为#searchForm input或设置.formDiv为999999,以为这样会将表单放在层次结构的顶部,这样它就不会受到下面的过渡的影响。但是,没有用。

  2. 将位置设置为#searchForm input设置.formDiv为绝对。http://www.w3schools.com/css/css_positioning.asp中,“从正常流程中删除了绝对定位的元素。文档和其他元素的行为就像不存在绝对定位的元素一样。”

  3. CSS3的此stackoverflow发布交替表行的不透明度会影响文本,而背景说明子元素也会受到不透明度的影响。我试着将装有背景图像的DIVformDiv类,以便它不会是一个孩子。但是,即使没有不透明度,这也将使该表格被顶部图像覆盖。

在此处输入图片说明

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;}
传奇

我做了一些小提琴,您可能会得到启发,我只是使用一个类来切换不透明度,并将它们置于绝对位置的形式下,希望对您有所帮助:)

然后在jQuery中使用click函数来切换效果。

CSS:

form {
    position: relative;
    z-index: 10;
}

#background1 {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: lightblue;
    -webkit-transition: all 0.5s ease-in-out;
    -moz-transition: all 0.5s ease-in-out;
    -ms-transition: all 0.5s ease-in-out;
    -o-transition: all 0.5s ease-in-out;
    transition: all 0.5s ease-in-out;
}

#background2 {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: lightgreen;
    -webkit-transition: all 0.5s ease-in-out;
    -moz-transition: all 0.5s ease-in-out;
    -ms-transition: all 0.5s ease-in-out;
    -o-transition: all 0.5s ease-in-out;
    transition: all 0.5s ease-in-out;
}

.hide {
    opacity: 0;
}

http://jsfiddle.net/9jb68w2o/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

css悬停以更改其他元素的不透明度值

css不透明度影响兄弟不透明度

受不透明度影响的元素的堆叠顺序

我可以使用不透明度仅影响段落的背景,而不影响文本和其他东西,仅影响背景吗?

在div中其他元素悬停时更改图像不透明度

使用jQuery降低div其他元素的不透明度

如何更改输入字段中焦点上其他元素的不透明度

CSS过渡不透明度仅从0到1,或其他过渡效果

CSS悬停img不适用于其他类不透明度

如何更改CSS中元素的不透明度?

CSS重叠元素和不透明度问题

CSS 元素过渡/动画:不透明度问题

CSS背景不透明度

将鼠标悬停在按钮上 -> 其他元素上的不透明度

如何仅将 CSS 中的背景图像设置为不透明度而不是其他任何内容

不透明度较低的背景,且不影响内容(且不使用伪元素)

应用不透明度而不影响子元素

CSS3:不透明度与过滤器不透明度?

影响子文本的背景不透明度

CSS背景图片不透明度会影响其中的子级

我的一张图片不受CSS不透明度的影响

如何使用此jQuery来影响CSS的不透明度

Edge Mobile忽略了CSS不透明度,但在其他移动和桌面浏览器上均采用了CSS不透明度

自定义动画将元素的不透明度变为0,但是psedo元素的不透明度可以保持为1

更改视图的不透明度/透明度并也影响子视图

与其他Transform函数异步制作不透明度动画

添加不透明度悬停过渡会使其他图像闪烁

如何在(不超过)其他图层下创建 div 不透明度

CSS-对主元素的不透明度而不是伪元素(:: after,:: before)