admin管理员组文章数量:1321061
I am trying to add opacity to a div.
Here is my Jquery:
$('.redDiv').fadeIn(0, 0.5);
My HTML:
<div class="redDiv" style="background:red;width:20px;height:20px;"> </div>
<div class="divBlue;" style="background:blue;width:20px;height:20px;"> </div>
<div class="divBlack;" style="background:black;width:20px;height:20px;"> </div>
I am trying to add opacity to a div.
Here is my Jquery:
$('.redDiv').fadeIn(0, 0.5);
My HTML:
<div class="redDiv" style="background:red;width:20px;height:20px;"> </div>
<div class="divBlue;" style="background:blue;width:20px;height:20px;"> </div>
<div class="divBlack;" style="background:black;width:20px;height:20px;"> </div>
Share
Improve this question
edited Aug 16, 2011 at 11:10
Lightness Races in Orbit
385k77 gold badges666 silver badges1.1k bronze badges
asked Aug 16, 2011 at 10:14
Rails beginnerRails beginner
14.5k35 gold badges140 silver badges259 bronze badges
2
- Jquery how to add opacity to a div? – Rails beginner Commented Aug 16, 2011 at 10:16
- @Rails: You said that much already. Delan is asking you to ask a real, well-formed, detailed question, not just to repeat the question title which we can already plainly see. – Lightness Races in Orbit Commented Aug 16, 2011 at 11:10
4 Answers
Reset to default 7There are a few different ways to do this:
$('.redDiv').css("opacity", "0.5"); //Immediately sets opacity
$('.redDiv').fadeTo(0, 0.5); //Animates the opacity to 50% over the course of 0 milliseconds. Increase the 0 if you want to animate it.
$('.redDiv').fadeIn(); //Animates the opacity from 0 to 100%
If an element has display:none
fadeTo and fadeIn will both make the element visible before fading it
Something like this ought to help:
$("div").css("opacity", ".7");
The fadeIn
method that you're calling doesn't just apply opacity to an element, but does so from a starting point (transparent) to an endpoint (opaque), essentially animating the element with the transition.
Use fadeTo method
$('.redDiv').fadeTo(0, 0.5);
try this-
$('.redDiv').css("opacity", "0.5");
本文标签: javascriptHow to add opacity to a divStack Overflow
版权声明:本文标题:javascript - How to add opacity to a div? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742090766a2420253.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论