jquery - 反转 CSS 转换的顺序

标签 jquery css css-transitions

我有一个菜单按钮和一个隐藏列表。我正在切换类以创建下拉效果。

添加类后,它会很好地打开下拉菜单。

但是,正如您将在下面的代码片段中看到的那样,在删除类时,转换并不是那么无缝。基本上我想做的是颠倒转换发生的顺序,但我不确定如何实现这一点。

感谢您的帮助。

$('.contact-menu-button').children('svg').click(function (event) {
	event.stopPropagation();
  $('.contact-menu-items').toggleClass('show');
  $('.contact-menu-button').children('svg').toggleClass('rotate');
});
@import url(https://fonts.googleapis.com/css?family=Kanit:200,400);

* {
  font-family: Kanit, sans-serif;
}

div {
  display: inline-block;
  padding: 5px;
}

.container {
  width: 90%;
  color: #fff;
  background-color: #4a7ff7;
  border-radius: 8px 8px 0 0;
  box-shadow: 0px 5px 5px 0px rgba(0, 0, 0, 0.4);
}

.active-contact {
  padding-right: 0;
}

.contact-menu {
  position: relative;
  padding-left: 0;
}

.contact-menu-button {
  background-color: transparent;
  border: none;
  border-radius: 4px;
  outline: none;
  color: #fff;
  padding: 0;
  width: 25px;
  cursor: pointer;
  transform: translateY(3px);
}

.contact-menu-button > svg {
    height: 20px;
    width: 20px;
    border-radius: 15px;
    stroke: #fff;
    transition: transform 250ms linear, border-radius 250ms ease-in 275ms;
}

.contact-menu-button > svg:hover {
  background-color: #fff;
  stroke: #4a7ff7;
}

.contact-menu-button > svg:active {
    stroke: #fff;
    background-color: #000;
}

.contact-menu-button > .rotate {
    background-color: #000;
    border-radius: 0 0 15px 15px;
    transform: rotate(180deg);
}

.contact-menu-button > .rotate:hover {
  background-color: #000;
  stroke: #fff;
}

.contact-menu-items {
    visibility: hidden;
    height: 0;
    overflow: hidden;
    color: #fff;
    position: absolute;
    list-style-type: none;
    padding: 10px 0;
    margin: 0 5px 5px 5px;
    border-radius: 6px 0 6px 6px;
    background-color: #000;
    transform: translate(-57px, -6px);
    transition: visibility 250ms ease-out 550ms, height 250ms ease-out 550ms;
}

.contact-menu-items > li {
    padding: 1px 15px 1px 15px;
}

.contact-menu-items.show {
    visibility: visible;
    height: 50px;
}

.contact-menu-items > li:hover {
    background-color: #fff;
    color: #000;
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<div class="container">
  <div class="active-contact">
    John Smith
  </div>
  <div class="contact-menu">
    <button class="contact-menu-button">
      <svg>
        <path d="M4 9 l 6 5 l 6 -5" stroke-width="1.5" fill="none" />
      </svg>
    </button>
    <ul class="contact-menu-items">
      <li>
        <a>Edit</a>
      </li>
      <li>
        <a>Delete</a>
      </li>
    </ul>
  </div>
</div>

最佳答案

您可以在基础状态和转换状态上设置转换时间:

$('.contact-menu-button').children('svg').click(function (event) {
	event.stopPropagation();
  $('.contact-menu-items').toggleClass('show');
  $('.contact-menu-button').children('svg').toggleClass('rotate');
});
@import url(https://fonts.googleapis.com/css?family=Kanit:200,400);

* {
  font-family: Kanit, sans-serif;
}

div {
  display: inline-block;
  padding: 5px;
}

.container {
  width: 90%;
  color: #fff;
  background-color: #4a7ff7;
  border-radius: 8px 8px 0 0;
  box-shadow: 0px 5px 5px 0px rgba(0, 0, 0, 0.4);
}

.active-contact {
  padding-right: 0;
}

.contact-menu {
  position: relative;
  padding-left: 0;
}

.contact-menu-button {
  background-color: transparent;
  border: none;
  border-radius: 4px;
  outline: none;
  color: #fff;
  padding: 0;
  width: 25px;
  cursor: pointer;
  transform: translateY(3px);
}

.contact-menu-button > svg {
    height: 20px;
    width: 20px;
    border-radius: 15px;
    stroke: #fff;
    transition: transform 250ms linear 275ms, border-radius 250ms ease-in 0ms;
}

.contact-menu-button > svg:hover {
  background-color: #fff;
  stroke: #4a7ff7;
}

.contact-menu-button > svg:active {
    stroke: #fff;
    background-color: #000;
}

.contact-menu-button > .rotate {
    background-color: #000;
    border-radius: 0 0 15px 15px;
    transform: rotate(180deg);
    transition: transform 250ms linear, border-radius 250ms ease-in 275ms;
}

.contact-menu-button > .rotate:hover {
  background-color: #000;
  stroke: #fff;
}

.contact-menu-items {
    visibility: hidden;
    height: 0;
    overflow: hidden;
    color: #fff;
    position: absolute;
    list-style-type: none;
    padding: 10px 0;
    margin: 0 5px 5px 5px;
    border-radius: 6px 0 6px 6px;
    background-color: #000;
    transform: translate(-57px, -6px);
    transition: visibility 250ms ease-out, height 250ms ease-out;
}

.contact-menu-items > li {
    padding: 1px 15px 1px 15px;
}

.contact-menu-items.show {
    visibility: visible;
    height: 50px;
    transition: visibility 250ms ease-out 550ms, height 250ms ease-out 550ms;
}

.contact-menu-items > li:hover {
    background-color: #fff;
    color: #000;
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<div class="container">
  <div class="active-contact">
    John Smith
  </div>
  <div class="contact-menu">
    <button class="contact-menu-button">
      <svg>
        <path d="M4 9 l 6 5 l 6 -5" stroke-width="1.5" fill="none" />
      </svg>
    </button>
    <ul class="contact-menu-items">
      <li>
        <a>Edit</a>
      </li>
      <li>
        <a>Delete</a>
      </li>
    </ul>
  </div>
</div>

关于jquery - 反转 CSS 转换的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44395877/

相关文章:

javascript - 如何从打开 UPS 页面返回 UPS 运输报价

javascript - 如何强制元素在中间?

javascript - 添加覆盖后如何禁用 div 元素?

html - CSS3 : Prevent transition-delay when transition is reversed

css - 可以使用css3创建这样的背景吗?

jquery - 检查 Bootstrap 折叠导航栏是否溢出

javascript - 使用 jQuery 在网页背景中显示图像

css - 伪选择第 n 个 child 问题

html - 背景颜色未出现在此上下文中

css - CSS 显示属性的过渡