There are links when you click on them, the block opens and after the opening there is an indent if there is little text.

$('a.plus').click(function(){ var noteHeight = $('.note').height(); var next = $(this).next(); next.toggleClass("open"); if(next.hasClass("open")) { next.css({'height': noteHeight}); } else { next.css({'height': 0}); } return false; }); 

https://jsfiddle.net/Zkolya_linkoln/gs118p1d/2/

I suppose it’s necessary here instead

 var noteHeight = $('.note').height(); 

something like that

  var noteHeight = $(this).find('.note').height(); 

But not working

  • What kind of indent? Do you want the block to be given a value if the text is not enough? What if you use CSS ( min-height: 400px; )? - Telion
  • Indent, for example, click link 2, I want height auto be - zkolya

3 answers 3

Replace:

 var noteHeight = $('.note').height(); 

on

 var noteHeight = $(this).next().children().height(); 

Example:

 $('a.plus').click(function() { var noteHeight = $(this).next().children().height(); var next = $(this).next(); next.toggleClass("open"); if (next.hasClass("open")) { next.css({ 'height': noteHeight }); } else { next.css({ 'height': 0 }); } return false; }); 
 .more { overflow: hidden; height: 10px; transition: .3s; } .open { height: auto; transition: .3s; } .note { background: #22dd66; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a class="plus">ссылка</a> <div class="more"> <div class="note"> <p>Π€Π΅Π΄Π΅Ρ€Π°Π»ΡŒΠ½Ρ‹ΠΉ Π·Π°ΠΊΠΎΠ½ ΠΎΡ‚ 02.05.2015 N 122-Π€Π—</p> <p>"О внСсСнии ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠΉ Π² Π’Ρ€ΡƒΠ΄ΠΎΠ²ΠΎΠΉ кодСкс Российской Π€Π΅Π΄Π΅Ρ€Π°Ρ†ΠΈΠΈ ΠΈ ΡΡ‚Π°Ρ‚ΡŒΠΈ 11 ΠΈ 73 Π€Π΅Π΄Π΅Ρ€Π°Π»ΡŒΠ½ΠΎΠ³ΠΎ Π·Π°ΠΊΠΎΠ½Π° "Об ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½ΠΈΠΈ Π² Российской Π€Π΅Π΄Π΅Ρ€Π°Ρ†ΠΈΠΈ"</p> <p>Начало дСйствия Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚Π° - 01.07.2016</p> <p>ΠŸΡ€ΠΈΠΊΠ°Π·ΠΎΠΌ ΠœΠΈΠ½Ρ‚Ρ€ΡƒΠ΄Π° России ΠΎΡ‚ 19 октября 2015 Π³. N 728Π½ ΡƒΡ‚Π²Π΅Ρ€ΠΆΠ΄Π΅Π½ ΠΏΡ€ΠΎΡ„Π΅ΡΡΠΈΠΎΠ½Π°Π»ΡŒΠ½Ρ‹ΠΉ стандарт "Аудитор". Π­Ρ‚ΠΎΡ‚ Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ прСдставляСт собой характСристику ΠΊΠ²Π°Π»ΠΈΡ„ΠΈΠΊΠ°Ρ†ΠΈΠΈ, Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎΠΉ Ρ€Π°Π±ΠΎΡ‚Π½ΠΈΠΊΡƒ для осущСствлСния аудиторской Π΄Π΅ΡΡ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΠΈ ΠΈ оказания ΠΏΡ€ΠΎΡ‡ΠΈΡ… связанных с Π½Π΅ΠΉ услуг.</p> <p>ΠŸΡ€ΠΈΠΊΠ°Π·ΠΎΠΌ ΠœΠΈΠ½Ρ‚Ρ€ΡƒΠ΄Π° России ΠΎΡ‚ 19 октября 2015 Π³. N 728Π½ ΡƒΡ‚Π²Π΅Ρ€ΠΆΠ΄Π΅Π½ ΠΏΡ€ΠΎΡ„Π΅ΡΡΠΈΠΎΠ½Π°Π»ΡŒΠ½Ρ‹ΠΉ стандарт "Аудитор". Π­Ρ‚ΠΎΡ‚ Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ прСдставляСт собой характСристику ΠΊΠ²Π°Π»ΠΈΡ„ΠΈΠΊΠ°Ρ†ΠΈΠΈ, Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎΠΉ Ρ€Π°Π±ΠΎΡ‚Π½ΠΈΠΊΡƒ для осущСствлСния аудиторской Π΄Π΅ΡΡ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΠΈ ΠΈ оказания ΠΏΡ€ΠΎΡ‡ΠΈΡ… связанных с Π½Π΅ΠΉ услуг.</p> </div> </div> <a class="plus">ссылка</a> <div class="more"> <div class="note"> <p>Π€Π΅Π΄Π΅Ρ€Π°Π»ΡŒΠ½Ρ‹ΠΉ Π·Π°ΠΊΠΎΠ½ ΠΎΡ‚ 02.05.2015 N 122-Π€Π—</p> <p>ΠŸΡ€ΠΈΠΊΠ°Π·ΠΎΠΌ ΠœΠΈΠ½Ρ‚Ρ€ΡƒΠ΄Π° России ΠΎΡ‚ 19 октября 2015 Π³. N 728Π½ ΡƒΡ‚Π²Π΅Ρ€ΠΆΠ΄Π΅Π½ ΠΏΡ€ΠΎΡ„Π΅ΡΡΠΈΠΎΠ½Π°Π»ΡŒΠ½Ρ‹ΠΉ стандарт "Аудитор". Π­Ρ‚ΠΎΡ‚ Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ прСдставляСт собой характСристику ΠΊΠ²Π°Π»ΠΈΡ„ΠΈΠΊΠ°Ρ†ΠΈΠΈ, Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎΠΉ Ρ€Π°Π±ΠΎΡ‚Π½ΠΈΠΊΡƒ для осущСствлСния аудиторской Π΄Π΅ΡΡ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΠΈ ΠΈ оказания ΠΏΡ€ΠΎΡ‡ΠΈΡ… связанных с Π½Π΅ΠΉ услуг.</p> </div> </div> <a class="plus">ссылка</a> <div class="more"> <div class="note"> <p>Π€Π΅Π΄Π΅Ρ€Π°Π»ΡŒΠ½Ρ‹ΠΉ Π·Π°ΠΊΠΎΠ½ ΠΎΡ‚ 02.05.2015 N 122-Π€Π—</p> <p>ΠŸΡ€ΠΈΠΊΠ°Π·ΠΎΠΌ ΠœΠΈΠ½Ρ‚Ρ€ΡƒΠ΄Π° России ΠΎΡ‚ 19 октября 2015 Π³. N 728Π½ ΡƒΡ‚Π²Π΅Ρ€ΠΆΠ΄Π΅Π½ ΠΏΡ€ΠΎΡ„Π΅ΡΡΠΈΠΎΠ½Π°Π»ΡŒΠ½Ρ‹ΠΉ стандарт "Аудитор". Π­Ρ‚ΠΎΡ‚ Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ прСдставляСт собой характСристику ΠΊΠ²Π°Π»ΠΈΡ„ΠΈΠΊΠ°Ρ†ΠΈΠΈ, Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎΠΉ Ρ€Π°Π±ΠΎΡ‚Π½ΠΈΠΊΡƒ для осущСствлСния аудиторской Π΄Π΅ΡΡ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΠΈ ΠΈ оказания ΠΏΡ€ΠΎΡ‡ΠΈΡ… связанных с Π½Π΅ΠΉ услуг.</p> </div> </div> 

     $('a.plus').click(function(){ var noteHeight = $(this).find('.note').height(); var next = $(this).next(); next.toggleClass("open"); if(next.hasClass("open")) { next.css('height', noteHeight+'px'); } else { next.css({'height': 0}); } return false; }); 

    var noteHeight = $ (this) .find ('. note'). height (); - find out the height of a particular block

    next.css ('height', noteHeight + 'px'); - height assignment

    • In this case, the smoothness of the transition disappears and it works 1 time, if you close it, it will not open again - zkolya

    The script does not make sense since there is a simpler solution: Leaving the tab open script:

     $('a.plus').click(function(){ var next = $(this).next(); next.toggleClass("open"); }); 

    Change CSS:

     .more {overflow:hidden;transition:1s;height:auto;max-height:10px;} .open {max-height: 999px;transition:1s;} .note{background:#22dd66;height: auto;} 

    Here is an example on JSFIDDLE: https://jsfiddle.net/gs118p1d/15/

    Edit: added min-height: 0 for transition animation.

    • transition does not work - zkolya
    • @zkolya fixed - Telion
    • There is an indent anyway, and the min-height:200px; variant is not fixed min-height:200px; There can be more text. I don't know yet what to do - zkolya
    • @zkolya I have specified min-height, this means the minimum height. If there is more text, then everything will also work. Check it yourself, I dropped the working example. By the way STOP. You wanted to remove the indent, and I added it specifically thinking that you need it. I'll fix it now :) - Telion
    • Wait, I'm confused about something, this is how super work is .open {height:auto;transition:.3s;} but then everything is not smooth - zkolya