通过了解行高,您可以将高度设置为等于 3 行。然后使用一些简单的 jQuery,我们可以让它在点击阅读更多时更改为自动高度。
这是一个工作示例:http:
//jsfiddle.net/Nct6e/
html:
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam viverra, justo in ornare lobortis, tortor dolor tempus nunc, a fermentum dui justo nec tortor. Nunc dapibus auctor accumsan. Nullam faucibus tortor erat, quis ultrices metus tincidunt sagittis. Vestibulum nec urna metus. Phasellus lorem ligula, hendrerit nec magna vel, elementum molestie tortor. In sapien ante, rutrum posuere malesuada ac, mattis a purus. Mauris at diam sem. Praesent ac neque aliquet, pharetra est a, gravida lorem. Cras eget risus velit. Praesent sed tempor orci. Ut eu metus sit amet lectus imperdiet aliquet. Curabitur at leo erat. Mauris magna est, consectetur tincidunt pharetra quis, lobortis eu erat. Phasellus egestas lorem quis augue hendrerit eleifend in ac leo. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nam ac arcu nec orci rutrum aliquet non at augue.</p>
<div class="read-more">Read more</div>
</div>
jQuery:
(function() {
var expanded = 0;
$(".content .read-more").on("click", function() {
if(expanded == 0) {
$(this).siblings("p").css("height", "auto");
$(this).text("Show less");
expanded = 1;
} else {
$(this).siblings("p").css("height", "60");
$(this).text("Show more");
expanded = 0;
}
})
})();
CSS:
.content {
background: #eee;
padding: 10px;
line-height: 20px;
}
.content p {
margin: 0;
padding: 0;
overflow: hidden;
height: 60px;
}
.content .read-more {
text-align: center;
font-weight: bold;
cursor: pointer;
}