admin管理员组文章数量:1397093
I want to change z-Index on button click. I have a static image and a dynamic div. Now I want to send the #imgDiv1 behind the static image. But I don't know how to do this. I tried everything but all in vain.
Here is the live demo. jholo I want to implement this concept in this web application.
Here is my mark up
<div id="safeZone">
<img src="default.png" />
<div id="imgDiv1">
<img src="myPic.jpg" />
</div>
</div>
<input id="back" type="button" value="Send To Back"/>
<input id="front" type="button" value="Bring To Front"/>
jQuery
$(document).ready(function(){
$("#back").click(function(){
$("#imgDiv1").css("z-index","-10");
});
$("#front").click(function(){
$("#imgDiv1").css("z-index","10");
});
});
I want to change z-Index on button click. I have a static image and a dynamic div. Now I want to send the #imgDiv1 behind the static image. But I don't know how to do this. I tried everything but all in vain.
Here is the live demo. jholo. I want to implement this concept in this web application.
Here is my mark up
<div id="safeZone">
<img src="default.png" />
<div id="imgDiv1">
<img src="myPic.jpg" />
</div>
</div>
<input id="back" type="button" value="Send To Back"/>
<input id="front" type="button" value="Bring To Front"/>
jQuery
$(document).ready(function(){
$("#back").click(function(){
$("#imgDiv1").css("z-index","-10");
});
$("#front").click(function(){
$("#imgDiv1").css("z-index","10");
});
});
Share
Improve this question
edited Mar 27, 2014 at 0:25
Fazil Mir
asked Mar 26, 2014 at 23:18
Fazil MirFazil Mir
8332 gold badges10 silver badges25 bronze badges
5
-
1
Do you have other CSS that goes with this that sets the
position
property?z-index
has no effect on objects with theposition
property set to something likeabsolute
orfixed
. – jfriend00 Commented Mar 26, 2014 at 23:21 - no. I dont have other css. :( – Fazil Mir Commented Mar 26, 2014 at 23:25
-
What version of jQuery are you using? Seems to work in this example: jsfiddle/g9XbD Note the css that sets
position: absolute
. – Rick Dailey Commented Mar 26, 2014 at 23:25 - 1.10.2. I don't understand whats wrong with this. – Fazil Mir Commented Mar 26, 2014 at 23:29
- You must set position, as explained in answer bellow - something like this> jsfiddle/DDLp2/1 – sinisake Commented Mar 26, 2014 at 23:30
2 Answers
Reset to default 5The z-index
style property only takes affect on elements that are not position: static
. So, you will need to set the position
property before you can see the impact of z-index
.
HTML:
<button id="up">Up</button><button id="down">Down</button>
<div id="container">
<img id="img1" src="http://dummyimage./300x200/ff0000/fff.jpg">
<img src="http://dummyimage./300x200/00ff00/fff.jpg">
</div>
CSS:
#container img {position: absolute;}
#container {height: 200px; width: 300px;}
Code:
$("#up").click(function(){
$("#img1").css("z-index", -10);
});
$("#down").click(function(){
$("#img1").css("z-index",10);
});
Working demo: http://jsfiddle/jfriend00/5Efm3/
Edit - Responding to your additional edit:
To achieve the effect you're looking for, you will also need the snowflake like image to have transparency inside the snowflake shape thus allowing the behind image to show through which means it can't be a JPG,. It could be GIF or PNG.
I used opacity instead of z-index... is that helpful?
$("#down").click(function(){
$("#img1").css("opacity", 0.0);
$("#img2").css("opacity", 1.0);
});
$("#up").click(function(){
$("#img2").css("opacity",0.0);
$("#img1").css("opacity", 1.0);
});
the js fiddle link is.. http://jsfiddle/6rwBR/ hopefully this helps
本文标签: javascriptHow to change zIndex of an element on click in jqueryStack Overflow
版权声明:本文标题:javascript - How to change zIndex of an element on click in jquery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744149136a2592979.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论