Set transparency of a DIV and its contents using jQuery

别来无恙 提交于 2019-12-21 06:59:06

问题


What is the best way to set the transparency of a HTML DIV element and its contents using jQuery?


回答1:


$('#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

<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.




回答2:


Another option - Save your keyboard and use fadeTo:

$('#someDiv').fadeTo("slow",0.5);



回答3:


As theIV said you can use the css method, but as an alternative you can use 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.




回答4:


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%



来源:https://stackoverflow.com/questions/1309297/set-transparency-of-a-div-and-its-contents-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!