admin管理员组文章数量:1135112
I want to change the -webkit-transform: rotate()
property using JavaScript dynamically, but the commonly used setAttribute
is not working:
img.setAttribute('-webkit-transform', 'rotate(60deg)');
The .style
is not working either...
How can I set this dynamically in JavaScript?
I want to change the -webkit-transform: rotate()
property using JavaScript dynamically, but the commonly used setAttribute
is not working:
img.setAttribute('-webkit-transform', 'rotate(60deg)');
The .style
is not working either...
How can I set this dynamically in JavaScript?
Share Improve this question edited Apr 30, 2018 at 21:48 Tot Zam 8,74611 gold badges54 silver badges79 bronze badges asked Apr 2, 2009 at 9:01 rymnrymn 1,4992 gold badges10 silver badges14 bronze badges 1- wont you set style attribute instead of just this one – Muhammad Umer Commented Jul 12, 2013 at 6:06
5 Answers
Reset to default 216The JavaScript style names are WebkitTransformOrigin
and WebkitTransform
element.style.webkitTransform = "rotate(-2deg)";
Check the DOM extension reference for WebKit here.
Here are the JavaScript notations for most common vendors:
webkitProperty
MozProperty
msProperty
OProperty
property
I reset inline transform styles like:
element.style.webkitTransform = "";
element.style.MozTransform = "";
element.style.msTransform = "";
element.style.OTransform = "";
element.style.transform = "";
And like this using jQuery:
$(element).css({
"webkitTransform":"",
"MozTransform":"",
"msTransform":"",
"OTransform":"",
"transform":""
});
See blog post Coding Vendor Prefixes with JavaScript (2012-03-21).
Try using
img.style.webkitTransform = "rotate(60deg)"
If you want to do it via setAttribute
you would change the style
attribute like so:
element.setAttribute('style','transform:rotate(90deg); -webkit-transform: rotate(90deg)') //etc
This would be helpful if you want to reset all other inline style and only set your needed style properties' values again, BUT in most cases you may not want that. That's why everybody advised to use this:
element.style.transform = 'rotate(90deg)';
element.style.webkitTransform = 'rotate(90deg)';
The above is equivalent to
element.style['transform'] = 'rotate(90deg)';
element.style['-webkit-transform'] = 'rotate(90deg)';
To animate your 3D object, use the code:
<script>
$(document).ready(function(){
var x = 100;
var y = 0;
setInterval(function(){
x += 1;
y += 1;
var element = document.getElementById('cube');
element.style.webkitTransform = "translateZ(-100px) rotateY("+x+"deg) rotateX("+y+"deg)"; //for safari and chrome
element.style.MozTransform = "translateZ(-100px) rotateY("+x+"deg) rotateX("+y+"deg)"; //for firefox
},50);
//for other browsers use: "msTransform", "OTransform", "transform"
});
</script>
本文标签: How to set the style webkittransform dynamically using JavaScriptStack Overflow
版权声明:本文标题:How to set the style -webkit-transform dynamically using JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736804195a1953618.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论