Reputation: 15
how to make a button with javascript when the user presses it to rotate an object 180 moires? (for my site)
<input type="button" value="button">
I have no idea how to do it.
Upvotes: 0
Views: 9964
Reputation: 1541
I would suggest using the Web Animations API (chrome, firefox, and opera all partially support it):
Simplest Reference on how to use.
For your code this would do:
element.animate([{transform: "rotate(0deg)"}, {transform: "rotate(100deg)"}], {duration: 2000});
Upvotes: 0
Reputation: 450
You can use the CSS3 rotate property how this:
HTML:
<input type="button" value="button" id="button">
CSS:
.rotate-180 {
-moz-transform: rotate(180deg);
-webkit-transform: rotate(180deg);
-o-transform: rotate(180deg);
-ms-transform: rotate(180deg);
}
JS:
$("#button").click(function(){
$(this).toggleClass("rotate-180");
})
DEMO: http://jsfiddle.net/1x84r9p6/
Upvotes: 0
Reputation: 197
Simply css3 rotate and onclick event: https://jsfiddle.net/xxarLyc6/
One class and default rotate:
div{
-ms-transform: rotate(0deg); /* IE 9 */
-webkit-transform: rotate(0deg); /* Chrome, Safari, Opera */
transform: rotate(0deg);
}
.rotated{
-ms-transform: rotate(180deg); /* IE 9 */
-webkit-transform: rotate(180deg); /* Chrome, Safari, Opera */
transform: rotate(180deg);
}
Javascript onclick event for button:
var button=document.getElementById("button");
button.onclick=function(){
document.getElementById("torotate").className = "rotated";
}
Upvotes: 4