仅对单击按钮的div进行动画处理

时间:2018-12-09 21:56:42

标签: javascript jquery

如果段落扩展到特定高度,我想隐藏div的文本,然后单击一个将高度更改为完全的按钮,但是通过单击该按钮,所有div都会受到影响,我想仅对该按钮的父div设置动画,而不仅仅是行在这种情况下,只有第一个div扩展了该高度才会受到按钮点击的影响

$(document).ready(function() {
var text = $('.description'),
     btn = $('.btn-overflow'),
       h = text[0].scrollHeight; 

if(h > 120) {
	btn.addClass('less');
	btn.css('display', 'block');
}

	$(".emp-bio .btn-overflow").click(function(e) {

  e.stopPropagation();
	e.preventDefault();
  if ($(this).hasClass('less')) {
      $(this).removeClass('less');
      $(this).addClass('more');
      $(this).text('Show less');

      text.animate({'height': h});
  } else {
      $(this).addClass('less');
      $(this).removeClass('more');
      $(this).text('Show more');
      text.animate({'height': '120px'});
  }  
});


});
.description {
  width: 250px;
  height: 120px;
  display: block;
  overflow: hidden;
  word-break: break-word;
  word-wrap: break-word;
}
<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
<div class="emp-bio">
<div class="description">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<a class="btn-overflow" href="#">Show more</a>
</div>
<div class="emp-bio">
<div class="text-overflow">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>

<a class="btn-overflow" href="#">Show more</a>
</div>
<script
  src="https://code.jquery.com/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous"></script>
<script type="text/javascript" src="script.js"></script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

希望我的代码段可以以某种方式为您提供帮助。

今天天气愉快,

$(document).ready(function() {
var text = $('.description'),
     btn = $('.btn-overflow'),
       h = text[0].scrollHeight; 

if(h > 120) {
	btn.addClass('less');
	btn.css('display', 'block');
}

	$(".emp-bio .btn-overflow").click(function(e) {

  e.stopPropagation();
	e.preventDefault();
  if ($(this).hasClass('less')) {
      $(this).removeClass('less');
      $(this).addClass('more');
      $(this).text('Show less');

      //use siblings to access .description in same level.
      $(this).siblings( ".description" ).animate({'height': h});
  } else {
      $(this).addClass('less');
      $(this).removeClass('more');
      $(this).text('Show more');

      //use siblings to access .description in same level.
      $(this).siblings( ".description" ).animate({'height': '120px'});
  }  
});


});
.description {
  width: 250px;
  height: 120px;
  display: block;
  overflow: hidden;
  word-break: break-word;
  word-wrap: break-word;
}
<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
<div class="emp-bio">
<div class="description">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<a class="btn-overflow" href="#">Show more</a>
</div>
<div class="emp-bio">
<div class="description">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>

<a class="btn-overflow" href="#">Show more</a>
</div>
<script
  src="https://code.jquery.com/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous"></script>
<script type="text/javascript" src="script.js"></script>
</body>
</html>