简体   繁体   English

JAvascript 180到-180旋转

[英]JAvascript 180 to -180 rotate

Right now when the user clicks on this image, it rotates 180 degrees. 现在,当用户点击此图像时,它会旋转180度。
I want to update it to do this: If the user clicks the image, it rotates 180 degrees, but if the user clicks the image again, it rotates back to its original position. 我想更新它来执行此操作:如果用户单击图像,它会旋转180度,但如果用户再次单击图像,它将旋转回原始位置。

Here's my javascript: 这是我的javascript:

var value = 0
$("#row-1").click(function(){
    value +=180;
    $("#image1").rotate({ animateTo:value});
});

HTML: HTML:

<th style="text-align:left;" width="20%" id="row-2">
    Company<br>
    <span class="move_right">Name</span> 
    <img src="/images/sort-arrow-up.png" title="Sort by Company Name" alt="Sort by Company Name" class="sort-right move-left" id="image2" />
</th>

Maintain a separate variable for how much to change the angle and negate it on each click: 保持一个单独的变量,用于改变角度,并在每次点击时取消它:

var value = 0;
var delta = 180;
$("#row-1").click(function(){
    value += delta;
    delta = -delta;
    $("#image1").rotate({ animateTo:value});
});

I would suggest to set your value = -180 at the begening and then every time the user click you do a 我建议在begening设置你的值= -180然后每次用户点击你做

value = value * -1;

This will give : 180, -180, 180, -180 (...) and so on. 这将给出:180,-180,180,-180(...)等等。 It's what you want ? 这是你想要的?

var angle = 180;
$("#row-1").toggle(function(){
    value +=angle ;
    $("#image1").rotate({ animateTo:value});
    angle = angle*(-1);
});

I know this has already been answered, but I wanted to get an answer with a fiddle on here. 我知道这已经得到了解答,但我想在这里找到答案。 There is also a dependency on the jQueryRotate plugin . jQueryRotate插件也有依赖。

Working Demo in Fiddle 小提琴中的工作演示

HTML : HTML

<table>
    <th id="row-1">
        <p>Company</p>
        <span>Name</span> 
        <img id="image1" src="http://i.imgur.com/wO5gsMy.png"
             title="Sort by Company Name" alt="Sort by Company Name"
             width="25" height="25"/>
    </th>
</table>

JavaScript : JavaScript

var value = 0;
$("#row-1").click(function(){
    value = value ? 0: 180
    $("#image1").rotate({ animateTo:value});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM