【问题标题】:Setting opacity of child element independently from parent element独立于父元素设置子元素的不透明度
【发布时间】:2011-01-28 01:36:40
【问题描述】:

我使用以下代码设置了fiddle

html:

<div id="content">
    <input id="splash_button" type="button" value="Splash!" />
    <p>OSEIFNSEOFN SOE:NF:SOERNG :SOJld;kkng d;ljrng so;ern gsejng ;seorjse;ongsod;jng;s jdg;ske\ ;sej se gdsrgn sd;orjngsd;oj go;ser o;s en;o jnse;orng;sekorg ;ksjdr ;kgsjurd; gjnsdrgj; s ;hg;kuhg k;sgksdgblsregilsebnvsfdnv sa;kljg ;khg ;zkljdng ;kjsgr; unbzsd;kjgb zk;j xcnbv;kjzb ;kjgrb snbz;gkljznbs;d,jbnzs;dkjvbz;sljbd ;zksjn ;kzjsbng ;kjsbk;zejr ;kgsjg ;kzsjbr ;kjszrb ;zkojg ;oszkrg ;ozsrb;ouszb </p>
</div>

css:

#content{
    z-index: 0;
}

javascript(jquery):

$('#splash_button').click(function(){
    $('#content').append($('<div id="splash"><a id="hideSplash">hide</a></div'));

    // display splash window
    var splash_width = 200;
    var splash_height = 200;
    $('#splash').css({
        'z-index': 1,
        'width': splash_width + 'px',
        'height': splash_height + 'px',
        'position': 'absolute',
        'left': Math.floor(($(window).width() - splash_width)/2) + 'px',
        'top': Math.floor(($(window).height() - splash_height)/2) + 'px',
        'z-index': 1,
        'border': '1px solid',
        'background-color': '#99ccff',
        'opacity': '1.0'
    });

    // set the content's opacity and disabled the input button
    $('#content')
        .css({
            'opacity': '0.4'
        })
        .children()
            .attr('disabled', 'disabled');

    // when everything's over, reset properties
    $('#hideSplash').click(function(){
        $('#splash').remove();
        $('#content')
            .css({
                'opacity': '1.0'
            })
            .children()
                .removeAttr('disabled');
    });
});

我希望启动 div 的不透明度为 1,内容 div 的不透明度为 0.4,但是,单击按钮后,两个 div 的不透明度均为 0.4。

非常感谢任何帮助。

【问题讨论】:

标签: html css opacity


【解决方案1】:

您应该将#splash 附加到正文或#content 之外的另一个div。

当您在 css 中为元素设置不透明度时,他的所有子元素都具有相同的值。 在您的示例中,#splash 的不透明度为 0.4,就像 #content 一样。

尝试使用 $('body').append 而不是 $('#content').append

【讨论】:

  • 我之前曾多次与这种行为作斗争,但从未找到让子元素的不透明度值大于其父元素的方法。
猜你喜欢
  • 2014-07-04
  • 1970-01-01
  • 1970-01-01
  • 2013-04-27
  • 2014-04-25
  • 2018-10-12
  • 2013-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多