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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:38:34  来源:igfitidea点击:

Rotate image with onclick

javascriptjquerytoggle

提问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

You may write like this. demo

你可以这样写。演示

jQuery(document).ready(function(){
   var deg_temp =45;
  jQuery("#image1").click(function(){
     deg_temp = deg_temp+30;
  jQuery(this).rotate(deg_temp);
  })

});

回答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

点击查看CSS3过渡演示

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 进行旋转:

  1. In Mozilla Firefox this will be -moz-transform: rotate(180deg)
  2. In Webkit based browsers, i.e. Chrome: -webkit-transform: rotate(180deg)
  3. In Opera: -o-transform: rotate(180deg)
  4. In IE: -ms-transform: rotate(180deg) (only IE9)
  5. In pre-IE9: not easily possible, will need the use of Matrix Filter
  1. 在 Mozilla Firefox 中,这将是 -moz-transform: rotate(180deg)
  2. 在基于 Webkit 的浏览器中,即 Chrome:-webkit-transform:rotate(180deg)
  3. 在 Opera 中:-o-transform:rotate(180deg)
  4. 在 IE 中:-ms-transform:rotate(180deg)(仅 IE9)
  5. 在 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)'
    });

https://jsfiddle.net/Lnckq5om/1/

https://jsfiddle.net/Lnckq5om/1/