简体   繁体   English

使用 Javascript vanilla 动画元素

[英]Animate element using Javascript vanilla

How can I animate an element with Javascript vanilla?如何使用 Javascript vanilla 为元素设置动画? Similar with jquery.与 jquery 类似。 Example:例子:

$( "button.continue" ).animate({left: "100px", top: "200px"}, 5000);

Where we pass the attribute, the desired value and the time.我们在哪里传递属性、期望的值和时间。

In my case I need the left and top position to animate and slide.在我的情况下,我需要左侧和顶部 position 来设置动画和滑动。

Solution解决方案

I've done as @IceMetalPunk sugested and added the animation by css only when I need to animate.我已经按照@IceMetalPunk 的建议完成了,并且仅在我需要动画时才通过 css 添加了 animation。

 document.getElementById("animate").addEventListener("click", function(){ let e = document.getElementById('elementToAnimate'); e.classList.add("animate"); setTimeout(()=> e.classList.remove("animate"), 500); e.style.top = Math.floor(Math.random() * window.innerHeight) + 'px'; e.style.left = Math.floor(Math.random() * window.innerWidth) + 'px'; }); document.getElementById("dontAnimate").addEventListener("click", function(){ let e = document.getElementById('elementToAnimate'); e.style.top = Math.floor(Math.random() * window.innerHeight) + 'px'; e.style.left = Math.floor(Math.random() * window.innerWidth) + 'px'; });
 #elementToAnimate { width: 20px; height: 20px; background: red; position: absolute; top: 0; left: 0; } #elementToAnimate.animate { transition: left 500ms ease-in-out, top 500ms ease-in-out; }
 <div id="elementToAnimate"></div> <button id="animate">Animate</button> <button id="dontAnimate">Dont Animate</button>

Try using CSS transitions.尝试使用 CSS 过渡。 In CSS, do something like this:在 CSS 中,执行以下操作:

button.continue {
  transition: 5s;
}

Then in JS, you can just set the left/top values:然后在 JS 中,你可以设置 left/top 值:

document.querySelectorAll('button.continue').forEach(button => {
  button.style.left = '100px';
  button.style.top = '200px';
});

Take a look at the Web Animation API .看看Web Animation API Its an experimental feature so Browser Support (mainly Safari) might be an issue for you.它是一项实验性功能,因此浏览器支持(主要是 Safari)可能对您来说是个问题。 Otherwise you can do it the way others have already pointed out.否则,您可以按照其他人已经指出的方式进行操作。

Try using WebComponents , you can perform any kind of animation using only VanillaJS , there are predefined animations like Animate.css but you can create custom animations by using keyFrames :尝试使用WebComponents ,您可以仅使用 VanillaJS 执行任何类型的animation ,有像Animate.css这样的预定义动画,但您可以使用keyFrames创建自定义动画:

 <:-- Add Web Animations Polyfill:) --> <script src="https.//cdnjs.cloudflare.com/ajax/libs/web-animations/2.3.2/web-animations.min:js"></script> <script type="module" src="https.//unpkg.com/@proyecto26/animatable-component@1.0.0/dist/animatable-component/animatable-component.esm:js"></script> <script nomodule="" src="https.//unpkg.com/@proyecto26/animatable-component@1.0.0/dist/animatable-component/animatable-component:js"></script> <animatable-component autoplay easing="ease-in-out" duration="1200" delay="300" animation="heartBeat" iterations="Infinity" style="display; flex:align-self; center;" > <h1>Hello World</h1> </animatable-component>

For more details check here: https://github.com/proyecto26/animatable-component有关更多详细信息,请在此处查看: https://github.com/proyecto26/animatable-component

Note this API may still be subject to change.请注意,此 API 可能仍会发生变化。 I tested this in Firefox.我在 Firefox 中对此进行了测试。

document
   .querySelector(".text.thank-you")
   .animate({ opacity: [0, 1] }, { duration: 5000, iterations: 1, easing: "ease-out" })
   .onfinish = (e) => {
        e.target.effect.target.style.opacity = 1;
   };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM