jquery animate top不起作用

时间:2018-05-29 18:38:55

标签: jquery html css

我正在创建三个矩形,如下所示。我想这样做,当我点击矩形右侧的close按钮时,它会消失(我已经完成了)并且它下面的每个矩形向上移动以填充空白区域(我仍然有问题)它)。到目前为止,我已经尝试了这个

$(".note-content-right").click(function() {
  $(this).parent().removeClass("note-float-view");
  var thisDataIndex = $(this).parent().attr("data-index");
  $(".note-float").each(function() {
    if(($(this).hasClass("note-float-view")) && ($(this).attr("data-index") > thisDataIndex)) {
      $(this).animate({
        "top" : "-=54px"
      });
    };
  });
});

但它不起作用。

任何帮助表示赞赏! :)

$(document).ready(function() {
  $(".note-float").removeClass("note-under");
  loadNote();
});

function loadNote() {
  $(".note-float").each(function(index) {
    var el = $(this); 
    setTimeout(function () {
      el.addClass("note-float-view");
    }, index * 200);
  });
}

function unloadNote() {
  $(".note-float-view").each(function(index) {
    var el = $(this); 
    setTimeout(function () {
      el.removeClass("note-float-view");
    }, index * 200);
  });
}

$(".note-content-right").click(function() {
  $(this).parent().removeClass("note-float-view");
  var thisDataIndex = $(this).parent().attr("data-index");
  $(".note-float").each(function() {
    if(($(this).hasClass("note-float-view")) && ($(this).attr("data-index") > thisDataIndex)) {
      $(this).animate({
        "top" : "-=54px"
      });
    };
  });
});

$(".load-note").click(function() {
  $(".note-float").removeClass("note-under");
  $(".note-float").removeClass("note-float-view");
  setTimeout(function() {
    loadNote();
  }, 500);
});
.note-float-view {
  top: 24px !important;
  opacity: 1 !important;
  transition: top 1s, margin-bottom 1s, opacity 1s;
}

.note {
  padding: 14px 8px 14px 20px;
  font-size: 13px;
  margin: 0 auto 48px;
  display: table;
  width: 768px;
  position: relative;
  top: 24px;
  font-family: 'Open Sans', sans-serif;
}

.note-float {
  top: 0;
  opacity: 0;
  margin-bottom: 8px;
  transition: top 1s, margin-bottom 1s, opacity 1s;
}

.note-content-left {
  display: table-cell;
  width: 32px; 
}

.note-content-center {
  display: table-cell;
  width: calc(100% - 64px);
}

.note-content-right {
  display: table-cell;
  width: 12px;
  text-align: center;
  cursor: pointer;
}
.info {
  background-color: #CAF1FF;
  color: #0099ff;
  border: solid 1px #B1DEFF;
}

.success {
  background-color: #DAFDDC;
  color: #117250;
}

.warning {
  background-color: #F8F2D7;
  color: #DD6F1E;
}

.error {
  background-color: #FFE2E2; 
  color: #E9190C;
}

i.fa-info-circle {
  color: #0099ff;
}

i.fa-puzzle-piece,
.close-warning {
  color: #DD6F1E;
}

i.fa-check-circle,
.close-success {
  color: #117250;
}

i.fa-times-circle,
.close-error {
  color: #E9190C;
}

.note-content-right > .fa-times {
  font-size: 11px;
  margin-right: 12px; 
}

.dropdown-for-note {
  margin: 0 auto; 
  width: 366px;
}

.select-info {
  top: 8px;
  width: 366px;
}

.load-note {
  margin: 0 auto; 
  text-align: center;
}

.load-note:hover {
  color: #333;
  text-decoration: none;
}

.note-wrapper {
  display: block;
  width: 100%;
}

.caption {
  font-family: 'Open Sans', sans-serif;
}

.select-caption {
  margin-top: 24px; 
}

.load-btn-wrapper {
  width: 100%;
  display: block;
  text-align: center;
  margin-top: 48px;
}

