javascript - 使用 JS 动画元素

标签 javascript css

我试图将一个 div 从其原始位置向左移动,即向右移动,我想要的效果是 div 向左移动然后向右滑动一点。

仅限 Vanilla JS。

代码:

CSS:

leftBtn.addEventListener("click", function() {
    pushDiv.style.right = "420px";
    pushDiv.style.right = "360px";
    });
* {
  box-sizing: border-box;
  font-family: 'Montserrat', sans-serif;
  transition: 0.5s ease;
}

.holder{
  position: absolute;
  left: 50%;
  top: 50%;
  height: 300px;
  width: 800px;
  margin: 0 auto;
  background: #eee;
  transform: translate(-50%, -50%);
  box-shadow: 4px 9px 2px #000;
}
.push-div {
  width: 350px;
  position: absolute;
  background: #F44336;
  height: 370px;
  right: 0;
  top: -35px;
  border-radius: 5px;
}
<div class="holder">
        <button type="button" name="button" id="btn1">Left</button>
        <button type="button" name="button" id="btn2">Right</button>
        <div class="push-div" id="pushDiv">

        </div>

但是在点击按钮时它显示 360px 而不是给出效果。

我该如何实现?我试过添加延迟,但这似乎不起作用。

最佳答案

var leftBtn = document.getElementById('leftBtn'),
  pushDiv = document.getElementById('pushDiv');

leftBtn.addEventListener("click", function() {
  pushDiv.style.right = "410px";
  setTimeout( function() {
    pushDiv.style.right = "360px";
    }, 600 );
});
#pushDiv {
  position: absolute;
  background: red;
  top: 100px;
  right: 200px;
  width: 100px;
  height: 100px;
  transition: all .6s;
}
<button id="leftBtn">Move It</button>
<div id="pushDiv"></div>

关于javascript - 使用 JS 动画元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48144351/

相关文章:

javascript对象解析并创建新的对象数组

javascript - 获取 vanilla JS 中选择元素的前一个(初始)和所选值

php - 是否可以使用 JS 打开具有所需内容类型的浏览器窗口?

javascript - 使用 CSS 或 JavaScript 缩放背景图像

html - 如何在父 DIV 内垂直居中放置两个子 DIV

javascript - 在超过 1000 页的 PDF 文件中搜索文本

javascript - 硬拷贝中的打印按钮

asp.net - css 虽然代码改变了,但图像没有改变

html - ionic 头内图像的大小

html - 删除焦点上的蓝色突出显示文本