jQuery 效果 - 滑动slideDown()、slideToggle()、slideUp()示例

slideDown:用滑动运动显示匹配的元素

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>slideDown demo</title>
  <style>
  div {
    background: #cfd;
    margin: 3px;
    width: 50px;
    text-align: center;
    float: left;
    cursor: pointer;
    border: 2px outset black;
    font-weight: bolder;
  }
  input {
    display: none;
    width: 120px;
    float: left;
    margin: 10px;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<div>Push!</div>
<input type="text">
<input type="text" class="middle">
<input type="text">

<script>
$( "div" ).click(function() {
  $( this ).css({
    borderStyle: "inset",
    cursor: "wait"
  });
  $( "input" ).slideDown( 1000, function() {
    $( this )
      .css( "border", "2px red inset" )
      .filter( ".middle" )
        .css( "background", "yellow" )
        .focus();
    $( "div" ).css( "visibility", "hidden" );
  });
});

</script>

</body>
</html>

slideToggle:使用滑动运动显示或隐藏匹配的元素。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>slideToggle demo</title>
  <style>
  div {
    background: #b977d1;
    margin: 3px;
    width: 60px;
    height: 60px;
    float: left;
  }
  div.still {
    background: #345;
    width: 5px;
  }
  div.hider {
    display: none;
  }
  span {
    color: red;
  }
  p {
    clear: left;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<div></div>
<div class="still"></div>
<div style="display:none;">
</div><div class="still"></div>
<div></div>
<div class="still"></div>
<div class="hider"></div>
<div class="still"></div>
<div class="hider"></div>
<div class="still"></div>
<div></div>
<p><button id="aa">Toggle</button> There have been <span>0</span> toggled divs.</p>

<script>
$( "#aa" ).click(function() {
  $( "div:not(.still)" ).slideToggle( "slow", function() {
    var n = parseInt( $( "span" ).text(), 10 );
    $( "span" ).text( n + 1 );
  });
});
</script>

</body>
</html>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>slideToggle demo</title>
  <style>
  p {
    width: 400px;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<button>Toggle</button>
<p>
  This is the paragraph to end all paragraphs.  You
  should feel <em>lucky</em> to have seen such a paragraph in
  your life.  Congratulations!
</p>

<script>
$( "button" ).click(function() {
  $( "p" ).slideToggle( "slow" );
});
</script>

</body>
</html>

slideUp:用滑动运动隐藏匹配的元素。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>slideUp demo</title>
  <style>
 div {
   margin: 2px;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<div>
  <button>Hide One</button>
  <input type="text" value="One">
</div>

<div>
  <button>Hide Two</button>
  <input type="text" value="Two">
</div>

<div>
  <button>Hide Three</button>
  <input type="text" value="Three">
</div>

<div id="msg"></div>

<script>
$( "button" ).click(function() {
  $( this ).parent().slideUp( "slow", function() {
    $( "#msg" ).text( $( "button", this ).text() + " has completed." );
  });
});
</script>

</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面是一个使用原生 JavaScript 实现 jQueryslideUp 和 slideDown 效果示例: HTML 代码: ```html <button id="btn">Toggle</button> <div id="box" style="height: 200px; background-color: yellow;">Content</div> ``` JavaScript 代码: ```javascript var box = document.getElementById('box'); var btn = document.getElementById('btn'); function slideUp() { var height = box.offsetHeight; box.style.transitionProperty = 'height, margin, padding'; box.style.transitionDuration = '0.5s'; box.style.boxSizing = 'border-box'; box.style.height = height + 'px'; box.offsetHeight; box.style.overflow = 'hidden'; box.style.height = 0; } function slideDown() { var height = box.offsetHeight; box.style.transitionProperty = 'height, margin, padding'; box.style.transitionDuration = '0.5s'; box.style.boxSizing = 'border-box'; box.style.height = 0; box.offsetHeight; box.style.overflow = 'hidden'; box.style.height = height + 'px'; } btn.addEventListener('click', function() { if (box.classList.contains('open')) { box.classList.remove('open'); slideUp(); } else { box.classList.add('open'); slideDown(); } }); ``` 在这个例子中,我们使用了 `transition` 属性来实现动画效果,并使用 `setTimeout` 函数来确保 CSS 的变化应用于 DOM 元素。由于 JavaScript 与 CSS 的执行顺序不同,我们需要使用 `offsetHeight` 属性来强制 JavaScript 引擎重新计算元素的高度,以便确保动画效果可以正确应用。 注意,在这个示例中,我们使用了一个 `.open` 类来标识盒子是否处于打开状态。这个类可以用来在 JavaScript 中切换 slideUp 和 slideDown 效果

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

生活中的思索

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值