jquery - 为什么我的动画 div 沿对 Angular 线移动?

标签 jquery css animation

我试图通过打开一对窗帘来使我的网页动画化。我使用 CSS 并通过将 div 的位置移出屏幕来实现了这个结果。但是,当我对一个按钮进行编程以将动画状态从暂停切换为运行时,div 沿对 Angular 线移出屏幕并在这样做时淡出。为什么?

#curtainLeft{
position: absolute;
z-index: 3;
top:0;
left: -1000px;
height:100%;
animation-name:left;
animation-duration: 1.5s;
animation-play-state: paused;

}
@keyframes left {
from {left: 0px;}
to {left: -1000px;}
}

#curtainRight{
position:absolute;
z-index: 3;
top:0;
right:-1000px;
animation-name:right;
animation-duration: 1.5s;
animation-play-state: paused;

}
@keyframes right {
from {right: 0px;}
to {right: -1000px;}
}

.runAnimation{
animation-play-state: running;
}


<button id="directionsButton" onclick="myFunction()">Begin</button>
</div>
<div id="curtainLeft">
    <img src="./assets/images/curtainLeft.png">
</div>
<div id="curtainRight">
    <img src="./assets/images/curtainRight.png">
</div>



function myFunction(){

$("#curtainRight").toggle("runAnimation");
$("#curtainLeft").toggle("runAnimation")
$("#directionsButton").remove();
audio.play();
}

最佳答案

起初,我认为这是一个错误,但后来我注意到,runAnimation 类从未分配给 div,然后我看到动画本身完全由 js 处理。答案很简单。你误用了.toggle() - 隐藏/显示元素的功能,.toggleClass()是你想要的。

您还需要为 animation-play-state: running 添加 !important 因为它是类的样式,而 animation-play- state: paused; 是优先级较高的 id 的样式。或者你可以像 #curtainLeft.runAnimation {...} 这样写,那么它的优先级会更高,你就不需要 !important 了。显然,将您的窗帘包裹到一个共同的父级中是个好主意,该父级将具有 overflow: hidden; 并且可能 pointer-events: none;。我只是在片段中使用了 body

$('#directionsButton').click(() => {
  $("#curtainRight").toggleClass("runAnimation");
  $("#curtainLeft").toggleClass("runAnimation")
  $("#directionsButton").remove();
});
body {overflow: hidden;}

#curtainLeft,
#curtainRight {
  height: 100%;
  width: 50%;
  position: absolute;
  z-index: 3;
  top: 0;
  animation-duration: 1.5s;
  animation-play-state: paused;
}

#curtainLeft {
  left: -1000px;
  background: firebrick;
  animation-name: left;
}

#curtainRight {
  right: -1000px;
  background: rebeccapurple;
  animation-name: right;
}

#directionsButton {
  position: absolute;
  left: 10px;
  right: 10px;
  z-index: 10000;
}

.runAnimation {
  animation-play-state: running !important;
}

@keyframes left {
  from {left: 0;}
  to {left: -1000px;}
}

@keyframes right {
  from {right: 0;}
  to {right: -1000px;}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="directionsButton">Begin</button>
<div id="curtainLeft"></div>
<div id="curtainRight"></div>

关于jquery - 为什么我的动画 div 沿对 Angular 线移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58494972/

相关文章:

javascript - jQuery 获取 YouTube 视频标题

javascript - 将表单输入与 JSON 对象进行比较

jquery hasclass 用于多标签

css - 获取即将到来的 CSS 过渡属性

html - 使用粘性图像时删除页面底部的空白

ios - 为什么 UITextField 在 vi​​ewDidAppear 中调用 becomeFirstResponder 会减慢当前动画的速度?

javascript - 保持div可以放大的大小

javascript - 失去 HTML 表中文本字段的焦点以启动 JQuery 计算

android - 将 View 本身移动到动画的结束位置

ios - Swift:UIView.transitionWithView 完成 block 问题