admin管理员组文章数量:1395897
My goal is to to create an animation (css / jquery), so green box slides (grows) over red one (red one is fixed). Then toggle it back : green shrinks and red es back again.
Using ("#green").css("wdith") makes the red one being pushed and I want to avoid that.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
</head>
<body>
<style>
.green {
width:400px;
background-color:green;
display:inline-block;
height:320px;
}
.red{
width:200px;
background-color:red;
display:inline-block;
height:320px;
}
</style>
<div>
<div id="green" class="green">eezez</div>
<div id="red" class="red">eezez</div>
<script>
$(function() {
$("#action").click(function() {
// toggle so green grows and takes red place (red does not move)
// toggle so green shrinks and so red takes his initial place again
});
});
</script>
</div>
</body>
</html>
here is the fiddle:
/
My goal is to to create an animation (css / jquery), so green box slides (grows) over red one (red one is fixed). Then toggle it back : green shrinks and red es back again.
Using ("#green").css("wdith") makes the red one being pushed and I want to avoid that.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
</head>
<body>
<style>
.green {
width:400px;
background-color:green;
display:inline-block;
height:320px;
}
.red{
width:200px;
background-color:red;
display:inline-block;
height:320px;
}
</style>
<div>
<div id="green" class="green">eezez</div>
<div id="red" class="red">eezez</div>
<script>
$(function() {
$("#action").click(function() {
// toggle so green grows and takes red place (red does not move)
// toggle so green shrinks and so red takes his initial place again
});
});
</script>
</div>
</body>
</html>
here is the fiddle:
http://jsfiddle/Sj575/
Share Improve this question edited Aug 5, 2014 at 9:05 Suresh Karia 18.3k19 gold badges68 silver badges85 bronze badges asked Aug 4, 2014 at 16:53 yarekyarek 12.1k31 gold badges131 silver badges250 bronze badges3 Answers
Reset to default 3I'd use a float instead. Try this:
jsFiddle example
.green {
width:400px;
background-color:green;
height:320px;
float:left;
}
.red {
background-color:red;
height:320px;
}
#container {
width:600px;
overflow:hidden;
}
<div id="container">
<div id="green" class="green">eezez</div>
<div id="red" class="red">eezez</div>
</div>
<button id="action">Toggle Green !</button>
Add
$('#green').slideToggle("fast");
then add css property
.green {
width:400px;
background-color:green;
display:inline-block;
position:absolute;
height:320px;
}
JSFIDDLE
Here's what I did:
Like j08691 I also added float:left to both divs and I added display:block and clear both to the button, otherwise it would too be on the left
<body>
<style>
.green {
width:400px;
background-color:green;
float: left;
height:320px;
}
.red{
width:200px;
background-color:red;
float: left;
height:320px;
}
#action{
display: block;
clear:both;
margin-top:2em;
}
</style>
<div>
<div id="green" class="green">eezez</div>
<div id="red" class="red">eezez</div>
</div>
<button id="action" >Toggle Green !</button>
Now what I did was make 2 different functions,to make sure the button wouldn't invoke both events I used the preventDefault(); method. I used animate to increase width and decrease the red using hide() to make it disappear, once you click on 'action' the green will disappear and the red div will take its place
<script>
$(document).ready(function() {
$("#action").one('click', toggleGreen);
// toggle so green grows and takes red place (red does not move)
function toggleGreen(){
$("#green").animate({
width:'600px'
});
$("#red").animate({
width:'100px'
}).hide(2000);
preventDefault();
}
$("#action").on('click', function(){
// toggle so green shrinks and so red takes his initial place again
$("#green").hide(2000);
$("#red").animate({
width:'600px'
}).show(3000);
})
});
</script>
Hope that helps!
本文标签: javascriptJquery how to toggleslide one div over another so it takes its placeStack Overflow
版权声明:本文标题:javascript - Jquery: how to toggleslide one div over another so it takes its place? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744092772a2589729.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论