javascript - 删除内容时如何为可折叠的内部高度设置动画

标签 javascript jquery html css

相关代码如下:

// Javascript
// delete formule
$('body').on('click', '.del-formule', function(event) {
    $(this).parent().remove();

    // collapsible
    for (var i=0; i<2; i++)
    {
        document.getElementById('collapsible').classList.toggle("active");
        var content = document.getElementById("content");
        if (content.style.maxHeight){
            content.style.maxHeight = null;
        } else {
            content.style.maxHeight = content.scrollHeight + "px";
        }
    }
});
/* CSS */
.collapsible {
    width: 100%;
    background-color: #eee;
}

.content {
    padding: 0 18px;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.2s ease-out;
    background-color: #f1f1f1;
}

.collapsible:after {
    content: '\02795'; /* Unicode character for "plus" sign (+) */
    font-size: 13px;
    color: white;
    float: right;
    margin-left: 5px;
}

.collapsible.active:after {
    content: "\2796"; /* Unicode character for "minus" sign (-) */
}

我想要制作动画的是:picture to show what I want to animate

因此,当我按下 Supprimer 按钮(意味着删除)时,我想要为 div 删除动画

现在,它只是直接删除该div,没有动画

我想也许如果我关闭并重新打开可折叠的它会给它设置动画(这就是我的代码中的内容),但这不起作用

检查下面的代码片段以显示示例

感谢您的帮助! :) 祝你有美好的一天

// collapsible
var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
    coll[i].addEventListener("click", function() {
        this.classList.toggle("active");
        var content = this.nextElementSibling;
        if (content.style.maxHeight){
            content.style.maxHeight = null;
        } else {
            content.style.maxHeight = content.scrollHeight + "px";
        }
    });
}

// add formule
$('body').on('click', '.ajout-formule', function() {

    const $formule = $('<div>').addClass('div-formule');

    const $ligne_formule = $('<div>').addClass('ligne-formule');
    const $btn_formule = $('<th>');
    const $tableau_formule = $('<table class="table tabform" id="formule">');
    const $head_formule = $('<thead>');
    const $tr_formule = $('<tr>');



    $btn_formule.append('<button type="button" class="btn spr-champs"><i class="fas fa-trash"></i></button>');
    $btn_formule.append('<button class="btn nbr-champs" type="button"><i class="fas fa-sort-numeric-down"></i></button>');
    $btn_formule.append('<button class="btn list-champs" type="button"><i class="far fa-list-alt"></i></button>');
    $btn_formule.append('<input type="number" name="champs" id="champs" class="form-control pull-left" value="0"/>');

    $tr_formule.append('<th><input type="text" name="titre-formule" id="titre-formule" class="form-control pull-left" /></th>');
    $tr_formule.append('<th style="width:16px;padding-bottom: 16px;"><i class="fas fa-equals"></i></th>');
    $tr_formule.append($btn_formule);

    $head_formule.append($tr_formule);
    $tableau_formule.append($head_formule);

    $ligne_formule.append('<label class="panel-heading">Introduisez la formule : </label>');
    $ligne_formule.append($tableau_formule);

    $formule.append($ligne_formule);
    $formule.append('<span id="error_formule" class="text-danger"></span>');
    $formule.append('<span id="success_formule" class="text-danger"></span>');
    $formule.append('<button type="button" class="btn ajout-champs"><i class="fas fa-plus-circle"> Ajouter un champ</i></button>');
    $formule.append('<button type="button" class="btn save-formule"><i class="fas fa-check-square"> Enregistrer</i></button>');
    $formule.append('<button type="button" class="btn del-formule"><i class="fas fa-trash"> Supprimer</i></button>');
    $(this).parent().find('.add-formule').append($formule);

    // collapsible
    for (var i=0; i<2; i++)
    {
        document.getElementById('collapsible').classList.toggle("active");
        var content = document.getElementById("content");
        if (content.style.maxHeight){
            content.style.maxHeight = null;
        } else {
            content.style.maxHeight = content.scrollHeight + "px";
        }
    }
});

// delete formule
$('body').on('click', '.del-formule', function(event) {
    $(this).parent().remove();

    // METTRE A JOUR LES LISTES EN ENLEVANT LA FORMULE

    // collapsible
    for (var i=0; i<2; i++)
    {
        document.getElementById('collapsible').classList.toggle("active");
        var content = document.getElementById("content");
        if (content.style.maxHeight){
            content.style.maxHeight = null;
        } else {
            content.style.maxHeight = content.scrollHeight + "px";
        }
    }
});
/* Style the header with a grey background and some padding */
* {box-sizing: border-box;}

body {
    margin: 0;
    font-family: Arial, Helvetica, sans-serif;
}

.header, .collapsible {
    overflow: hidden;
    background-color: #f1f1f1;
    padding: 20px 10px;
}

