【发布时间】:2017-09-21 16:39:53
【问题描述】:
我想通过单击按钮显示更多文本。 我该如何制作动画?我通过单击按钮添加一个新类。我希望它通过添加新类来产生向下滚动的效果。它不起作用。我必须将类添加到另一个 div 吗?还是css错了?谢谢你的帮助。。
$(document).ready(function() {
const hiddenElement = $('.paragraph-small');
$('.show-more').click(function(){
hiddenElement.toggleClass('paragraph-large');
$(this).toggleClass('active');
});
});
.paragraph-small {
max-width: 500px;
text-align: justify !important;
margin: 1rem auto 3rem;
}
.hidden-text {
max-height: 0;
overflow: hidden;
}
.paragraph-large .hidden-text {
max-height: 200px;
overflow: visible;
transition: all linear 2.5s;;
}
.show-more {
width: 20px;
height: 20px;
background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<div class="toggle-box">
<div class="paragraph-small">
<div class="visible-text">
<span>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
</span>
</div>
<div class="hidden-text">
<span>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore e dolore magna aliquyam erat, sed diam voluptua.
</span>
</div>
<div class="show-more"></div>
</div>
</div>
【问题讨论】:
-
您无法设置动画或过渡到设置为
auto的长度值。请改用以下方法:不要定义height,而是定义max-height。将其设置为零以表示隐藏状态,并将其设置为像10000px这样高得离谱的值以表示可见状态。这将使它工作:) -
@connexo 好的,谢谢! :) max-height 有效 - 但仅适用于按钮而不适用于文本;/