【问题标题】:jQuery hide() animation slide rightjQuery hide() 动画向右滑动
【发布时间】:2016-10-25 22:28:18
【问题描述】:

使用 jQuery 的 hide() 函数和可选的持续时间参数,我可以在我的网站上获得一些警报框,优雅地滑出屏幕并消失。

隐藏动画的默认方向似乎是向左滑动,尽管hide() definition page 中没有定义此行为。

我需要选择让框也向右滑动或向上滑动。

同样重要的是,如果未定义此行为,我担心不同的浏览器可能会以不同的方式呈现动画。所以我也希望保持一致性。

左滑行为在哪里定义?切换到右滑或上滑最简单的方法是什么?

$(document).ready(function() {
  $("#close-alert-box-urgent").click(function() {
    $("#alert-box-urgent").hide(1000);
  });
  $("#close-alert-box-news").click(function() {
    $("#alert-box-news").hide('fast');
  });
});
.alert-box {
  width: 50vw;
  position: relative;
  margin: 20px auto;
  border: 1px solid black;
}
.alert-box-close {
  position: absolute;
  top: -12px;
  right: -12px;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>

<article class="alert-box" id="alert-box-urgent">
  <h1>Tuition Alert</h1>
  <p>text text text text text text text text text text text text text text</p>
  <a class="alert-box-close" id="close-alert-box-urgent">
    <img src="http://i.imgur.com/czf8yas.png" height="25" width="25" alt="">
  </a>
</article>

<article class="alert-box" id="alert-box-news">
  <h1>Latest News</h1>
  <p>text text text text text text text text text text text text text text</p>
  <a class="alert-box-close" id="close-alert-box-news">
    <img src="http://i.imgur.com/czf8yas.png" height="25" width="25" alt="">
  </a>
</article>

https://jsfiddle.net/n0zpev0v/

【问题讨论】:

  • 你会反对使用jQueryUI吗?
  • @HunterTurner,我正在寻找轻量级解决方案。如果这符合要求,是的,我愿意接受。谢谢。
  • 您可以使用类似于 Animate CSS 对其 jquery fn.extend 所做的事情。让我做一个演示,看看它是否是一个好的解决方案。您可以将@keyframes 用于您想要的任何动画。

标签: javascript jquery html css


【解决方案1】:

如果你包含 jQueryUI,你可以使用他们的 hide 方法,这将允许你指定一个方向。

jQueryUI

hide("slide", {direction: "right"}, 1000);

JSFiddle

【讨论】:

    【解决方案2】:

    可以使用jQuery.animate()方法吗,如下。

    $(document).ready(function() {
      $("#close-alert-box-urgent").click(function() {
        $("#alert-box-urgent").animate({
        'margin-left' : "50%",
        'opacity' : '0',
        },500);
      });
      $("#close-alert-box-news").click(function() {
        $("#alert-box-news").animate({
        'margin-top' : "-50%",
        'opacity' : '0'
        },500);;
      });
    });
    body{
      overflow-x:hidden;
    }
    .alert-box {
      width: 50vw;
      position: relative;
      margin: 20px auto;
      border: 1px solid black;
      display:block;
    }
    
    .alert-box-close {
      position: absolute;
      top: -12px;
      right: -12px;
      cursor: pointer;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <article class="alert-box" id="alert-box-urgent">
      <h1>Tuition Alert</h1>
      <p>text text text text text text text text text text text text text text text text text text</p>
      <a class="alert-box-close" id="close-alert-box-urgent"><img src="http://i.imgur.com/czf8yas.png" height="25" width="25" alt=""></a>
    </article>
    
    <article class="alert-box" id="alert-box-news">
      <h1>Latest News</h1>
      <p>text text text text text text text text text text text text text text text text text text</p>
      <a class="alert-box-close" id="close-alert-box-news"><img src="http://i.imgur.com/czf8yas.png" height="25" width="25" alt=""></a>
    </article>

    【讨论】:

      【解决方案3】:

      关于这个答案,我只想指出并澄清这一行:

      看来隐藏动画的默认方向是向左滑动

      该动画没有“方向”,它只是根据元素的实际盒子行为为元素本身设置动画,并适应元素的增长方式。动画维度的属性是:

      Width // Height
      

      因为正常元素从左上角开始增长,所以你会看到它向左移动。


      如果您更改框的行为,例如使用float,您将看到另一种行为。

      Jsfiddle Demo

      【讨论】:

      • 这是我与body { direction: rtl; }的原始小提琴:jsfiddle.net/n0zpev0v/3
      • 这真是太好了@Michael_B 没有考虑过direction 的可能性
      【解决方案4】:

      您可以使用与animate.css类似的方法。

      您可以像这样创建fn.extend

      $.fn.extend({
        animateAlert: function(animationName, speed, isForwards) {
          var animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
          this.addClass('animated ' + animationName).css("animation-duration", speed + "ms");
          if (!isForwards) {
            this.one(animationEnd, function() {
              $(this).removeClass('animated ' + animationName);
            });
          }
        }
      });
      

      您可以随意自定义它,在此示例中,我们添加一个 animationName,这将是一个 @keyframe 和您的 CSS 文件中的一个类。

      例如:

      .slideOutUp {
        -webkit-animation-name: slideOutUp;
        animation-name: slideOutUp
      }
      @keyframes slideOutUp {
        0% {
          -webkit-transform: translate3d(0, 0, 0);
          transform: translate3d(0, 0, 0)
        }
        100% {
          visibility: hidden;
          -webkit-transform: translate3d(0, -100%, 0);
          transform: translate3d(0, -100%, 0)
        }
      }
      

      然后speed 以毫秒为单位,isForwards 为布尔值,模拟forwards animation-fill-mode 值。


      代码片段:

      $.fn.extend({
        animateAlert: function(animationName, speed, isForwards) {
          var animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
          this.addClass('animated ' + animationName).css("animation-duration", speed + "ms");
          if (!isForwards) {
            this.one(animationEnd, function() {
              $(this).removeClass('animated ' + animationName);
            });
          }
        }
      });
      
      $(document).ready(function() {
        $("#close-alert-box-urgent").on("click", function() {
          $("#alert-box-urgent").animateAlert('slideOutUp', 6000);
        });
        $("#close-alert-box-news").on("click", function() {
          $("#alert-box-news").animateAlert('slideOutUp', 500, true);
        });
      });
      .alert-box {
        width: 50vw;
        position: relative;
        margin: 20px auto;
        border: 1px solid black;
      }
      .alert-box-close {
        position: absolute;
        top: -12px;
        right: -12px;
        cursor: pointer;
      }
      .animated {
        -webkit-animation-duration: 1s;
        animation-duration: 1s;
        -webkit-animation-fill-mode: both;
        animation-fill-mode: both;
      }
      .slideOutUp {
        -webkit-animation-name: slideOutUp;
        animation-name: slideOutUp
      }
      @keyframes slideOutUp {
        0% {
          -webkit-transform: translate3d(0, 0, 0);
          transform: translate3d(0, 0, 0)
        }
        100% {
          visibility: hidden;
          -webkit-transform: translate3d(0, -100%, 0);
          transform: translate3d(0, -100%, 0)
        }
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <article class="alert-box" id="alert-box-urgent">
        <h1>Tuition Alert</h1>
        <p>text text text text text text text text text text text text text text text text text text</p>
        <a class="alert-box-close" id="close-alert-box-urgent">
          <img src="http://i.imgur.com/czf8yas.png" height="25" width="25" alt="">
        </a>
      </article>
      
      <article class="alert-box" id="alert-box-news">
        <h1>Latest News</h1>
        <p>text text text text text text text text text text text text text text text text text text</p>
        <a class="alert-box-close" id="close-alert-box-news">
          <img src="http://i.imgur.com/czf8yas.png" height="25" width="25" alt="">
        </a>
      </article>

      游乐场

      jsFiddle

      $.fn.extend({
        animateAlert: function(animationName, speed, isForwards) {
          var animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
          this.addClass('animated ' + animationName).css("animation-duration", speed + "ms");
          if (!isForwards) {
            this.one(animationEnd, function() {
              $(this).removeClass('animated ' + animationName);
            });
          }
        }
      });
      
      $(document).ready(function() {
        $("#close-alert-box-urgent").on("click", function() {
          $("#alert-box-urgent").animateAlert("rotateOutUpRight", 500, true);
        });
        $("#close-alert-box-news").on("click", function() {
          $("#alert-box-news").animateAlert("zoomOutDown", 500, true);
        });
      });
      .alert-box {
        width: 50vw;
        position: relative;
        margin: 20px auto;
        border: 1px solid black;
      }
      .alert-box-close {
        position: absolute;
        top: -12px;
        right: -12px;
        cursor: pointer;
      }
      .animated {
        -webkit-animation-duration: 1s;
        animation-duration: 1s;
        -webkit-animation-fill-mode: both;
        animation-fill-mode: both;
      }
      .slideOutUp {
        -webkit-animation-name: slideOutUp;
        animation-name: slideOutUp
      }
      @keyframes slideOutUp {
        0% {
          -webkit-transform: translate3d(0, 0, 0);
          transform: translate3d(0, 0, 0)
        }
        100% {
          visibility: hidden;
          -webkit-transform: translate3d(0, -100%, 0);
          transform: translate3d(0, -100%, 0)
        }
      }
      @keyframes zoomOutDown {
        40% {
          opacity: 1;
          -webkit-transform: scale3d(.475, .475, .475) translate3d(0, -60px, 0);
          transform: scale3d(.475, .475, .475) translate3d(0, -60px, 0);
          -webkit-animation-timing-function: cubic-bezier(0.55, .055, .675, .19);
          animation-timing-function: cubic-bezier(0.55, .055, .675, .19)
        }
        100% {
          opacity: 0;
          -webkit-transform: scale3d(.1, .1, .1) translate3d(0, 2000px, 0);
          transform: scale3d(.1, .1, .1) translate3d(0, 2000px, 0);
          -webkit-transform-origin: center bottom;
          transform-origin: center bottom;
          -webkit-animation-timing-function: cubic-bezier(0.175, .885, .32, 1);
          animation-timing-function: cubic-bezier(0.175, .885, .32, 1)
        }
      }
      .zoomOutDown {
        -webkit-animation-name: zoomOutDown;
        animation-name: zoomOutDown
      }
      @keyframes rotateOutUpRight {
        0% {
          -webkit-transform-origin: right bottom;
          transform-origin: right bottom;
          opacity: 1
        }
        100% {
          -webkit-transform-origin: right bottom;
          transform-origin: right bottom;
          -webkit-transform: rotate3d(0, 0, 1, 90deg);
          transform: rotate3d(0, 0, 1, 90deg);
          opacity: 0
        }
      }
      .rotateOutUpRight {
        -webkit-animation-name: rotateOutUpRight;
        animation-name: rotateOutUpRight
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <article class="alert-box" id="alert-box-urgent">
        <h1>Tuition Alert</h1>
        <p>text text text text text text text text text text text text text text text text text text</p>
        <a class="alert-box-close" id="close-alert-box-urgent">
          <img src="http://i.imgur.com/czf8yas.png" height="25" width="25" alt="">
        </a>
      </article>
      
      <article class="alert-box" id="alert-box-news">
        <h1>Latest News</h1>
        <p>text text text text text text text text text text text text text text text text text text</p>
        <a class="alert-box-close" id="close-alert-box-news">
          <img src="http://i.imgur.com/czf8yas.png" height="25" width="25" alt="">
        </a>
      </article>

      作为附注,我建议使用.on() jQuery 方法来附加事件处理程序,而不是使用click()。这是why

      【讨论】:

        猜你喜欢
        • 2015-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-03
        • 2011-03-12
        相关资源
        最近更新 更多