以编程方式更改动画规则中的 webkit-transformation 值
我有这个样式表:
@-webkit-keyframes run {
0% {
-webkit-transform: translate3d(0px, 0px, 0px);
}
100% {
-webkit-transform: translate3d(0px, 1620px, 0px);
}
}
现在,我想根据一些参数修改 1620px 的值。像这样:
@-webkit-keyframes run {
0% {
-webkit-transform: translate3d(0px, 0px, 0px);
}
100% {
-webkit-transform: translate3d(0px, height*i, 0px);
}
}
我更希望能够使用 JavaScript 和 jQuery,尽管纯 CSS 解决方案也可以。
这是一款在移动 Apple Safari 浏览器中运行的 iPhone 游戏。
I have this stylesheet:
@-webkit-keyframes run {
0% {
-webkit-transform: translate3d(0px, 0px, 0px);
}
100% {
-webkit-transform: translate3d(0px, 1620px, 0px);
}
}
Now, I would like to modify the value of 1620px depending on some parameters. Like this:
@-webkit-keyframes run {
0% {
-webkit-transform: translate3d(0px, 0px, 0px);
}
100% {
-webkit-transform: translate3d(0px, height*i, 0px);
}
}
I would prefer to be able to use JavaScript and jQuery, though a pure CSS solution would be ok.
This is for an iPhone game that runs in it's mobile Apple Safari browser.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用 CSSOM
如果要修改已包含的样式表中的关键帧规则,请执行以下操作:
如果您不知道顺序但知道 CSS 文件的 URL,请替换
document.styleSheets[0]
与document.querySelector("link[href='
your-css-url.css
']") .sheet
。Use the CSSOM
If you want to modify a keyframe rule in a stylesheet that's already included, do the following:
If you don't know the order but do know the URL of the CSS file, replace
document.styleSheets[0]
withdocument.querySelector("link[href='
your-css-url.css
']").sheet
.您是否尝试过在 html 文档头部的
元素中声明 css 的关键帧部分。然后,您可以为该元素指定一个 id 或其他任何内容,并使用 javaScript 随时更改其内容。像这样:
然后你的jquery可以像平常一样改变它:
Have you tried declaring the keyframe portion of your css in a
<style>
element in the head of your html document. You can then give this element an id or whatever and change it's content whenever you like with javaScript. Something like this:Then your jquery can change this as normal:
从你的例子来看,我认为 CSS 动画可能有点矫枉过正。使用过渡:
然后通过 js 对元素应用新的转换:
它应该为该元素设置动画。
Well from your example it seems to me that CSS animations may be overkill. Use transitions instead:
and then apply a new transform to the element via js:
It should animate that.
我不知道你什么时候想修改这些值(即使用变量),但尽管如此,这里有 3 到 4 个解决方案和 1 个不可能的解决方案(目前)。
服务器端计算:为了不时提供不同的 CSS,您可以告诉 PHP 或任何服务器端语言将
.css
文件解析为以及.php
或.html
,然后在 PHP 标记之间使用 PHP 变量。注意文件缓存:为了避免它,您可以加载一个样式表,如 style.css?1234567890随机或时间它会产生一个明显不同的文件,因此不会被缓存SASS 也是一个需要 Ruby 的服务器端解决方案,将为您提供现有的语法,可能比手工制作的解决方案,就像其他人已经关于问题和解决方案一样
LESS 是一个客户端解决方案,它将加载您的 .less 文件和一个 less.js 文件,该文件将解析前者并为您提供变量在 CSS 中,无论你的服务器是什么。它还可以在服务器端与node.js一起工作
在显示页面时动态修改 CSS?
对于 2D,有 jquery-animate-enhanced 来自 Ben Barnett,2d-transform 或 CSS3 旋转 的方向相反(它们在可能的情况下使用 CSS3,并且在没有此类功能的情况下,它们回退到现有的 jQuery .animate() 和 IE 矩阵过滤器)但仅此而已。
您可以创建一个 jQuery 插件,它可以使用一些参数来管理您想要通过 3D 实现的目标转换并避免在 DOM 中修改又长又复杂的 CSS 规则的麻烦
仅 CSS:您可以使用 -moz-calc [1][2] 仅适用于 Firefox 4.0,而 -webkit-transform 仅适用于...好吧没关系:-)
I didn't get when you wanted to modify these values (i.e. use variables) but nevertheless here are 3 to 4 solutions and 1 impossible solution (for now).
server-side calculation: in order to serve a different CSS from time to time, you can tell PHP or any server-side language to parse
.css
files as well as.php
or.html
and then use PHP variables in-between PHP tags. Beware of file caching: to avoid it, you can load a stylesheet like style.css?1234567890random-or-time it will produce an apparent different file and thus won't be cachedSASS is also a server-side solution that needs Ruby and will provide you an existing syntax, probably cleaner than a hand-made solution as others have already about problems and solutions
LESS is a client-side solution that will load your .less file and a less.js file that will parse the former and provide you variables in CSS whatever your server is. It can also work server-side with node.js
CSS being dynamically modified while your page is displayed?
For 2D there are jquery-animate-enhanced from Ben Barnett, 2d-transform or CSS3 rotate are pitched the other way around (they use CSS3 where possible and where there are no such functions, they fallback to existing jQuery .animate() and IE matrix filter) but that's it.
You could create a plugin for jQuery that would manage with a few parameters what you want to achieve with 3D Transformation and avoid the hassle of modifying long and complex CSS rules in the DOM
CSS only: you could use -moz-calc [1][2] that works only in Firefox 4.0 with -webkit-transform that works only in ... OK nevermind :-)
尝试这样的事情:
Try something like this:
一个“厚重”的解决方案?
在您选择的粒度内创建高度变换,例如 0-99、100-199、200-299 等。每个变换都有一个唯一的动画名称标识符,例如:
然后创建匹配的 css 类:
然后使用 javascript 决定您所在的块并将适当的类分配给周围的元素。
如果粒度不是太细的话可能还可以!
A 'Chunky' solution?
Create transforms for heights within your chosen granularity, say 0-99, 100-199, 200-299, etc. Each with a unique animation name identifier like:
then create matching css classes:
then with javascript decide which chunk you're in and assign the appropriate class to the surrounding element.
Might be ok if the granularity doesn't get too fine!
我使用了 warmanp 的解决方案并且它有效,但进行了一些非常重要的调整。
为了更新 CSS 过渡,我必须停止动画,然后重新启动,而不是使用之前定义的值。为此,我在元素上放置了一个“active”类,并使 CSS 仅将过渡应用于具有该类的元素
然后我只需关闭“active”类,等待它应用DOM(因此是 setTimeout),然后重新应用它。
效果很好!现在我可以动态改变文本移动的距离!
I used warmanp's solution and it worked, but with a bit of very important tweaking.
In order to get the CSS transition to update, instead of using the previously-defined values, I had to make the animation stop, then restart. To do this, I placed a class of "active" on the elements, and made the CSS only apply the transition to the elements that had that class
Then I just had to toggle the "active" class off, wait for it to apply in the DOM (hence the setTimeout), then reapply it.
And that works great! Now I can dynamically change the distance the text moves!