【发布时间】:2016-09-28 03:56:43
【问题描述】:
有人可以帮我解决这个问题吗? 我只是学习 CSS 和 Js,所以我真的不知道该怎么做。
这个东西在显示内容之前上下 2x,然后在隐藏内容之前再次上下。
CSS:
.pureyad {
max-width: 600px;
line-height: 1.1em;
font-family: 'Roboto Slab';
overflow: hidden;
background: #46988f;
font-size: 14px;
height: 120px;
padding-left: 20px
}
.purgex {
display: block;
background: transparent;
border: none;
border-top: 1px solid #222;
width: 585px;
max-width: 100%;
font-family: 'baloo bhai';
font-size: 15px
}
JS:
function ssss() {
var text = $('.pureyad'),
btn = $('.purgex'),
h = text[0].scrollHeight;
if(h > 120) {
btn.addClass('less');
}
btn.click(function(e)
{
e.stopPropagation();
if (btn.hasClass('less')) {
btn.removeClass('less');
btn.addClass('more');
btn.text('Show less');
text.animate({'height': h});
} else {
btn.addClass('less');
btn.removeClass('more');
btn.text('Show more');
text.animate({'height': '120px'});
}
});
}
HTML:
<div class="pureyad">
<p>Lorem ipsum dolor sit amet..</p>
</div>
<center><button class="purgex" onclick="ssss()">Show more</button></center>
上下动图:http://i.giphy.com/l0HlJmVHLDGnSZWuY.gif
我从这里的回复帖子中得到这个脚本: https://stackoverflow.com/a/38680734/5118751 该脚本在 Jsfiddle 上运行良好,当我直接在 html 上加载脚本时也是如此。
【问题讨论】:
-
html 段落中没有太多内容 - 显示更多内容不会展开,因为它会显示所有内容..