Javascript 使用 onclick 旋转图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14396614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rotate image with onclick
提问by Art Planteur
I try to make a toggle button where my image (an arrow) will be rotated by 180 degrees each click:
我尝试制作一个切换按钮,每次点击我的图像(箭头)都会旋转 180 度:
<a href='#' onclick='$(\"#divToggle\").slideToggle(\"slow\");'>
<img src='blue_down_arrow.png' onclick='$(this).rotate(180);' /></a>
<div id='divToggle' style='display:none;'>...CONTENT...</div>";
This code toggle my div but the image rotates only for the first click and then stays "up". I am using this : http://code.google.com/p/jqueryrotate/
此代码切换我的 div,但图像仅在第一次单击时旋转,然后保持“向上”。我正在使用这个:http: //code.google.com/p/jqueryrotate/
回答by muthu
回答by ATOzTOA
This is because the value for rotate angle is absolute, not based on the last rotate.
这是因为旋转角度的值是绝对的,而不是基于最后一次旋转。
Working code:
工作代码:
jQuery
jQuery
var rotate_factor = 0;
function rotateMe(e) {
rotate_factor += 1;
var rotate_angle = (180 * rotate_factor) % 360;
$(e).rotate({angle:rotate_angle});
}
HTML
HTML
<img src='blue_down_arrow.png' onclick='rotateMe(this);' /></a>
Update: Shorter code
更新:更短的代码
var rotate_angle = 0;
<img src='blue_down_arrow.png' onclick='rotate_angle = (rotate_angle + 180) % 360; $(this).rotate(rotate_angle);' /></a>
回答by Jason
Click to see CSS3 transition demo
check out this demo. It uses CSS3 for transition + transform rotating
看看这个演示。它使用 CSS3 进行过渡 + 变换旋转
.testRotate{
-moz-transition: transform 1s;
-webkit-transition: transform 1s;
transition: transform 1s;
}
.testRotate:hover{
transform: rotate(45deg);
-moz-transform: rotate(45deg);
-webkit-transform: rotate(45deg);
}
these are vital part of this demo =)
这些是这个演示的重要组成部分 =)
回答by Rachel Gallen
Use CSS3 for the rotation:
使用 CSS3 进行旋转:
- In Mozilla Firefox this will be -moz-transform: rotate(180deg)
- In Webkit based browsers, i.e. Chrome: -webkit-transform: rotate(180deg)
- In Opera: -o-transform: rotate(180deg)
- In IE: -ms-transform: rotate(180deg) (only IE9)
- In pre-IE9: not easily possible, will need the use of Matrix Filter
- 在 Mozilla Firefox 中,这将是 -moz-transform: rotate(180deg)
- 在基于 Webkit 的浏览器中,即 Chrome:-webkit-transform:rotate(180deg)
- 在 Opera 中:-o-transform:rotate(180deg)
- 在 IE 中:-ms-transform:rotate(180deg)(仅 IE9)
- 在 IE9 之前:不容易实现,需要使用矩阵过滤器
Use jQuery rotate pluginto unify all the browser differences.
使用jQuery 旋转插件统一所有浏览器差异。
回答by Jai
try this:
尝试这个:
$('img').click(function(){
$('img').rotate({ angle: 0, bind:{
"click":function(){
$(this).rotate({animateTo:360});
}
}
});
});
回答by Rossitten
This will enable you to perform an additional rotation of each clik
这将使您能够对每个点击执行额外的旋转
var degrees = 0;
$('.img').click(function rotateMe(e) {
degrees += 90;
//$('.img').addClass('rotated'); // for one time rotation
$('.img').css({
'transform': 'rotate(' + degrees + 'deg)',
'-ms-transform': 'rotate(' + degrees + 'deg)',
'-moz-transform': 'rotate(' + degrees + 'deg)',
'-webkit-transform': 'rotate(' + degrees + 'deg)',
'-o-transform': 'rotate(' + degrees + 'deg)'
});

