简体   繁体   English

如何做更多功能的切换可见性版本?

[英]How can I do a toggle visibility version of a read more function?

I am trying to do a toggle(hide and show) version of read more function. 我试图做一个阅读更多功能的切换(隐藏和显示)版本。 For now I can only show the rest of the text, not not hide it. 目前,我只能显示其余文本,不能隐藏它。

Any suggestions? 有什么建议么?

 $(document).ready(function(){ var toggleReadMore = function() { $('#read-more').click(function(e) { var prev = $(this).prev(); $(this).prev().css('height', $(this).prev()[0].scrollHeight + 'px'); $(this).hide(); $('#read-less').show(); }); }; toggleReadMore(); }()); 
 #p { height: 50px; overflow: hidden; } #read-less { display: none; } #read-more, #read-less { background: linear-gradient(to bottom, rgba(255,0,0,0), rgba(255,255,255,1)); color: blue; cursor: pointer; position: absolute; bottom: -20px; padding: 15px 0; text-align: center; width: 100%; } #wrapper { position: relative; width: 400px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='wrapper'> <p id='p'>Pinterest taxidermy et heirloom, ennui enim eu bicycle rights fugiat nesciunt commodo. High Life food truck jean shorts in. Blog asymmetrical cold-pressed photo booth. Neutra chia in, mustache Etsy nostrud plaid kogi. Magna polaroid stumptown aliqua put a bird on it gentrify, street art craft beer bicycle rights skateboard. DIY plaid gentrify, sustainable sapiente seitan mumblecore viral cardigan. Nisi pariatur laborum cornhole kitsch tempor fingerstache Bushwick. </p> <div id='read-more'> READ MORE </div> <div id='read-less'> READ LESS </div> </div> 

Update your function to this: 将您的功能更新为此:

$(document).ready(function() {
  $('#read-more').click(function(e) {
    $(this).prev().css('height', $(this).prev()[0].scrollHeight + 'px');
    $(this).hide();
    $(this).next('#read-less').show();
  });
  $('#read-less').click(function(e) {
    $(this).prev().prev().css('height', '50px');
    $(this).hide();
    $(this).prev('#read-more').show();
  });
});

You should use the latest version of jQuery, which is 3.2.1 and not 2.1.1 您应该使用最新版本的jQuery,它是3.2.1而不是2.1.1。

 $(document).ready(function() { $('#read-more').click(function(e) { $(this).prev().css('height', $(this).prev()[0].scrollHeight + 'px'); $(this).hide(); $(this).next('#read-less').show(); }); $('#read-less').click(function(e) { $(this).prev().prev().css('height', '50px'); $(this).hide(); $(this).prev('#read-more').show(); }); }); 
 #p { height: 50px; overflow: hidden; } #read-less { display: none; } #read-more, #read-less { background: linear-gradient(to bottom, rgba(255, 0, 0, 0), rgba(255, 255, 255, 1)); color: blue; cursor: pointer; position: absolute; bottom: -40px; padding: 15px 0; text-align: center; width: 100%; } #wrapper { position: relative; width: 400px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <div id='wrapper'> <p id='p'>Pinterest taxidermy et heirloom, ennui enim eu bicycle rights fugiat nesciunt commodo. High Life food truck jean shorts in. Blog asymmetrical cold-pressed photo booth. Neutra chia in, mustache Etsy nostrud plaid kogi. Magna polaroid stumptown aliqua put a bird on it gentrify, street art craft beer bicycle rights skateboard. DIY plaid gentrify, sustainable sapiente seitan mumblecore viral cardigan. Nisi pariatur laborum cornhole kitsch tempor fingerstache Bushwick. </p> <div id='read-more'> READ MORE </div> <div id='read-less'> SHOW LESS </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM