CSS3 - 通过 JavaScript 动画 margin-left 属性
考虑到这个概念证明 ,是否可以通过 JavaScript 对 margin-left(负值和正值)进行动画处理?...您将如何做到这一点?
注意:我知道这仅适用于 WebKit。我对此很满意,因为我正在为 iOS Safari 进行开发。
更新
感谢您的回答,但是 jQuery 的 animate 函数不支持纯 CSS 动画,而这正是我所需要的。
Considering this proof of concept, would it be possible to animate margin-left (both negative and positive values) through JavaScript?.. And how would you go about doing so?
Note: I know this is WebKit-only. And I'm fine with that, seeing as I am developing for iOS Safari.
Update
Thanks for the answers, but jQuery's animate function doesn't support pure CSS animations, which is what I need.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我知道你特别说“你能用 JavaScript 做到这一点吗”,但你不应该需要使用 JavaScript。我相当确定您链接到的概念证明仅使用 jQuery 作为使动画回退到 JavaScript 的一种方式,以便所有浏览器都能很好地播放动画。由于您是专门为 Mobile Safari 进行开发,因此您不需要为此使用 jQuery,除非使用历史记录插件来推送和弹出状态以使浏览器的后退按钮正常工作;这完全可以通过 CSS 转换属性和
:target
伪选择器实现。因此,作为替代方案,您应该能够执行以下操作:
在 HTML 中:
在 CSS 中:
只要您的片段 URL 与要转换的元素的名称匹配,那么您应该如果您绝对必须这样做,则能够使用 JavaScript 将片段推送到 URL,而不是在仍然进行转换的情况下使用带有片段 href 的锚点。如果您使用 jQuery 历史插件或自己推送和弹出历史堆栈,那么您的应用程序仍然可以获得后退按钮行为。
我知道您特别要求使用 JavaScript 解决方案来触发 CSS 动画,但我只是不确定为什么您需要这个。很抱歉,如果这对您没有任何帮助。
更新:这是一个 jsFiddle 演示上述内容。它使用这个 jQuery历史插件来管理历史堆栈,这样你仍然可以获得可接受的后退/前进按钮来自片段 URL 的行为。锚标记在其 onClick 中使用插件的“push”或“load”方法,并在 href 属性中使用标准片段作为未启用 JS 的浏览器的后备。
更新2: 这是另一个使用变换/翻译而不是转换的jsFiddle。
更新3(由roosteronacid):
至于通过JavaScript获得动画,你可以这样做:
I know that you specifically say "can you do this in JavaScript", but you shouldn't need to use JavaScript. I'm fairly certain that the proof of concept you link to only uses jQuery as a way to make the animations fall back to JavaScript so that all browsers play nice with the animation. Since you're specifically developing for Mobile Safari, you shouldn't need to use jQuery for this except to use a history plugin to push and pop states to make the browser's back button work; this is entirely doable via CSS transition properties and the
:target
pseudo-selector.So as an alternative, you should be able to do something like this:
In HTML:
In CSS:
As long as your fragment URL matches up with the name of the element that you want to transition then you should be able to push the fragment in to the URL using JavaScript if you absolutely have to instead of using anchor with a fragment href while still having the transition take place. And if you use a jQuery history plugin or do your own pushing and popping of the history stack then you still get back-button behavior for your app.
I know you specifically asked for a JavaScript solution to trigger the CSS animation, but I'm just not sure why this is what you need. Sorry if this doesn't help you at all.
UPDATE: Here's a jsFiddle demonstrating the above. It uses this jQuery history plugin to manage the history stack, so that you can still get acceptable back/forward button behavior from the fragment URL's. The anchor tag uses the plugin's "push" or "load" method in its onClick with a standard fragment in the href attribute as a fallback for browsers without JS enabled.
UPDATE 2: Here's another jsFiddle that uses transforms/translations instead of transitions.
UPDATE 3 (by roosteronacid):
And as for getting the animations going through JavaScript, you can do:
您可以在 css3 中设置过渡,然后对元素的后续更改将被动画化。
这指定了一个几乎跨浏览器(该死的IE)过渡,适用于所有CSS更改,持续0.3秒并缓和,因此它会在过渡结束时减慢速度。因此,要将其动画到左/右,只需更改 css:
请注意,这会将其动画到 300px 的固定位置,如果您想动画到相对于其当前位置的位置,请使用:
在此处查看工作示例 (jsFiddle)
You can set a transition in css3, and then subsequent changes to the element will be animated.
This specifies a nearly cross browser (damn IE) transition that applies to all css changes, lasts 0.3 seconds and eases, so it will slow down towards the end of the transition. Therefore, to animate it to the left/right, simply change the css:
Note this will animate it to a fixed position of 300px, if you want to animate to a position relative to its current location use:
See a working example here (jsFiddle)
最好使用几乎跨浏览器支持的过渡(IE除外),并通过JS设置关键帧。
Better use transitions, that are almost cross-browser supported (except IE), and set the keyframes through JS.
这仅适用于 CSS、HTML 和 WebKit:
只需在 HTML 文件中创建一个
即可对其进行测试。在 Google Chrome 12.0.742.112 和 Safari 5.0.5 (6533.21.1) 中为我工作。如果它没有立即执行动画,可能是由于您的浏览器处理翻译的速度太快(或者可能是缓存?)。您可能会考虑以某种方式添加延迟。我只是快速地按了几次刷新按钮。为我工作。
编辑:
查看 girliemac 测试页面背后的源代码。那里有一些有见地的东西。另请参阅此SO帖子。
This works using CSS, HTML and WebKit only:
Just make a
<div id="wrapper">Placeholder text</div>
in an HTML file to test it out. Worked for me in Google Chrome 12.0.742.112 and Safari 5.0.5 (6533.21.1). If it doesn't do the animation right away, it may be due to your browser processing the translation too quickly (or caching, perhaps?). You might consider adding a delay somehow. I just pressed the refresh button a few times really fast. Worked for me.Edit:
Check out the source behind girliemac's test page. Some insightful stuff there. Also see this SO post.
您可以使用 jqueries
.animate()
- http://api.jquery.com/ animate/检查我的示例 - http://jsfiddle.net/ajthomascouk/jS83H/ - 按 + 和 -
you can use jqueries
.animate()
- http://api.jquery.com/animate/Check my example - http://jsfiddle.net/ajthomascouk/jS83H/ - Press the + and -
使用 jQuery 动画 css 边距的工作方式如下:
Animating css margins with jQuery works like this:
要使用 webkit 的 css 动画,您需要创建一个具有 Transform 属性的类,然后您可以使用 jQuery 根据需要添加/删除所述类。
To use webkit's css animations, you'd create a class that has the transform property, then you can use jQuery to add/remove said class as needed.