简体   繁体   English

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

[英]css opacity ease in out affecting other elements too

My page has a text form in the middle. 我的页面中间有一个文本形式。 The aim is to use css opacity transitions to switch background images by fading. 目的是使用css不透明度过渡通过淡入淡出切换背景图像。 (I'll be switching background images quite often) (我会经常切换背景图片)

Currently got it working nicely by using two layers of background images. 目前,通过使用两层背景图像使其运行良好。 To display a new image at the bottom layer, we fade out the top layer (opacity 1 to 0). 要在底层显示新图像,请淡出顶层(不透明度1到0)。 To display a new image at the top layer, we fade in the top layer (opacity 0 to 1). 要在顶层显示新图像,请在顶层褪色(不透明度0到1)。

The problem is that my text form fades along with the top layer - which I want it to stay visible. 问题是我的文本表单随着顶层淡入淡出-我希望它保持可见状态。 How do I make it unaffected by the fading? 如何使其不受褪色影响?

Attempts to solve this: 尝试解决此问题:

  1. Setting z-index of either #searchForm input or .formDiv to 999999, thinking that this will put the form right at the top of the hierachy so it would be unaffected by transitions below. #searchForm input.formDiv z-index设置为999999,以为这会将表单放在.formDiv的顶部,因此不受下面的转换的影响。 However, didn't work. 但是,没有用。

  2. Setting position of #searchForm input or .formDiv to absolute. #searchForm input.formDiv位置设置为绝对位置。 From http://www.w3schools.com/css/css_positioning.asp , "Absolutely positioned elements are removed from the normal flow. The document and other elements behave like the absolutely positioned element does not exist." http://www.w3schools.com/css/css_positioning.asp中 ,“从正常流程中删除了绝对定位的元素。文档和其他元素的行为就像不存在绝对定位的元素一样。”

  3. This stackoverflow post CSS3 Alternating table rows opacity affects text as well as background says that child elements are affected by opacity too. CSS3的此stackoverflow发布交替表行的不透明度会影响文本,而背景说明子元素也会受到不透明度的影响。 I tried placing the div containing the background images inside the formDiv class so that it wouldn't be a child. 我试着将装有的背景图像的DIV formDiv类,以便它不会是一个孩子。 But this will get the form covered by the top image, even without opacity on. 但是,即使没有不透明度,这也将使该表格被顶部图像覆盖。

在此处输入图片说明

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

I have made a little fiddle where you might can get inspiration, i just use a class to toggle the opacity and them put under the form with position absolute, hope it helps :) 我做了一些小提琴,您可能会得到启发,我只是使用一个类来切换不透明度,并将它们置于绝对位置的形式下,希望对您有所帮助:)

and then use a click function with jQuery to toggle the effect. 然后在jQuery中使用click函数来切换效果。

the css: 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/ http://jsfiddle.net/9jb68w2o/

+++ If you feel better to use css opacity transitions to switch background images by using only one div ie) #background1, you can use this code +++如果您觉得只使用一个div(例如,#background1)使用css不透明度转换来切换背景图像更好,则可以使用此代码

 $(document).ready(function() { $('#toggle').click(function(e) { e.preventDefault(); $('#background1').toggleClass('color1'); }); }); 
 body { background-color: #f8f8f8; color: #555; }.container { position: relative; margin: 30px auto; padding: 20px; width: 200px; height: 150px; background-color: #fff } form { position: relative; z-index: 10; } input[type=text] { margin: 10px 0; padding: 5px; width: calc(100% - 10px); font-size: 15px; outline: none; } input[type=submit] { width: 100%; text-transform: uppercase; font-size: 15px; background-color: #333; color: #fff; border: none; cursor: pointer; } #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; } #background1.color1 { background-color: lightgreen; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div class="container"> <div id="background1"></div> <form> <h2>Awesome form!</h2> <input type="text" placeholder="Some text here" /> <input id="toggle" type="submit" value="Change now!" /> </form>`enter code here` </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM