简体   繁体   English

计算旋转度时出现问题

[英]Problems calculating rotation degrees

Got some math issues in my code that i can't figure out. 我的代码中出现了一些我不知道的数学问题。

When you press on one of the red circles it should take the closest path (left or right) and rotate down to the bottom, it's kinda working, but there are a few issues with it. 当您按红色圆圈之一时,它应该走最近的路径(向左或向右)并向下旋转到底部,这有点奏效,但是存在一些问题。

eg If you start by pressing '8' and then '3', the container is only rotating 45 degrees in the wrong direction. 例如,如果先按“ 8”再按“ 3”开始,则容器仅在错误的方向上旋转了45度。

And the second issue is that the numbers are spinning around when the wheel is rotating. 第二个问题是,当车轮旋转时,数字在旋转。

Here is a jsfiddle to the code: https://jsfiddle.net/she4x2w6/10/ 这是代码的jsfiddle: https ://jsfiddle.net/she4x2w6/10/

$('.item').click(function() {
var currentId = $('#container').data('id');
var nextId = $(this).data('id');
var currentRotation = (360 / items.length) * currentId - (360 / items.length);
var nextRotation = (360 / items.length) * nextId - (360 / items.length);
var deg;

if (currentRotation - nextRotation > 180 || nextRotation - currentRotation > 180) {
    deg = nextRotation > 180 ? 360 - nextRotation : nextRotation - 360;
} else {
    deg = -Math.abs(nextRotation);
}
var itemDeg = nextRotation <= 180 ? nextRotation : -Math.abs(360 - nextRotation);

$('#container').css({
    transition: 'transform 0.6s',
    transform: 'rotate(' + deg + 'deg)'
})

$('.item').css({
    transition: 'transform 0.6s',
    transform: 'rotate(' + itemDeg + 'deg)'
})

CSS rotate() function rotates an element to given angle while you are trying to rotate your element by a given angle. 当您尝试将元素旋转给定角度时,CSS rotation()函数会将元素旋转到给定角度。

rotate() -CSS | rotation()-CSS | MDN MDN

I had to change some of your approach to get it working: 我必须更改一些方法才能使其正常工作:

 var radius = 100; // adjust to move out items in and out var items = $('.item'), container = $('#container'), width = container.width(), height = container.height(); var angle = 0, step = (2 * Math.PI) / items.length, angle = Math.PI/2; items.each(function() { var x = Math.round(width / 2 + radius * Math.cos(angle) - $(this).width() / 2); var y = Math.round(height / 2 + radius * Math.sin(angle) - $(this).height() / 2); $(this).css({ left: x + 'px', top: y + 'px' }); angle += step; }); $('#container').data('deg', 0); $('.item').click(function() { var currentId = $('#container').data('id'); var nextId = $(this).data('id'); var currentDeg =$('#container').data('deg'); var deg; var degg = ((nextId+items.length-1)%items.length)*(360 / items.length); if (degg>=180) { deg = 360-degg; } else { deg = -degg; } var t = (currentDeg - deg) % 360; if (t<0) { t = 360+t; } if (t<180) { deg = currentDeg-t; } else { deg = currentDeg+360-t; } var itemDeg = -deg; var time = Math.abs(deg-currentDeg)/100; $('#container').css({ transition: 'transform ' + time + 's', transform: 'rotate(' + deg + 'deg)' }) $('.item').css({ transition: 'transform ' + time + 's', transform: 'rotate(' + itemDeg + 'deg)' }) $('#container').data('id', nextId).data('deg', deg); }); 
 body { padding: 2em; } #container { width: 200px; height: 200px; margin: 10px auto; border: 1px solid #000; position: relative; border-radius: 50%; } .item { width: 30px; height: 30px; line-height: 30px; text-align: center; border-radius: 50%; position: absolute; background: #f00; } .item p { margin: 0; } .active .item-content { opacity: 1 !important; transition: opacity ease 0.6s; } .item .item-content { opacity: 0; transition: opacity ease 0.3s; margin: auto; position: absolute; width: 230px; transform: translate(-50%); left: 50%; pointer-events: none; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container" data-id="1"> <div data-id="1" class="item">1</div> <div data-id="2" class="item">2</div> <div data-id="3" class="item">3</div> <div data-id="4" class="item">4</div> <div data-id="5" class="item">5</div> <div data-id="6" class="item">6</div> <div data-id="7" class="item">7</div> <div data-id="8" class="item">8</div> <div data-id="9" class="item">9</div> </div> 

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

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