动态添加内容时,从底部隐藏html元素

时间:2018-03-09 11:02:26

标签: javascript jquery html css

我正在开发评论Feed。作为默认,我隐藏了所有评论4。 如果有更多4条评论,并且我点击了显示更多锚点,则会显示最多4条评论。我的问题是,如果我现在动态添加更多注释,CSS会在Feed中间隐藏注释。当我添加评论时,具有第四个位置的评论被隐藏。但是,当您单击锚点时显示的所有其他注释仍然显示。我想要的是:在页面加载时只显示四条评论。如果在点击锚点之前添加了注释,则隐藏第四个注释并在顶部显示新注释(现在正在工作)。如果单击锚点(无论多少次),则隐藏当前显示的最后一条评论并在顶部显示新评论。我在下面有一个工作片段。我希望我能让自己明白。



var _divNum = jQuery('div.commentsWrapper > div').length;
if (_divNum > 4) {
  $('.showMoreAnchorWrapper').fadeIn(300);
}

jQuery(document).on('click', '#showMoreAnchor', function() {

  jQuery('div.commentsWrapper > div:hidden').slice(0, 4).slideDown(300);
  if (jQuery('div.commentsWrapper > div').length === jQuery('div.commentsWrapper > div:visible').length) {

    jQuery('#showMoreAnchor').fadeOut(300);
  }
});

jQuery(document).on('click', "#postButton", function(e) {
  var textAreaContent = jQuery("#textAreaInput").val().trim();
  var html = "<div class='commentAreaWrapper'><div class='titleStyle'><span class='imgWrapper'><img class='' style='min-width:48px; min-height:48px; clip:rect(0px, 48px, 48px, 0px); max-width:48px' src='' alt='Admin'></span>Admin</div><div class='commentArea'>" + textAreaContent + "</div></div>";

  jQuery(html).prependTo(".commentsWrapper").hide().slideDown();;

  var _divNum = jQuery('div.commentsWrapper > div').length;
  if (_divNum > 4) {
    jQuery('.showMoreAnchorWrapper').fadeIn(300);
  }

});
&#13;
.globalWrapper {
  height: 50%;
  width: 50%;
  margin-bottom: 15px;
  margin-left: 10px;
}

.commentsWrapper {
  padding-bottom: 10px;
  padding-top: 10px;
  border-radius: 5px;
  background-color: #EBEDEF;
}

.commentArea {
  padding: 5px 5px 5px 5px;
  background-color: #D5D8DC;
  margin-top: 0px;
  border-bottom-left-radius: 5px;
  border-bottom-right-radius: 5px;
  font-family: "Segoe UI Semilight", "Segoe UI", "Segoe", Tahoma, Helvetica, Arial, sans-serif;
  font-size: 1.15em;
  word-wrap: break-word
}

.titleStyle {
  padding-left: 8px;
  background-color: #FBF1E7;
  margin-top: 10px;
  padding-bottom: 2px;
  padding-top: 6px;
  border-bottom: 1px solid grey;
}

div.commentsWrapper>div:nth-child(1)>div.titleStyle {
  margin-top: 0px;
}

.inputWrapper {
  //background-color:#EBEDEF;              
}

.imgWrapper {
  margin-right: 7px;
}

#textAreaInput {
  width: 100%;
  max-width: 97%;
  height: 70px;
  ;
  resize: none;
  text-align: left;
  padding-left: 0.4;
  padding-top: 0.4;
  padding-bottom: 0.4em;
  padding-right: 0.6em;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
  margin-bottom: 5px;
  word-wrap: break-word
}

.buttonWrapper {
  margin - left: 10px;
  height: 35px;
}

#postButton {
  float: right;
}

.commentAreaWrapper {
  margin-left: auto;
  margin-right: auto;
  width: 94%;
}

.commentAreaWrapper:hover {
  /*box-shadow:10px 10px 10px;*/
  //border-bottom: 2px solid grey;
  //border-right: 2px solid grey;
  //border-radius: 5px;
}

div.commentsWrapper>div:nth-child(n+5) {
  display: none;
}

