[英]How to set 'auto' height in JQuery animate
问题:我有一个固定宽度的盒子(div),但高度(自动)未知。
我需要使用JQuery动画功能打开/关闭该框。
问题是在Open事件上(也已在代码中注释),我需要设置自动高度,但我不能这样做。 有人可以帮忙将高度设置为自动吗?
JSFiddle: http : //jsfiddle.net/7m5Qa/下面也给出了代码:
的HTML
<button id="open">open</button>
<button id="close">close</button>
<div id="textselector-body">
a<br/>
b<br/>
c<br/>
</div>
Java脚本
$(document).ready(function(){
$("#open").click(function(){
$("#textselector-body").animate({
height:'100px' //here is problem. I need it 'auto'.
},2000);
});
$("#close").click(function(){
$("#textselector-body").animate({
height:'0'
},2000);
});
});
您是否尝试过slideDown和slideUp? :
$(document).ready(function(){
$("#open").click(function(){
$("#textselector-body").slideDown(2000);
});
$("#close").click(function(){
$("#textselector-body").slideUp(2000);
});
});
jsFiddle: http : //jsfiddle.net/7m5Qa/2/
$(document).ready(function(){
var H = $("#textselector-body").height();
$("#open").click(function(){
$("#textselector-body").animate({
height:'100px'
},2000);
});
$("#close").click(function(){
$("#textselector-body").animate({
height:H
},2000);
});
});
编辑:
不确定我的问题对吗? 如果您只是想切换它,则可以执行以下操作:
$(function(){
$("#open, #close").click(function(e){
$("#textselector-body")[e.target.id=='open'?'slideDown':'slideUp'](2000);
});
});
您可以在jQuery中使用.slideUp和.slideDown方法:
$("#open").click(function(){
$("#textselector-body").slideDown('slow')
});
$("#close").click(function(){
$("#textselector-body").slideUp('slow')
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.