【问题标题】:How to "fadeOut" & "remove" a div in jQuery?如何在 jQuery 中“淡出”和“删除”一个 div?
【发布时间】:2010-10-07 21:25:58
【问题描述】:

我正在尝试为 div 提供淡出效果并在单击图像时删除该 div(id = "notification")。

这就是我的做法:

<a onclick="$("#notification").fadeOut(300,function() { $("#notification").remove(); });" class="notificationClose "><img src="close.png"/></a>

这似乎不起作用。 我需要做什么来解决这个问题?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    .fadeOut('slow', this.remove);

    【讨论】:

      【解决方案2】:

      如果您像我一样从 google 搜索并希望删除带有酷动画的 html 元素,那么这可以帮助您:

      $(document).ready(function() {
          
          var $deleteButton = $('.deleteItem');
      
          $deleteButton.on('click', function(event) {
          
              event.preventDefault();
      
              var $button = $(this);
      
              if(confirm('Are you sure about this ?')) {
      
                  var $item = $button.closest('tr.item');
      
                  $item.addClass('removed-item')
      
                      .one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) {
      
                          $(this).remove();
                  });
              }
            
          });
          
      });
      /**
       * Credit to Sara Soueidan
       * @link https://github.com/SaraSoueidan/creative-list-effects/blob/master/css/styles-3.css
       */
      
      .removed-item {
          -webkit-animation: removed-item-animation .8s cubic-bezier(.65,-0.02,.72,.29);
          -o-animation: removed-item-animation .8s cubic-bezier(.65,-0.02,.72,.29);
          animation: removed-item-animation .8s cubic-bezier(.65,-0.02,.72,.29)
      }
      
      @keyframes removed-item-animation {
          0% {
              opacity: 1;
              -webkit-transform: translateX(0);
              -ms-transform: translateX(0);
              -o-transform: translateX(0);
              transform: translateX(0)
          }
      
          30% {
              opacity: 1;
              -webkit-transform: translateX(50px);
              -ms-transform: translateX(50px);
              -o-transform: translateX(50px);
              transform: translateX(50px)
          }
      
          80% {
              opacity: 1;
              -webkit-transform: translateX(-800px);
              -ms-transform: translateX(-800px);
              -o-transform: translateX(-800px);
              transform: translateX(-800px)
          }
      
          100% {
              opacity: 0;
              -webkit-transform: translateX(-800px);
              -ms-transform: translateX(-800px);
              -o-transform: translateX(-800px);
              transform: translateX(-800px)
          }
      }
      
      @-webkit-keyframes removed-item-animation {
          0% {
              opacity: 1;
              -webkit-transform: translateX(0);
              transform: translateX(0)
          }
      
          30% {
              opacity: 1;
              -webkit-transform: translateX(50px);
              transform: translateX(50px)
          }
      
          80% {
              opacity: 1;
              -webkit-transform: translateX(-800px);
              transform: translateX(-800px)
          }
      
          100% {
              opacity: 0;
              -webkit-transform: translateX(-800px);
              transform: translateX(-800px)
          }
      }
      
      @-o-keyframes removed-item-animation {
          0% {
              opacity: 1;
              -o-transform: translateX(0);
              transform: translateX(0)
          }
      
          30% {
              opacity: 1;
              -o-transform: translateX(50px);
              transform: translateX(50px)
          }
      
          80% {
              opacity: 1;
              -o-transform: translateX(-800px);
              transform: translateX(-800px)
          }
      
          100% {
              opacity: 0;
              -o-transform: translateX(-800px);
              transform: translateX(-800px)
          }
      }
      <!DOCTYPE html>
      <html>
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width">
        <title>JS Bin</title>
      </head>
      <body>
        
        <table class="table table-striped table-bordered table-hover">
          <thead>
            <tr>
              <th>id</th>
              <th>firstname</th>
              <th>lastname</th>
              <th>@twitter</th>
              <th>action</th>
            </tr>
          </thead>
          <tbody>
            
            <tr class="item">
              <td>1</td>
              <td>Nour-Eddine</td>
              <td>ECH-CHEBABY</td>
              <th>@__chebaby</th>
              <td><button class="btn btn-danger deleteItem">Delete</button></td>
            </tr>
            
            <tr class="item">
              <td>2</td>
              <td>John</td>
              <td>Doe</td>
              <th>@johndoe</th>
              <td><button class="btn btn-danger deleteItem">Delete</button></td>
            </tr>
            
            <tr class="item">
              <td>3</td>
              <td>Jane</td>
              <td>Doe</td>
              <th>@janedoe</th>
              <td><button class="btn btn-danger deleteItem">Delete</button></td>
            </tr>
          </tbody>
        </table>
        
      <script src="https://code.jquery.com/jquery.min.js"></script>
      <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
      
      
      </body>
      </html>

      【讨论】:

        【解决方案3】:

        使用

        .fadeOut(360).delay(400).remove();
        

        【讨论】:

          【解决方案4】:

          如果你在几个不同的地方使用它,你应该把它变成一个插件。

          jQuery.fn.fadeOutAndRemove = function(speed){
              $(this).fadeOut(speed,function(){
                  $(this).remove();
              })
          }
          

          然后:

          // Somewhere in the program code.
          $('div').fadeOutAndRemove('fast');
          

          【讨论】:

            【解决方案5】:

            试试这个:

            <a onclick='$("#notification").fadeOut(300, function() { $(this).remove(); });' class="notificationClose "><img src="close.png"/></a>
            

            我认为您在 onclick 周围的双引号使其无法正常工作。 :)

            编辑:正如下面所指出的,内联 javascript 是邪恶的,你应该把它从 onclick 中取出,并将它移到 jQuery 的 click() 事件处理程序中。这就是现在酷孩子们的做法。

            【讨论】:

              【解决方案6】:

              你真的应该尝试在单独的文件中使用 jQuery,而不是内联。这是您需要的:

              <a class="notificationClose "><img src="close.png"/></a>
              

              然后在页面底部的 &lt;script&gt; 标记中或在外部 JavaScript 文件中。

              $(".notificationClose").click(function() {
                  $("#notification").fadeOut("normal", function() {
                      $(this).remove();
                  });
              });
              

              【讨论】:

                【解决方案7】:

                你试过了吗?

                $("#notification").fadeOut(300, function(){ 
                    $(this).remove();
                });
                

                也就是说,使用当前的 this 上下文来定位内部函数中的元素而不是 id。我一直使用这种模式 - 它应该可以工作。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-05-04
                  相关资源
                  最近更新 更多