javascript - 单击事件后,jQuery slideDown() 函数对两个元素均不起作用

标签 javascript jquery html css

我想使用 slideDown() 函数制作动画,但不知何故,.quote-container 不包含在其中,即使 slideDown() 函数包含 .quote-container#new-quote 元素。

为什么它们在第一个加载动画中有效,而在第二个加载动画中(在点击事件之后)却没有(只有 #new-quote 有效)? .quote-container 仅在 slideDown() 动画结束后显示,即 2 秒。

// slideDown() works only for #new-quote
$("#new-quote").on("click", function() {
  $(".quote-container, #new-quote").slideUp(2000, function() {
    $(".quote-container, #new-quote")
      .css("display", "inline-block")
      .hide();

    $(".quote-container, #new-quote").slideDown(2000);
  });
});

// slideDown() for both elements works
var height = $(".quote-container").outerHeight();
$(".quote-container, #new-quote")
  .css("display", "inline-block")
  .hide();
$("#new-quote").css("height", height + 80);
$(".quote-container").css("padding", "2.5rem");
$(".quote-container, #new-quote").slideDown(2000);
html,
body {
  height: 100%;
  width: 100%;
}

body {
  margin: 0;
  padding: 0;
  background: #333;
  color: #333;
  font-family: sans-serif;
}

.v-wrap {
  height: 100%;
  text-align: center;
}

.v-wrap:before {
  content: "";
  display: inline-block;
  vertical-align: middle;
  width: 0;
  height: 100%;
}

.quote-container {
  width: 31.25rem;
  background: #fff;
  margin: auto;
  vertical-align: middle;
  border-radius: 0.1875rem;
  padding: 0 2.5rem;
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
  display: none;
}

.quote-text {
  font-size: 1.625rem;
}

.quote-text i {
  margin-right: 0.6rem;
}

.quote-text p {
  display: inline;
}

.quote-author {
  font-size: 1rem;
  margin: 0 0.4rem 2rem 0;
  text-align: right;
}

.button {
  padding: 0.75rem;
  text-align: center;
  font-size: 1em;
  color: #fff;
  border-radius: 3px;
  display: inline-block;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
}

.button:not(#new-quote) {
  min-width: 1rem;
  min-height: 1rem;
}

.button:hover:not(#new-quote) {
  opacity: 0.8;
}

.button i {
  vertical-align: middle;
}

#new-quote {
  white-space: nowrap;
  writing-mode: vertical-lr;
  height: 100%;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  vertical-align: middle;
  background: #fff !important;
  position: relative;
  right: 0.25625rem;
  color: #333;
  padding: 0 .75rem;
  display: none;
}

#new-quote:before {
  content: "";
  position: absolute;
  height: 100%;
  width: 0.0625rem;
  bottom: 0;
  left: 0;
  visibility: hidden;
  -webkit-transform: scaleY(0);
  transform: scaleY(0);
  -webkit-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}

#new-quote:hover:before {
  visibility: visible;
  -webkit-transform: scaleY(1);
  transform: scaleY(1);
}

footer {
  font-size: 0.85rem;
}

footer a {
  position: relative;
  text-decoration: none;
  color: #fff;
}

footer a:before {
  content: "";
  position: absolute;
  width: 100%;
  height: 1px;
  bottom: 0;
  left: 0;
  background: #fff;
  visibility: hidden;
  -webkit-transform: scaleX(0);
  transform: scaleX(0);
  -webkit-transition: all 0.3s ease-in-out 0s;
  transition: all 0.3s ease-in-out 0s;
}

footer a:hover:before {
  visibility: visible;
  -webkit-transform: scaleX(1);
  transform: scaleX(1);
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<div class="v-wrap">
  <div class="quote-container">
    <div class="quote-text">
    </div>
    <div class="quote-author"></div>
    <a id="tweet-quote" class="button"><i class="fa fa-twitter"></i></a>
    <a id="tumblr-quote" class="button"><i class="fa fa-tumblr"></i></a>
  </div>
  <div id="new-quote" class="button">New quote</div>
  <footer><a href="https://codepen.io/Kestis500">Created by LukasLSC</a></footer>
</div>

最佳答案

问题已修复:

我需要从 onclick 中删除 $(".quote-container, #new-quote").css("display", "inline-block").hide(); 行事件:

$("#new-quote").on("click", function() {
  $(".quote-container, #new-quote").slideUp(2000, function() {
    $(".quote-container, #new-quote").slideDown(2000);
  });
});

关于javascript - 单击事件后,jQuery slideDown() 函数对两个元素均不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48037758/

相关文章:

php - 将 javascript 放入文件和脚本标记中有什么区别?

javascript - jquery 上下 slider

html - 导航栏未正确位于菜单中

Java:从html文本到xhtml文本的Jtidy转换

javascript - 如何在选中单选按钮的基础上显示div?

javascript - 如何检查 Node.js 中是否定义了变量?

javascript - 使用复选框控制文本框禁用或自述文件行为

javascript - 在另一个 div 旁边显示相对位置的 div

javascript - 无法在 aptana studio 中运行 "canvas"标记和 javascript 程序

javascript - 当选中复选框时,禁用其他相同值的复选框