jQuery 旋转/变换
我想使用此功能旋转然后停止在特定点或角度。现在该元素只是旋转而不停止。这是代码:
<script type="text/javascript">
$(function() {
var $elie = $("#bkgimg");
rotate(0);
function rotate(degree) {
// For webkit browsers: e.g. Chrome
$elie.css({ WebkitTransform: 'rotate(' + degree + 'deg)'});
// For Mozilla browser: e.g. Firefox
$elie.css({ '-moz-transform': 'rotate(' + degree + 'deg)'});
// Animate rotation with a recursive call
setTimeout(function() { rotate(++degree); },65);
}
});
</script>
感谢您的帮助
I'd like to use this function to rotate then stop at a particular point or degree. Right now the element just rotates without stopping. Here's the code:
<script type="text/javascript">
$(function() {
var $elie = $("#bkgimg");
rotate(0);
function rotate(degree) {
// For webkit browsers: e.g. Chrome
$elie.css({ WebkitTransform: 'rotate(' + degree + 'deg)'});
// For Mozilla browser: e.g. Firefox
$elie.css({ '-moz-transform': 'rotate(' + degree + 'deg)'});
// Animate rotation with a recursive call
setTimeout(function() { rotate(++degree); },65);
}
});
</script>
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需删除一次将其旋转一度的行并永远调用该脚本即可。
然后将所需的值传递到函数中...在此示例中,
45
表示 45 度。将
.css()
更改为.animate()
以便 使用 jQuery 实现旋转动画。我们还需要添加动画持续时间,5000 表示 5 秒。并更新您的原始函数以删除一些冗余并支持更多浏览器...编辑:上面的标准 jQuery CSS 动画代码不起作用,因为显然 jQuery
.animate()
尚不支持 CSS3transforms
。这个 jQuery 插件应该为旋转设置动画:
http://plugins.jquery.com/project/QTransform
Simply remove the line that rotates it one degree at a time and calls the script forever.
Then pass the desired value into the function... in this example
45
for 45 degrees.Change
.css()
to.animate()
in order to animate the rotation with jQuery. We also need to add a duration for the animation, 5000 for 5 seconds. And updating your original function to remove some redundancy and support more browsers...EDIT: The standard jQuery CSS animation code above is not working because apparently, jQuery
.animate()
does not yet support the CSS3transforms
.This jQuery plugin is supposed to animate the rotation:
http://plugins.jquery.com/project/QTransform
这是因为您在旋转内部有一个递归函数。它再次调用自己:
将其取出,它就不会继续递归运行。
我还建议只使用这个函数:
它更干净,并且适用于大多数浏览器。
It's because you have a recursive function inside of rotate. It's calling itself again:
Take that out and it won't keep on running recursively.
I would also suggest just using this function instead:
It's much cleaner and will work for the most amount of browsers.
为什么不直接使用点击时的toggleClass?
js:
css:
您也可以将其添加到 css: 中,
这将添加动画。
PS...
回答你原来的问题:
你说它旋转但永远不会停止。使用 set timeout 时,您需要确保有一个不会调用 settimeout 的条件,否则它将永远运行。所以对于你的代码:
Why not just use, toggleClass on click?
js:
css:
you can also add this to the css:
which will add the animation.
PS...
to answer your original question:
you said that it rotates but never stops. When using set timeout you need to make sure you have a condition that will not call settimeout or else it will run forever. So for your code:
我想出了某种解决问题的方法。涉及到jquery和css。这与切换类似,但不是切换元素的显示,而是在交替单击时更改其属性。单击 div 后,带有标签的元素将旋转 180 度,再次单击时,带有标签的元素将返回到其原始位置。如果你想改变动画持续时间只需改变transition-duration属性。
CSS
jQuery
});
I came up with some kind of solution to the problem. It involves jquery and css. This works like toggle but instead of toggling the display of elements it just changes its properties upon alternate clicks. Upon clicking the div it rotates the element with tag 180 degrees and when you click it again the element with tag returns to its original position. If you want to change the animation duration just change transition-duration property.
CSS
jQuery
});
和
clearTimeout
来阻止我将其与 AJAX 一起使用
and
clearTimeout
to stopI use this with AJAX