简体   繁体   English

我如何使用 JS 在 HTML/CSS 中完成 1° 和 359° 之间的旋转转换

[英]How can i accomplish a rotation-transition between 1° and 359° in HTML/CSS with JS

I'm reading rotation data from a sensor and sending it to a webpage where i have a 2D-Model, which rotates according to the data.我正在从传感器读取旋转数据并将其发送到我有 2D 模型的网页,该模型根据数据进行旋转。 I've added a css-transition to smooth out the otherwise shaky movement.我添加了一个 css-transition 来平滑否则不稳定的运动。

The problem is that if the rotation data goes from eg 1° rotation to 359° the transition makes the object rotate one round the opposite way to achieve 359° instead of taking the shortest way.问题是,如果旋转数据从例如 1° 旋转到 359°,过渡会使对象以相反的方式旋转一圈以达到 359°,而不是采用最短的方式。 How can i change that?我怎样才能改变它?

 document.getElementById('button1').onclick = function() { var div = document.getElementById('object'), deg = 359; div.style.webkitTransform = 'rotate(' + deg + 'deg)'; div.style.mozTransform = 'rotate(' + deg + 'deg)'; div.style.msTransform = 'rotate(' + deg + 'deg)'; div.style.oTransform = 'rotate(' + deg + 'deg)'; div.style.transform = 'rotate(' + deg + 'deg)'; } document.getElementById('button2').onclick = function() { var div = document.getElementById('object'), deg = 1; div.style.webkitTransform = 'rotate(' + deg + 'deg)'; div.style.mozTransform = 'rotate(' + deg + 'deg)'; div.style.msTransform = 'rotate(' + deg + 'deg)'; div.style.oTransform = 'rotate(' + deg + 'deg)'; div.style.transform = 'rotate(' + deg + 'deg)'; }
 #object { -webkit-transition: all 0.5s linear; -moz-transition: all 0.5s linear; -o-transition: all 0.5s linear; transition: all 0.5s linear; text-align: center; color: white; height: 100px; width: 80px; margin: 30px; background: green; }
 <button id="button1">Go to 359&#176;</button> <button id="button2">Go to 1&#176;</button> <br/> <div id="object">UP</div>

Rotating to -1 will get you there.旋转到 -1 会让你到达那里。

 document.getElementById('button1').onclick = function() { var div = document.getElementById('object'), deg = -1; div.style.webkitTransform = 'rotate(' + deg + 'deg)'; div.style.mozTransform = 'rotate(' + deg + 'deg)'; div.style.msTransform = 'rotate(' + deg + 'deg)'; div.style.oTransform = 'rotate(' + deg + 'deg)'; div.style.transform = 'rotate(' + deg + 'deg)'; } document.getElementById('button2').onclick = function() { var div = document.getElementById('object'), deg = 1; div.style.webkitTransform = 'rotate(' + deg + 'deg)'; div.style.mozTransform = 'rotate(' + deg + 'deg)'; div.style.msTransform = 'rotate(' + deg + 'deg)'; div.style.oTransform = 'rotate(' + deg + 'deg)'; div.style.transform = 'rotate(' + deg + 'deg)'; }
 #object { -webkit-transition: all 0.5s linear; -moz-transition: all 0.5s linear; -o-transition: all 0.5s linear; transition: all 0.5s linear; text-align: center; color: white; height: 100px; width: 80px; margin: 30px; background: green; }
 <button id="button1">Go to 359&#176;</button> <button id="button2">Go to 1&#176;</button> <br/> <div id="object">UP</div>

So just use some smart calculations to know if you should pass a possitive or negative number...所以只需使用一些智能计算来知道您是否应该传递正数或负数......

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

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