简体   繁体   English

单击按钮将div1移出屏幕,将div2从顶部移至20px时会弹出动画效果

[英]bouncing animation effect when click on button to move div1 out of screen and div2 to 20px from top

plz help thanks in advance...sorry for bad english ... want a animation effect with bouncing when div1 and div2 goes up and down plz提前感谢您...抱歉英语不好...希望当div1和div2上下波动时出现跳动的动画效果

I have made main div in which there are 2 div... div1 and div2. 我做了主div,其中有2 div ... div1和div2。

I want to make it like, when someone click on button. 我想让它像有人单击按钮时一样。

That time div1 should slowly move out of the screen from top side with animation (slowly moving out of the top side screen). 那个时间div1应该随着动画从上侧缓慢移出屏幕(缓慢移出上侧屏幕)。

And at the same time div2 should take a place 20px from top of screen. 并且同时div2应该位于距离屏幕顶部20px的位置。

I have setup almost everything but I'm not able to give it a animation effect no wonder why but transition effect or anything not working on it. 我已经设置了几乎所有内容,但是我无法给它提供动画效果,也难怪为什么要设置过渡效果或其他不起作用的效果。 I want to do it with JavaScript code if possible I less use jQuery or etc. 我想用JavaScript代码来做,如果可能的话,我较少使用jQuery等。

I am happy to use another approach, like instead of using margin-top to move div1 out of screen and div2 to top side if you recommend any other css or javascript, that's fine. 我很乐意使用另一种方法,例如,如果您建议使用任何其他CSS或JavaScript,那么也可以使用margin-top将div1移出屏幕,将div2移出顶部。 I just want the animation effect in which slowly div1 and div2 move to top side. 我只希望div1和div2慢慢移到顶部的动画效果。

 function myFunction1() { document.getElementById("div1").style.marginTop = "-110px"; document.getElementById("div2").style.marginTop = "20px"; } function myFunction2() { document.getElementById("div1").style.marginTop = "0px"; document.getElementById("div2").style.marginTop = "0px"; } 
 #main { background-color: grey; height: 1000px; width: 100%; margin: 0; padding: 0; } #div1 { background-color: red; height: 100px; width: 100%; margin-top: 0px; position: relative; } #div2 { background-color: green; height: 100px; width: 100%; margin-top: 0px; position: relative; } 
 <div id="main"> <div id="div1"> </div> <div id="div2"> </div> <button onclick="myFunction1()">Button 1</button> <button onclick="myFunction2()">Button 2</button> </div> 

Just add a CSS transition to your elements. 只需将CSS过渡添加到您的元素即可。 You can change the parameters to taste. 您可以更改参数以进行品尝。 I believe this is the cleanest and most efficient solution. 我相信这是最干净,最有效的解决方案。

EDIT: As per OP's request in the comments, I've shown an example of a transition function which emulates a slight "bounce" effect on the transition using extreme values. 编辑:根据OP在注释中的要求,我展示了一个过渡函数的示例,该函数使用极值模拟过渡上的轻微“弹跳”效果。 You can quickly create your own transition function and test it in real time using online tools (ex: cubic-bezier.com ) or the Chrome developer tools (you can click on a transition function for an element you are inspecting to edit the function in real time). 您可以快速创建自己的转换函数,并使用在线工具(例如: cubic-bezier.com )或Chrome开发者工具(可以在要查看的元素上单击转换函数,以在其中编辑该函数)进行实时测试。即时的)。 Note that this functionality is currently not universally supported (see: Safari Bug 45761 ) 请注意,目前尚不普遍支持此功能(请参阅: Safari Bug 45761

Transitions on MDN 在MDN上进行过渡

 function myFunction1() { document.getElementById("div1").style.marginTop = "-110px"; document.getElementById("div2").style.marginTop = "20px"; } function myFunction2() { document.getElementById("div1").style.marginTop = "0px"; document.getElementById("div2").style.marginTop = "0px"; } 
 #main { background-color: grey; height: 1000px; width: 100%; margin: 0; padding: 0; } /* *** THIS IS NEW *** */ #main div { -webkit-transition: margin 1s cubic-bezier(1,.7,.49,1.7); transition: margin 1s cubic-bezier(1,.7,.49,1.7); } /* *** END NEW *** */ #div1 { background-color: red; height: 100px; width: 100%; margin-top: 0px; position: relative; } #div2 { background-color: green; height: 100px; width: 100%; margin-top: 0px; position: relative; } 
 <div id="main"> <div id="div1"> </div> <div id="div2"> </div> <button onclick="myFunction1()">Button 1</button> <button onclick="myFunction2()">Button 2</button> </div> 

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

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