[英]hide show div (but display first 200px)
I am using some code from a fiddle made by a member of StackOverflow. 我正在使用StackOverflow成员制作的一些小提琴代码。 To hide show divs on click.
单击时隐藏显示div。
My issue is I wish to extend this , I will explain. 我的问题是我想延长这个,我会解释。
I have content loading from database, which lets say is approx 500px in height. 我从数据库加载内容,可以说高度约为500px。 But On page load, I only want to show the first 200px height of this content, so user can click "Show More" and the remainder of the div slides down.
但在页面加载时,我只想显示此内容的前200px高度,因此用户可以单击“显示更多”,然后div的其余部分向下滑动。
To keep things tidy, I would like to extend the use of the code , given on here if possible, so that my JS is tidy. 为了保持整洁,我想扩展代码的使用,如果可能的话,在这里给出,以便我的JS很整洁。
The code to hide/show content thus far is: 到目前为止隐藏/显示内容的代码是:
JS JS
$(document).ready(function() {
$("#setup-ofi").click(function() {
$("#closeable-ofi").slideToggle(function() {
if ($(this).is(":visible")) {
$("#setup-ofi").text("Hide Details \u25b2");
}
else {
$("#setup-ofi").text("Show Details \u25bc");
}
});
});
}); });
The HTML HTML
<h3>Property Details - <span class="sub-searchBlue"><a id="setup-ofi" href="javascript:;"> Show Details ▼</a></span></h3>
<div id="closeable-ofi" style="display:none">
content of db shows here
</div>
FIDDLE : http://jsfiddle.net/3x2uG/ FIDDLE: http : //jsfiddle.net/3x2uG/
Use jQuery UI's effects module to animate transitions of the element's height
property between 200 and 500. 使用jQuery UI的效果模块为元素
height
属性的过渡设置200到500之间的动画。
$('#myDiv').animate({height: 500});
Set the style overflow: hidden
on the div so that the "excess" content doesn't spill out or cause a scrollbar to appear when in the 200 pixel state. 设置样式
overflow: hidden
在div上,以便“多余”内容不会溢出或导致在200像素状态下出现滚动条。
Demo at http://jsfiddle.net/alnitak/cnHJw/ , which works even if you don't know how big the underlying div is supposed to be: 在http://jsfiddle.net/alnitak/cnHJw/上进行演示,即使您不知道底层div应该有多大,它仍然有效:
var hidden = false;
$('#show').click(function() {
if (hidden) {
$('#main').stop().animate({height: 20});
$('#show').text('hide...');
} else {
var h = $('#main').get(0).scrollHeight;
$('#main').stop().animate({height: h});
$('#show').text('show...');
}
hidden = !hidden;
});
Find working sample here: http://jsfiddle.net/ezmilhouse/Lt3LQ/ 在这里找到工作样本: http : //jsfiddle.net/ezmilhouse/Lt3LQ/
$("#setup-ofi").click(function() {
if ($("#closeable-ofi").css('height') === "100px") {
$("#closeable-ofi").animate({'height': '500px', 'overflow': 'visible'}, function(){
$("#setup-ofi").text("Hide Details \u25b2");
});
} else {
$("#closeable-ofi").animate({'height': '100px'}, function(){
$("#setup-ofi").text("Show Details \u25bc");
});
}
});
change your css: 改变你的CSS:
#closeable-ofi {
height:100px;
overflow:hidden;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.