.hide()

.hide( ) Returns: jQuery

Description: 隐藏匹配的元素。

  • version added: 1.0.hide()

  • version added: 1.0.hide( duration, [ callback ] )

    duration一个字符串或者数字决定动画将运行多久。

    callback在动画完成时执行的函数。

  • version added: 1.4.3.hide( [ duration ], [ easing ], [ callback ] )

    duration一个字符串或者数字决定动画将运行多久。

    easing一个字符串,指示该函数使用缓和的过渡。

    callback在动画完成时执行的函数。

如果没有参数,.hide()方法是最简单的方法来隐藏一个元素:

$('.target').hide();

匹配的元素将被立即隐藏,没有动画。这大致相当于调用.css('display', 'none'),但display属性值保存在jQuery的数据缓存中,所以display可以方便以后可以恢复到其初始值。如果一个元素的display属性值为inline,然后是隐藏和显示,这个元素将再次显示inline

当提供一个持续时间参数,.hide()成为一个动画方法。.hide()方法将为匹配元素的宽度,高度,以及不透明度,同时进行动画。一旦透明度达到0,display样式属性将被设置为none,这个元素将不再在页面中影响布局。

持续时间是以毫秒为单位的,数值越大,动画越慢,不是越快。字符串 'fast''slow' 分别代表200和600毫秒的延时。

如果提供回调函数参数,回调函数会在动画完成的时候调用。这个对于将不同的动画串联在一起按顺序排列是非常有用的。这个回调函数不设置任何参数,但是this是存在动画的DOM元素,如果多个元素一起做动画效果,值得注意的是每执行一次回调匹配的元素,而不是作为一个整体的动画一次。

我们可以给任何元素做动画,比如一个简单的图片:

<div id="clickme">
  Click here
</div>
<img id="book" src="book.png" alt="" width="100" height="123" />
With the element initially shown, we can hide it slowly:
$('#clickme').click(function() {
  $('#book').hide('slow', function() {
    alert('Animation complete.');
  });
});

例子:

举例: 点击链接隐藏所有段落。

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
  <p>Hello</p>
  <a href="#">Click to hide me too</a>
  <p>Here is another paragraph</p>
<script>

    $("p").hide();
    $("a").click(function () {
      $(this).hide();
      return true;
    });
</script>

</body>
</html>

Demo:

举例: 缓慢的隐藏所有显示的段落,600毫秒内完成的动画。

<!DOCTYPE html>
<html>
<head>
  <style>
  p { background:#dad; font-weight:bold; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
  <button>Hide 'em</button>

  <p>Hiya</p>
  <p>Such interesting text, eh?</p>
<script>
    $("button").click(function () {
      $("p").hide("slow");
    });    
</script>

</body>
</html>

Demo:

举例: Animates all spans (words in this case) to hide fastly, completing each animation within 200 milliseconds. Once each animation is done, it starts the next one.

<!DOCTYPE html>
<html>
<head>
  <style>
  span { background:#def3ca; padding:3px; float:left; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
  <button id="hidr">Hide</button>
  <button id="showr">Show</button>
  <div>

    <span>Once</span> <span>upon</span> <span>a</span> 
    <span>time</span> <span>there</span> <span>were</span> 
    <span>three</span> <span>programmers...</span>

  </div>
<script>
    $("#hidr").click(function () {
      $("span:last-child").hide("fast", function () {
        // use callee so don't have to name the function
        $(this).prev().hide("fast", arguments.callee); 
      });
    });
    $("#showr").click(function () {
      $("span").show(2000);
    });

</script>

</body>
</html>

Demo:

Example: Hides the divs when clicked over 2 seconds, then removes the div element when its hidden. Try clicking on more than one box at a time.

<!DOCTYPE html>
<html>
<head>
  <style>
  div { background:#ece023; width:30px; 
        height:40px; margin:2px; float:left; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
  <div></div>
<script>
    for (var i = 0; i < 5; i++) {
      $("<div>").appendTo(document.body);
    }
    $("div").click(function () {
      $(this).hide(2000, function () {
        $(this).remove();
      });
    });
</script>

</body>
</html>

Demo:

jQuery 1.6 API 中文版Clove整理、修订