简体   繁体   English

在动态设置div高度时切换文本

[英]toggle text while dynamically animating div height

I have a fixed div height with some text that overflows. 我有一个固定的div高度,其中有些文本会溢出。 I've set the overflow to hidden and want to expand the div height to display the rest of text inside if a link is pressed; 我已将溢出设置为hidden并且如果单击链接,则希望扩展div height以在其中显示其余文本; then return the div to its initial height when the link is pressed again. 然后在再次按下链接时将div返回到其初始高度。 I came across the following question which is what I want, however their text is broken up into 2 p tags whereas I only have one. 我遇到了以下问题 ,这是我想要的,但是他们的文本分为2个p标签,而我只有一个。 I tried this but I get the following error: 我尝试了此操作,但出现以下错误:

Unable to get property 'scrollHeight' of undefined or null reference

Here is my code 这是我的代码

Any help would be appreciated. 任何帮助,将不胜感激。

the problem was getting the element.. 问题出在元素上。

check the result 检查结果

$(function() {

  if ($('#dialog-box').is(':visible')) {
    showMoreTextDialog();
  }

  function showMoreTextDialog() {

    var dialog_txt = $('.dialog_middle p').html();

    if (dialog_txt.length > 350) {

      var append_dialog = dialog_txt.substr(0, 350);

      $('.dialog_middle p')
        .html(append_dialog)
        .append('<span class="showMore">    (.... Show More )</span>');

      $('.dialog_middle')
        .data("original-height", $(".dialog_middle")[0].scrollHeight);

      $(document).on({
        'mouseover': function() {
          $(this).css('cursor', 'pointer');
        },
        'click': function() {

          $('.dialog_middle p')
            .html(dialog_txt)
            .append('<span class="showLess">    (.... Show Less )</span>');

          $('.dialog_middle')
            .animate({
              height: $(".dialog_middle")[0].scrollHeight
            }, 2000);

        }
      }, '.dialog_middle .showMore');

      $(document).on({
        'mouseover': function() {
          $(this).css('cursor', 'pointer');
        },
        'click': function() {

          $('.dialog_middle')
            .animate({
              height: $(".dialog_middle").data("original-height")
            }, 2000, function() {

              $('.dialog_middle p')
                .html(append_dialog)
                .append('<span class="showMore">    (.... Show More )</span>');

            });

        }
      }, '.dialog_middle .showLess');

    }
  }

});

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

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