.form-unit {
  font-family: 'Open Sans', sans-serif;
}

@media (max-width: 767px) and (min-width: 576px) {
  .note {
    width: calc(100% - 32px); 
    margin-left: 16px;
  }
  
  .select-info {
    width: 366px;
    margin: 0 auto;
  }
}

@media (max-width: 575px) {
  .note,
  .select-info {
    width: calc(100% - 32px); 
    margin-left: 16px;
  }
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.11/css/all.css" integrity="sha384-p2jx59pefphTFIpeqCcISO9MdVfIm4pNnsL08A6v5vaQc4owkQqxMV8kg4Yvhaw/" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700,700i" rel="stylesheet">

<div class="note-wrapper">

  
  <div class="note note-float success" data-index="1">
    <div class="note-content-left">
      <i class="fas fa-check-circle"></i>
    </div>
    <div class="note-content-center">
      You have successfully added <b>1 item(s)</b>
    </div>
    <div class="note-content-right">
      <i class="fas fa-times close-success"></i>
    </div>
  </div>

  <div class="note note-float warning" data-index="2">
    <div class="note-content-left">
      <i class="fas fa-puzzle-piece"></i>
    </div>
    <div class="note-content-center">
      You have successfully deleted <b>1 item(s)</b>, but failed to delete <b>1 item(s)</b> 
    </div>
    <div class="note-content-right">
      <i class="fas fa-times close-warning"></i>
    </div>
  </div>

  <div class="note note-float error" data-index="3">
    <div class="note-content-left">
      <i class="fas fa-times-circle"></i>
    </div>
    <div class="note-content-center">
      <b>BPJS TK:</b> Data is used on other module
    </div>
    <div class="note-content-right">
      <i class="fas fa-times close-error"></i>
    </div>
  </div>
  <div class="load-btn-wrapper">
    <a class="button white-btn footer-btn load-note">Reload </a>
  </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

1 个答案:

答案 0 :(得分:3)

top: 24px !important;上的.note-float-view会覆盖动画。删除!important就足够了。

我还更新了&#39;重置&#39;功能设置所有项目&#39;顶部回到24px而不是动画碰巧离开的地方。 (不过,最好在动画结束时对单个项目执行此操作。)

&#13;
&#13;
$(document).ready(function() {
  $(".note-float").removeClass("note-under");
  loadNote();
});

function loadNote() {
  $(".note-float").each(function(index) {
    var el = $(this);
    setTimeout(function() {
      el.addClass("note-float-view");
    }, index * 200);
  });
}

function unloadNote() {
  $(".note-float-view").each(function(index) {
    var el = $(this);
    setTimeout(function() {
      el.removeClass("note-float-view");
    }, index * 200);
  });
}

$(".note-content-right").click(function() {
  $(this).parent().removeClass("note-float-view");
  var thisDataIndex = $(this).parent().attr("data-index");
  $(".note-float").each(function() {
    if (($(this).hasClass("note-float-view")) && ($(this).attr("data-index") > thisDataIndex)) {
      $(this).animate({
        "top": "-=54px"
      });
    };
  });
});

$(".load-note").click(function() {
  $(".note-float")
    .removeClass("note-under")
    .removeClass("note-float-view")
    .css("top","24px");
  
  setTimeout(function() {
    loadNote();
  }, 500);
});
&#13;
.note-float-view {
  top: 24px ;
  opacity: 1 !important;
  transition: top 1s, margin-bottom 1s, opacity 1s;
}

.note {
  padding: 14px 8px 14px 20px;
  font-size: 13px;
  margin: 0 auto 48px;
  display: table;
  width: 768px;
  position: relative;
  top: 24px;
  font-family: 'Open Sans', sans-serif;
}

.note-float {
  top: 0;
  opacity: 0;
  margin-bottom: 8px;
  transition: top 1s, margin-bottom 1s, opacity 1s;
}

.note-content-left {
  display: table-cell;
  width: 32px;
}

.note-content-center {
  display: table-cell;
  width: calc(100% - 64px);
}

.note-content-right {
  display: table-cell;
  width: 12px;
  text-align: center;
  cursor: pointer;
}

.info {
  background-color: #CAF1FF;
  color: #0099ff;
  border: solid 1px #B1DEFF;
}

.success {
  background-color: #DAFDDC;
  color: #117250;
}

.warning {
  background-color: #F8F2D7;
  color: #DD6F1E;
}

.error {
  background-color: #FFE2E2;
  color: #E9190C;
}

i.fa-info-circle {
  color: #0099ff;
}

i.fa-puzzle-piece,
.close-warning {
  color: #DD6F1E;
}

i.fa-check-circle,
.close-success {
  color: #117250;
}

i.fa-times-circle,
.close-error {
  color: #E9190C;
}

.note-content-right>.fa-times {
  font-size: 11px;
  margin-right: 12px;
}

.dropdown-for-note {
  margin: 0 auto;
  width: 366px;
}

.select-info {
  top: 8px;
  width: 366px;
}

.load-note {
  margin: 0 auto;
  text-align: center;
}

.load-note:hover {
  color: #333;
  text-decoration: none;
}

.note-wrapper {
  display: block;
  width: 100%;
}

.caption {
  font-family: 'Open Sans', sans-serif;
}

.select-caption {
  margin-top: 24px;
}

.load-btn-wrapper {
  width: 100%;
  display: block;
  text-align: center;
  margin-top: 48px;
}

.form-unit {
  font-family: 'Open Sans', sans-serif;
}

@media (max-width: 767px) and (min-width: 576px) {
  .note {
    width: calc(100% - 32px);
    margin-left: 16px;
  }
  .select-info {
    width: 366px;
    margin: 0 auto;
  }
}

@media (max-width: 575px) {
  .note,
  .select-info {
    width: calc(100% - 32px);
    margin-left: 16px;
  }
}
&#13;
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.11/css/all.css" integrity="sha384-p2jx59pefphTFIpeqCcISO9MdVfIm4pNnsL08A6v5vaQc4owkQqxMV8kg4Yvhaw/" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700,700i" rel="stylesheet">

<div class="note-wrapper">


  <div class="note note-float success" data-index="1">
    <div class="note-content-left">
      <i class="fas fa-check-circle"></i>
    </div>
    <div class="note-content-center">
      You have successfully added <b>1 item(s)</b>
    </div>
    <div class="note-content-right">
      <i class="fas fa-times close-success"></i>
    </div>
  </div>

  <div class="note note-float warning" data-index="2">
    <div class="note-content-left">
      <i class="fas fa-puzzle-piece"></i>
    </div>
    <div class="note-content-center">
      You have successfully deleted <b>1 item(s)</b>, but failed to delete <b>1 item(s)</b>
    </div>
    <div class="note-content-right">
      <i class="fas fa-times close-warning"></i>
    </div>
  </div>

  <div class="note note-float error" data-index="3">
    <div class="note-content-left">
      <i class="fas fa-times-circle"></i>
    </div>
    <div class="note-content-center">
      <b>BPJS TK:</b> Data is used on other module
    </div>
    <div class="note-content-right">
      <i class="fas fa-times close-error"></i>
    </div>
  </div>
  <div class="load-btn-wrapper">
    <a class="button white-btn footer-btn load-note">Reload </a>
  </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
&#13;
&#13;
&#13;

作为旁注,如果这些项目中的文本换行到另一行,那么您为该位置设置动画的硬编码54px数量不会起作用。您可能希望捕获已删除元素的高度,然后从以下元素中按该量进行动画处理。

一般情况下,尽可能避免硬编码像素值,并尽可能避免依赖于绝对定位,因为它往往涉及很多像这样的繁琐重新定位。如果您依赖文档流程,浏览器将为您完成大部分工作。例如,对于这种情况,我通过将其最大高度设置为零(具有隐藏溢出-y)来移除元素;那么你根本不需要做任何手动定位。