简体   繁体   English

使用 DOM 插入 Jquery 的 javascript 中的动画

[英]Animation in javascript with DOM insted of Jquery

Is possible to recreate this animation with DOM instead of Jquery?是否可以使用 DOM 而不是 Jquery 重新创建此动画? if so how?如果是这样怎么办?

$('.bubbles-animation')
  .animate({
    'bottom': '100%',
    'opacity' : '-=0.7'
   }, 2000, function(){
     $(this).remove()
   }
);

jQuery is just js and css under the hood. jQuery 只是底层的 js 和 css。 It is absolutely possible to recreate any jquery action in vanilla js.绝对有可能在 vanilla js 中重新创建任何 jquery 操作。 If you wanted to create a 1:1 replica of jquery action, you could inspect the source code to see how jquery is accomplishing the task.如果您想创建 jquery 操作的 1:1 副本,您可以检查源代码以查看 jquery 如何完成任务。 However, if all you need to do is use vanilla js to create an animation and then remove the element, here is a demo of how to accomplish that.但是,如果您需要做的只是使用 vanilla js 创建动画然后删除元素,这里是如何完成此操作的演示。

My entire code snippet is wrapped in an async anonymous function which will be immediately called.我的整个代码片段都包含在一个异步匿名函数中,该函数将立即被调用。 This prevents naming conflicts within the scope of my code, and also allows me to access the await keyword.这可以防止我的代码范围内的命名冲突,并且还允许我访问await关键字。

First I create two helper functions.首先,我创建了两个辅助函数。 $ will make it so I can grab elements from the DOM in a way that resembles jquery. $将使我可以以类似于 jquery 的方式从 DOM 中获取元素。 The wait helper function returns a promise that resolves after a timeout. wait助手函数返回一个在超时后解决的承诺。

I wait 1000ms or 1s before changing the opacity as a personal preference to the timing of the animation.我在更改不透明度之前等待 1000 毫秒或 1 秒,这是个人对动画时间的偏好。

After I change the opacity I wait 2 more seconds, which is also how long it takes the transition to display, and then I remove the element.更改不透明度后,我再等 2 秒,这也是显示过渡所需的时间,然后我删除元素。

Keep in mind, this will only target the first element with a class of bubbles-animation , also I would like to add that personally I prefer changing transition on the height rather than the opacity because it collapses other elements a bit more smoothly in my own opinion.请记住,这只会针对具有一类bubbles-animation的第一个元素,另外我想补充一点,我个人更喜欢更改高度上的过渡而不是不透明度,因为它可以更平滑地折叠我自己的其他元素观点。

The css is pretty self explanatory but if you have any trouble with it I would recommend checking out this demo css 是不言自明的,但如果您有任何问题,我建议您查看此演示

 (async () => { const $ = str => document.querySelector(str); const wait = t => new Promise(r => setTimeout(r, t)); await wait(1000); $(".bubbles-animation").style.opacity = "0.3"; await wait(2000); $(".bubbles-animation").remove(); })();
 .bubbles-animation { transition: opacity 2s; }
 <div class="bubbles-animation">Vanish!</div> <div>Stay!</div>

Yes, you can rely on CSS transitions to achieve it.是的,您可以依靠 CSS 过渡来实现它。 However, that'll depend on what you'll try to achieve.但是,这取决于您将尝试实现的目标。 There is not enough information to go far in the reflection.没有足够的信息在反射中走得更远。

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions

 document.getElementById('css-js') .addEventListener('click', () => document.querySelector('.bubbles').classList.add('animation')) document.getElementById('reset') .addEventListener('click', () => document.querySelector('.bubbles').classList.remove('animation'))
 #settings { position: fixed; top: 0px; } .bubbles { position: absolute; height: 100%; top: 0px; width: 100%; background-color: green; transition-property: transform, opacity; transition-duration: 2s, 1.5s; } .animation { opacity: 0; transform: translateY(-100%); }
 <div class=bubbles></div> <div id=settings> <button id=css-js>CSS &amp; Native JS</button> <button id=reset>Reset</button> </div>

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

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