.header a, .panel-body button.collapsible {
    float: left;
    color: black;
    text-align: center;
    padding: 12px;
    text-decoration: none;
    font-size: 18px;
    line-height: 25px;
    border-radius: 4px;
}

.header a.logo, .panel-body button.collapsible {
    font-size: 25px;
    font-weight: bold;
}

.header a:hover, .panel-body button.collapsible:hover {
    background-color: #ddd;
    color: black;
}

.header a.active {
    background-color: dodgerblue;
    color: white;
}

.header-right {
    float: right;
}

@media screen and (max-width: 500px) {
    .header a, .panel-body button.collapsible {
        float: none;
        display: block;
        text-align: left;
    }

    .header-right {
        float: none;
    }
}

.contenuaccueil {
    text-align: center;
    position : absolute;
    width : 100%;
    color : black;
    top:50%;
    left:50%;
    transform:translate(-50%,-50%);
}

.background
{
    margin-top : 10%;
    margin-bottom : 10%;
    position:relative;
    text-align: center;
}

.img
{
    background-repeat: repeat-x;
    width: 100%;
    height: auto;
    text-align: center;
}

footer {
    text-align : center;
    padding-top: 10px;
    padding-bottom: 0px;
    bottom:0;
    width:100%;
    color					: #A5A5A5;
    font-family				: "Lato", sans-serif;
    font-size				: 15px;
    font-weight				: 400;
    text-transform			: uppercase;
    text-decoration			: none;
    letter-spacing			: 3px;
}

.box
{
    width:800px;
    margin:0 auto;
}
.active_tab1
{
    background-color:#fff;
    color:#333;
    font-weight: 600;
}
.inactive_tab1
{
    background-color: #f5f5f5;
    color: #333;
    cursor: not-allowed;
}
.has-error
{
    border-color:#cc0000;
    background-color:#ffff99;
}

/* Styles go here */

.table-content {
    padding: 20px;
}

.form-control {
    width: 90px;
}

/* Style buttons */
.ajout-lig,.ajout-col,.ajout-graph,.ajout-formule,.save-formule,.ajout-champs, .del-formule {
    background-color: DodgerBlue; /* Blue background */
    border: none; /* Remove borders */
    color: white; /* White text */
    padding: 12px 16px; /* Some padding */
    font-size: 16px; /* Set a font size */
    cursor: pointer; /* Mouse pointer on hover */
    border-radius: 5px;
    margin-bottom: 1%;
}

/* Darker background on mouse-over */
.ajout-lig:hover,.ajout-col:hover,.ajout-graph,.ajout-formule,.save-formule,.ajout-champs, .del-formule {
    background-color: RoyalBlue;
}

.graph, .formule {
    display: block;
    margin : 0.75%;
    width: 50%;
    height: 34px;
    padding: 6px 12px;
    font-size: 14px;
    line-height: 1.42857143;
    color: #555;
    background-color: #fff;
    background-image: none;
    border: 1px solid #ddd;
    border-radius: 4px;
    -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
    box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
    -webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;
    -o-transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
    transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
}

.add, .add-formule {
    background-color: #fff;
    background-image: none;
    border: 1px solid transparent;
    border-radius: 4px;
}

.div-graph, .div-formule {
    padding: 15px;
    background-color: #fff;
    background-image: none;
    border: 1px solid #ddd;
    border-radius: 4px;
}

.grad, .axe-x, .axe-y {
    width: 19.7%;
    display : initial;
}

.grad {
    width: 15%;
    margin-bottom: 5%;
}

.collapsible {
    width: 100%;
    background-color: #eee;
}

.content {
    padding: 0 18px;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.2s ease-out;
    background-color: #f1f1f1;
}

.collapsible:after {
    content: '\02795'; /* Unicode character for "plus" sign (+) */
    font-size: 13px;
    color: white;
    float: right;
    margin-left: 5px;
}

.collapsible.active:after {
    content: "\2796"; /* Unicode character for "minus" sign (-) */
}

#previous_btn_personal_details, #btn_personal_details, .ajout-champs, .save-formule, .del-formule{
    margin-top : 2.5%;
}

.save-formule, .del-formule{
    margin-left:1%;
    float:right;
}

.operateur {
    font-family: FontAwesome, sans-serif;
    -moz-appearance: none;
    -webkit-appearance: none;
}

.operateur::-ms-expand{
    display:none;
}

.add-formule, .div-formule{
    background-color: #f1f1f1;
}

.div-formule{
    margin-bottom: 2%;
}

.center{
    display:table;
    margin:0 auto;
}
<html>
<head>
    <title>Innovatech</title>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="css/custom.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://kit.fontawesome.com/38b99a3f0e.js" crossorigin="anonymous"></script>
</head>
<body>
  <button type="button" class="collapsible" id="collapsible">Gérer les formules</button>
  <div class="form-group content" id="content">
    <br />
    <button type="button" class="ajout-formule"><i class="fas fa-plus-circle"> Ajouter une formule</i></button>
    <div class="panel-body add-formule">
    </div>
  </div>
</body>

最佳答案

我只是替换你的

$(this).parent().remove();

$(this).parent().animate({
    height: "0px",
}, 400, () => {
     $(this).parent().remove();
});

这是你想要的吗?

var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
  coll[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var content = this.nextElementSibling;
    if (content.style.maxHeight) {
      content.style.maxHeight = null;
    } else {
      content.style.maxHeight = content.scrollHeight + "px";
    }
  });
}

// add formule
$('body').on('click', '.ajout-formule', function() {

  const $formule = $('<div>').addClass('div-formule');

  const $ligne_formule = $('<div>').addClass('ligne-formule');
  const $btn_formule = $('<th>');
  const $tableau_formule = $('<table class="table tabform" id="formule">');
  const $head_formule = $('<thead>');
  const $tr_formule = $('<tr>');



  $btn_formule.append('<button type="button" class="btn spr-champs"><i class="fas fa-trash"></i></button>');
  $btn_formule.append('<button class="btn nbr-champs" type="button"><i class="fas fa-sort-numeric-down"></i></button>');
  $btn_formule.append('<button class="btn list-champs" type="button"><i class="far fa-list-alt"></i></button>');
  $btn_formule.append('<input type="number" name="champs" id="champs" class="form-control pull-left" value="0"/>');

  $tr_formule.append('<th><input type="text" name="titre-formule" id="titre-formule" class="form-control pull-left" /></th>');
  $tr_formule.append('<th style="width:16px;padding-bottom: 16px;"><i class="fas fa-equals"></i></th>');
  $tr_formule.append($btn_formule);

  $head_formule.append($tr_formule);
  $tableau_formule.append($head_formule);

  $ligne_formule.append('<label class="panel-heading">Introduisez la formule : </label>');
  $ligne_formule.append($tableau_formule);

  $formule.append($ligne_formule);
  $formule.append('<span id="error_formule" class="text-danger"></span>');
  $formule.append('<span id="success_formule" class="text-danger"></span>');
  $formule.append('<button type="button" class="btn ajout-champs"><i class="fas fa-plus-circle"> Ajouter un champ</i></button>');
  $formule.append('<button type="button" class="btn save-formule"><i class="fas fa-check-square"> Enregistrer</i></button>');
  $formule.append('<button type="button" class="btn del-formule"><i class="fas fa-trash"> Supprimer</i></button>');
  $(this).parent().find('.add-formule').append($formule);

  // collapsible
  for (var i = 0; i < 2; i++) {
    document.getElementById('collapsible').classList.toggle("active");
    var content = document.getElementById("content");
    if (content.style.maxHeight) {
      content.style.maxHeight = null;
    } else {
      content.style.maxHeight = content.scrollHeight + "px";
    }
  }
});

// delete formule
$('body').on('click', '.del-formule', function(event) {
  //$(this).parent().remove();

  $(this).parent().animate({
    height: "0px",
  }, 400, () => {
    $(this).parent().remove();
  });

  // METTRE A JOUR LES LISTES EN ENLEVANT LA FORMULE
  // collapsible
  for (var i = 0; i < 2; i++) {
    document.getElementById('collapsible').classList.toggle("active");
    var content = document.getElementById("content");
    if (content.style.maxHeight) {
      content.style.maxHeight = null;
    } else {
      content.style.maxHeight = content.scrollHeight + "px";
    }
  }
});
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.header,
.collapsible {
  overflow: hidden;
  background-color: #f1f1f1;
  padding: 20px 10px;
}

.header a,
.panel-body button.collapsible {
  float: left;
  color: black;
  text-align: center;
  padding: 12px;
  text-decoration: none;
  font-size: 18px;
  line-height: 25px;
  border-radius: 4px;
}

.header a.logo,
.panel-body button.collapsible {
  font-size: 25px;
  font-weight: bold;
}

.header a:hover,
.panel-body button.collapsible:hover {
  background-color: #ddd;
  color: black;
}

.header a.active {
  background-color: dodgerblue;
  color: white;
}

.header-right {
  float: right;
}

@media screen and (max-width: 500px) {
  .header a,
  .panel-body button.collapsible {
    float: none;
    display: block;
    text-align: left;
  }
  .header-right {
    float: none;
  }
}

