【发布时间】:2014-02-06 06:07:28
【问题描述】:
这是我的标记:
<div class="content">
<div class="content_expander">
{newsrow.MESSAGE}
</div>
<a href="#" class="expand">Rozwiń</a>
</div>
我用来列出我在论坛上的帖子。
.content div默认高度为500px,.content_expander为文章高度(有时高于父div)。
我想要实现的是能够展开和折叠.content div 以显示/隐藏整个帖子。
所以我已经尝试过:
$(document).ready(function(){
$("article .content").each(function() {
var boxHeight = 500;
var insideHeight = $(this).find(".content_expander").height();
//alert(insideHeight);
if(boxHeight<insideHeight) {
$(this).children(".expand").css({
opacity: 0,
display: 'inline-block'
}).animate({opacity:1},600);
}
});
$('.expand').click(function(){
var elementToChange = $(this).parent().find(".content_expander");
newHeight = elementToChange.height();
elementToChange.parent().animate({
height: newHeight + 'px',
}, 500 );
$(this).addClass('collapse');
$(this).removeClass('expand');
$(this).html('Collapse');
return false;
});
$('.collapse').click(function(){
var elementToChange = $(this).parent().find(".content_expander");
newHeight = elementToChange.height();
elementToChange.parent().animate({
height: '500px',
}, 500 );
$(this).addClass('expand');
$(this).removeClass('collapse');
$(this).html('Expand');
return false;
});
});
你们能帮我改进一下吗? :/
【问题讨论】:
标签: javascript jquery html css