【发布时间】:2014-02-20 14:35:26
【问题描述】:
如何更新我的代码,这样我就可以用slideToggle() 更改<div> 的高度,现在它几乎是正确的,但是当我单击一个"read_more" 并单击另一个时它不是正确的。我需要slideDown 何时显示隐藏p 并将主要<div> 更改为特定高度,slideUp 何时隐藏此p 并更改<div> 以前的特定高度("... Read More" 基础@ 987654332@高度,"... Read Less"新.main高度)。
请参阅demo 或下面的代码。
HTML
<div class="main">
<!-- === h1 === -->
<h1>Lorem ipsum dolor sit amet</h1>
<p>Ut enim ad minim veniam, quis nostrud exercitation.</p>
<p class="more">Duis aute irure dolor in reprehenderit in voluptate.</p>
<a class="read_more">... Read More</a>
<!-- === h2 === -->
<h2>Consectetur adipisicing elit</h2>
<p>On the other hand, we denounce with righteous.</p>
<p class="more">Gemoralized by the charms of pleasure of the moment.</p>
<a class="read_more">... Read More</a>
<!-- === h3 === -->
<h3>Sed do eiusmod tempor incididunt</h3>
<p>The wise man therefore always holds.</p>
<p class="more">Et harum quidem rerum facilis est et expedita distinctio.</p>
<a class="read_more">... Read More</a>
</div>
CSS
.main {
border:1px solid blue;
height: 260px;
padding: 5px;
}
h1,h2,h3 {
font-size: 18px;
color: #96bc33;
}
p {
margin: 0;
font-size: 13px;
}
.more {
display:none;
}
.read_more {
cursor: pointer;
color: red;
}
JavaScript
var rm = $(".read_more"),
moreText = "... Read More",
lessText = "... Read Less",
i = 0 ;
rm.click(function () {
var $this = $(this);
rm.not($this).text(moreText).prev(".more").slideUp();
$this.text($this.text() == moreText ? lessText : moreText).prev(".more").slideToggle("normal");
$(".main").animate({height:(++i % 2) ? 280 : 260}, "normal");
});
【问题讨论】:
-
你必须设置一个特定的高度吗?如果没有,只需更改您的 css
.main { height:auto; ... }并删除第二个click函数。 -
感谢@Matt,但是我需要一个特定的高度
-
我知道你会想要一个特定的高度 :-) 现在所有现有答案都不正确。
-
@onlinepch 特定的
min-height可以吗? -
@andyb 非常感谢您的帮助,乍一看没问题,但是当
slideUp我需要以前的高度时。这个想法是当slideToggleheight 改变它。
标签: javascript jquery show-hide slidetoggle