.contenuaccueil {
  text-align: center;
  position: absolute;
  width: 100%;
  color: black;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.background {
  margin-top: 10%;
  margin-bottom: 10%;
  position: relative;
  text-align: center;
}

.img {
  background-repeat: repeat-x;
  width: 100%;
  height: auto;
  text-align: center;
}

footer {
  text-align: center;
  padding-top: 10px;
  padding-bottom: 0px;
  bottom: 0;
  width: 100%;
  color: #A5A5A5;
  font-family: "Lato", sans-serif;
  font-size: 15px;
  font-weight: 400;
  text-transform: uppercase;
  text-decoration: none;
  letter-spacing: 3px;
}

.box {
  width: 800px;
  margin: 0 auto;
}

.active_tab1 {
  background-color: #fff;
  color: #333;
  font-weight: 600;
}

.inactive_tab1 {
  background-color: #f5f5f5;
  color: #333;
  cursor: not-allowed;
}

.has-error {
  border-color: #cc0000;
  background-color: #ffff99;
}


/* Styles go here */

.table-content {
  padding: 20px;
}

.form-control {
  width: 90px;
}


/* Style buttons */

.ajout-lig,
.ajout-col,
.ajout-graph,
.ajout-formule,
.save-formule,
.ajout-champs,
.del-formule {
  background-color: DodgerBlue;
  /* Blue background */
  border: none;
  /* Remove borders */
  color: white;
  /* White text */
  padding: 12px 16px;
  /* Some padding */
  font-size: 16px;
  /* Set a font size */
  cursor: pointer;
  /* Mouse pointer on hover */
  border-radius: 5px;
  margin-bottom: 1%;
}


/* Darker background on mouse-over */

.ajout-lig:hover,
.ajout-col:hover,
.ajout-graph,
.ajout-formule,
.save-formule,
.ajout-champs,
.del-formule {
  background-color: RoyalBlue;
}

.graph,
.formule {
  display: block;
  margin: 0.75%;
  width: 50%;
  height: 34px;
  padding: 6px 12px;
  font-size: 14px;
  line-height: 1.42857143;
  color: #555;
  background-color: #fff;
  background-image: none;
  border: 1px solid #ddd;
  border-radius: 4px;
  -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
  -webkit-transition: border-color ease-in-out .15s, -webkit-box-shadow ease-in-out .15s;
  -o-transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
  transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
}

.add,
.add-formule {
  background-color: #fff;
  background-image: none;
  border: 1px solid transparent;
  border-radius: 4px;
}

.div-graph,
.div-formule {
  padding: 15px;
  background-color: #fff;
  background-image: none;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.grad,
.axe-x,
.axe-y {
  width: 19.7%;
  display: initial;
}

.grad {
  width: 15%;
  margin-bottom: 5%;
}

.collapsible {
  width: 100%;
  background-color: #eee;
}

.content {
  padding: 0 18px;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.2s ease-out;
  background-color: #f1f1f1;
}

.collapsible:after {
  content: '\02795';
  /* Unicode character for "plus" sign (+) */
  font-size: 13px;
  color: white;
  float: right;
  margin-left: 5px;
}

.collapsible.active:after {
  content: "\2796";
  /* Unicode character for "minus" sign (-) */
}

#previous_btn_personal_details,
#btn_personal_details,
.ajout-champs,
.save-formule,
.del-formule {
  margin-top: 2.5%;
}

.save-formule,
.del-formule {
  margin-left: 1%;
  float: right;
}

.operateur {
  font-family: FontAwesome, sans-serif;
  -moz-appearance: none;
  -webkit-appearance: none;
}

.operateur::-ms-expand {
  display: none;
}

.add-formule,
.div-formule {
  background-color: #f1f1f1;
}

.div-formule {
  margin-bottom: 2%;
}

.center {
  display: table;
  margin: 0 auto;
}
<head>
  <title>Innovatech</title>
  <meta charset="utf-8" />
  <link rel="stylesheet" href="css/custom.css" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
  <script src="https://kit.fontawesome.com/38b99a3f0e.js" crossorigin="anonymous"></script>
</head>

<body>
  <button type="button" class="collapsible" id="collapsible">Gérer les formules</button>
  <div class="form-group content" id="content">
    <br />
    <button type="button" class="ajout-formule"><i class="fas fa-plus-circle"> Ajouter une formule</i></button>
    <div class="panel-body add-formule">
    </div>
  </div>
</body>

关于javascript - 删除内容时如何为可折叠的内部高度设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60666952/

相关文章:

javascript - 如何在等待 1 秒后发送不和谐消息

javascript - HTML Canvas 文本输入和退格键

javascript - 将 for 循环重写为 filter() javascript

html - Timevis - 使用自定义 css 更改字体大小

javascript - 使用 JQuery 更改 svg 内的填充颜色

javascript - 如何在 JavaScript 中对不同的 ID 使用相同的函数?

javascript - 如何从 .php 检索数据并在 Cordova 混合应用程序中的 <select> 中显示?

javascript - 关闭 mouseoff 和 mouseup 的功能

javascript - HTML 元素在 PHP 数组中传递第一个值

javascript - 如何使 iframe 响应