.showMoreAnchorWrapper {
  text-align: center;
  margin-top: 10px;
  display: none;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="globalWrapper">
  <div class="inputWrapper">
    <textarea id="textAreaInput">test test</textarea>

    <div class="buttonWrapper">
      <button id="postButton" type="button">Inlägg</button>
    </div>
  </div>

  <div class="commentsWrapper">
    <div class="commentAreaWrapper">
      <div class="titleStyle"><span class="imgWrapper"><img class="" style="min-width:48px; min-height:48px; clip:rect(0px, 48px, 48px, 0px); max-width:48px" src="" alt="Admin"></span>Admin</div>
      <div class="commentArea">1</div>
    </div>
    <div class="commentAreaWrapper">
      <div class="titleStyle"><span class="imgWrapper"><img class="" style="min-width:48px; min-height:48px; clip:rect(0px, 48px, 48px, 0px); max-width:48px" src="" alt="Admin"></span> Admin</div>
      <div class="commentArea">2</div>
    </div>
    <div class="commentAreaWrapper">
      <div class="titleStyle"><span class="imgWrapper"><img class="" style="min-width:48px; min-height:48px; clip:rect(0px, 48px, 48px, 0px); max-width:48px" src="" alt="Admin"></span>Admin</div>
      <div class="commentArea">3</div>
    </div>
    <div class="commentAreaWrapper">
      <div class="titleStyle"><span class="imgWrapper"><img class="" style="min-width:48px; min-height:48px; clip:rect(0px, 48px, 48px, 0px); max-width:48px" src="" alt="Admin"></span>Admin</div>
      <div class="commentArea">4</div>
    </div>
    <div class="commentAreaWrapper">
      <div class="titleStyle"><span class="imgWrapper"><img class="" style="min-width:48px; min-height:48px; clip:rect(0px, 48px, 48px, 0px); max-width:48px" src="" alt="Admin"></span>Unisight Admin</div>
      <div class="commentArea">5</div>
    </div>
    <div class="commentAreaWrapper">
      <div class="titleStyle"><span class="imgWrapper"><img class="" style="min-width:48px; min-height:48px; clip:rect(0px, 48px, 48px, 0px); max-width:48px" src="" alt="Admin"></span>Admin</div>
      <div class="commentArea">6</div>
    </div>
    <div class="commentAreaWrapper">
      <div class="titleStyle"><span class="imgWrapper"><img class="" style="min-width:48px; min-height:48px; clip:rect(0px, 48px, 48px, 0px); max-width:48px" src="" alt="Admin"></span>Admin</div>
      <div class="commentArea">7</div>
    </div>
  </div>
  <div class="showMoreAnchorWrapper">
    <a href="#" id="showMoreAnchor">Visa fler</a>
  </div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

用此替换JS代码并测试我希望这将根据您的要求工作:)

var _divNum = jQuery('div.commentsWrapper > div').length;
if (_divNum > 4) {
    $('.showMoreAnchorWrapper').fadeIn(300);
}

jQuery(document).on('click', '#showMoreAnchor', function() {
    $($("#showMoreAnchor").parents(".globalWrapper")[0]).children(".commentsWrapper").attr("track", true);
    jQuery('div.commentsWrapper > div:hidden').slice(0, 4).slideDown(300);
    if (jQuery('div.commentsWrapper > div').length === jQuery('div.commentsWrapper > div:visible').length) {

        jQuery('#showMoreAnchor').fadeOut(300);
    }
});

jQuery(document).on('click', "#postButton", function(e) {
    var textAreaContent = jQuery("#textAreaInput").val().trim();
    var html = "<div class='commentAreaWrapper'><div class='titleStyle'><span class='imgWrapper'><img class='' style='min-width:48px; min-height:48px; clip:rect(0px, 48px, 48px, 0px); max-width:48px' src='' alt='Admin'></span>Admin</div><div class='commentArea'>" + textAreaContent + "</div></div>";

    jQuery(html).prependTo(".commentsWrapper").hide().slideDown();;
    if ($($("#showMoreAnchor").parents(".globalWrapper")[0]).children(".commentsWrapper").attr("track")) {
        var visibleBlocks = jQuery('div.commentsWrapper > div:visible');
        var lastBlock = visibleBlocks[visibleBlocks.length - 1];
        $(lastBlock).css("display", "none")
        jQuery('#showMoreAnchor').fadeIn(300);
    };

    var _divNum = jQuery('div.commentsWrapper > div').length;
    if (_divNum > 4) {
        jQuery('.showMoreAnchorWrapper').fadeIn(300);
    }

});

答案 1 :(得分:0)

您可以将此代码添加到#postButton点击事件:

  // if more button was clicked      
  if (jQuery('div.commentsWrapper > div:visible').length > 4) {
    // hide the last visible element
    jQuery('div.commentsWrapper > div:visible:last').fadeOut(300);
  }

这是一个有效的jsfiddle