简体   繁体   English

如何将元素从静态布局位置动画到绝对布局位置?

[英]How can I animate an element from a static layout position to an absolute layout position?

I have a form and inside this form a button.我有一个表格,在这个表格里面有一个按钮。 Initially the button is statically positioned at its default position based on usual layout.最初,按钮根据通常的布局静态定位在其默认位置。 On an event (in the example below, button click) I want to move it to the center of the form through animation and during this animation doing a horizontal flip (using scale transform) and when the animation is in the middle (when the rendered width is 0) changing the contents of the button to a paragraph that once loaded will show an animation probably done with svg and a link.在事件(在下面的示例中,单击按钮)上,我想通过动画将其移动到表单的中心,并在此动画期间进行水平翻转(使用缩放变换)以及动画处于中间时(渲染时)宽度为 0) 将按钮的内容更改为一个段落,一旦加载将显示可能使用 svg 和链接完成的动画。

This snippet does a part of what I want (everything until the second part of the flip with changing the contents and resizing the button to be bigger), but without an initial static position from which to start the animation:这个片段做了我想要的一部分(直到翻转的第二部分,改变内容并将按钮调整为更大),但没有开始动画的初始静态位置:

 var form = $("form") var button = $("button") button.on("click", function(){ var x = (form.outerWidth() - button.outerWidth()) / 2; var y = (form.outerHeight() - button.outerHeight()) / 2; button.css({ transform: `translateX(${x}px) translateY(${y}px) scaleX(0)` }); })
 form { background: #aaa; border-radius: 4px; padding: 20px; font-size: 25px; transition: all 0.2s; margin: 0 auto; width: 300px; position: relative; } button { background: #0084ff; border: none; border-radius: 5px; padding: 8px 14px; font-size: 15px; color: #fff; position: absolute; left: 0; top: 0; transition: transform 1s; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form> <p>Hello World</p> <button onclick="return false;">Do something</button> </form>

( https://jsfiddle.net/silviubogan/L1ogpf6a/ ) ( https://jsfiddle.net/silviubogan/L1ogpf6a/ )

How can I achieve what I want in the most correct manner?我怎样才能以最正确的方式实现我想要的? Please note that the rest of the form should remain in place.请注意,表格的其余部分应保留原位。

Thank you.谢谢你。

There's two ways you can do this.有两种方法可以做到这一点。 First is using setTimeout ( reference ) with 1000ms as a parameter, since your css animation lasts 1 second, and a callback function that displays the SVG.首先是使用setTimeout ( reference ) 和 1000ms 作为参数,因为您的 css 动画持续 1 秒,以及显示 SVG 的回调函数。 The second is using jQuery animate ( reference ) instead of css, and using the parameter complete to show your SVG.第二个是使用 jQuery animate ( reference ) 而不是 css,并使用参数complete来显示您的 SVG。 Since you are already using css for the animation, let's go with the first option:由于您已经在使用 css 制作动画,让我们使用第一个选项:

button.on("click", function(){
  // hide button
  window.setTimeout(transform2, 1000);
})

function transform2() {
  // change contents
  // resize button
}

Example fiddle: https://jsfiddle.net/eynL91qu/小提琴示例: https : //jsfiddle.net/eynL91qu/

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

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