简体   繁体   English

如何用半透明图像覆盖div?

[英]How can I cover a div with a semi-transparent image?

I want to cover a <div> so it looks like it has a semi-transparent block over it. 我想覆盖一个<div>所以看起来它上面有一个半透明的块。

I am using jQuery but I do not know how to do it. 我正在使用jQuery但我不知道该怎么做。

Any help thanks... 任何帮助谢谢......

If you have another div , you can just get the dimensions + position like 如果你有另一个div ,你可以得到尺寸+位置

var $somediv = $('#some_div_element'),
    pos      = $.extend({
       width:    $somediv.outerWidth(),
       height:   $somediv.outerHeight()
    }, $somediv.position());

and then use it while dynamically creating an overlay 然后在动态创建叠加层时使用它

$('<div>', {
    id:    'overlay',
    css:   {
        position:         'absolute',
        top:              pos.top,
        left:             pos.left,
        width:            pos.width,
        height:           pos.height,
        backgroundColor:  '#000',
        opacity:          0.50
    }
}).appendTo($somediv);

Example: http://www.jsfiddle.net/GkFu4/1/ 示例: http//www.jsfiddle.net/GkFu4/1/

You could use CSS3 to do this. 您可以使用CSS3来执行此操作。 Position a <div> absolutely on top of it and give the <div> a background of: rgba(255,255,255,.3) . <div>绝对放在它上面并给<div>一个背景: rgba(255,255,255,.3)

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

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