使用 jQuery 设置 DIV 及其内容的透明度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1309297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set transparency of a DIV and its contents using jQuery
提问by Keith Adler
What is the best way to set the transparency of a HTML DIV element and its contents using jQuery?
使用 jQuery 设置 HTML DIV 元素及其内容透明度的最佳方法是什么?
回答by theIV
$('#my_element').css({ 'opacity' : 0.7 });
$('#my_element').css({ 'opacity' : 0.7 });
Do you want to actually set the opacity to each of the contained elements as well, or you just want it to 'appear' as if the child elements have the same opacity?
您是否真的想为每个包含的元素设置不透明度,或者您只是希望它“出现”,就好像子元素具有相同的不透明度?
As an example to my question, if you wanted something that sets an element, and each of the children elements, you could do something like this
作为我的问题的一个例子,如果你想要设置一个元素和每个子元素的东西,你可以做这样的事情
html
html
<div id="my_element">
<div>
lorem
</div>
<div>
ipsum
</div>
</div>
jquery
查询
$('#my_element').children().
css({ 'opacity' : 0.25 }).
end().
css({ 'opacity' : 0.25 });
Hope this helps. Cheers.
希望这可以帮助。干杯。
回答by redsquare
回答by subindas pm
Try this properties
试试这个属性
$('#my_div').css("opacity", "0.5");
//Immediately sets opacity
$('#my_div').fadeTo(0, 0.5);
//Animates the opacity to 50% over the course of 0 milliseconds. Increase the 0 if you want to animate it.
$('#my_div').fadeIn();
//Animates the opacity from 0 to 100%
$('#my_div').css("opacity", "0.5");
//立即设置不透明度
$('#my_div').fadeTo(0, 0.5);
//在 0 毫秒的过程中将不透明度设置为 50%。如果要为其设置动画,请增加 0。
$('#my_div').fadeIn();
//动画不透明度从0到100%
回答by Darko Z
As theIV said you can use the css method, but as an alternative you can use animate:
正如 theIV 所说,您可以使用 css 方法,但作为替代,您可以使用 animate:
$('#my_element').animate({ opacity: 0.5 }, 100);
this will animate the opacity of you div (and its contents) to 0.5 (from whatever it was to begin with) in 100 milliseconds.
这将在 100 毫秒内将您的 div(及其内容)的不透明度设置为 0.5(从任何开始)。