我一直致力于这个涉及纺车的代码笔项目。我想知道如何检测指针指向哪个段。下面是javascript。我想我必须将360度除以我所拥有的段数。提前谢谢,抱歉我的英语不好。我来自阿根廷。
http://codepen.io/AndreCortellini/pen/vERwmL
//set default degree (360*5)
var degree = 1800;
//number of clicks = 0
var clicks = 0;
$(document).ready(function(){
/*WHEEL SPIN FUNCTION*/
$('#spin').click(function(){
//add 1 every click
clicks ++;
/*multiply the degree by number of clicks
generate random number between 1 - 360,
then add to the new degree*/
var newDegree = degree*clicks;
var extraDegree = Math.floor(Math.random() * (360 - 1 + 1)) + 1;
totalDegree = newDegree+extraDegree;
/*let's make the spin btn to tilt every
time the edge of the section hits
the indicator*/
$('#wheel .sec').each(function(){
var t = $(this);
var noY = 0;
var c = 0;
var n = 700;
var interval = setInterval(function () {
c++;
if (c === n) {
clearInterval(interval);
}
var aoY = t.offset().top;
$("#txt").html(aoY);
console.log(aoY);
/*23.7 is the minumum offset number that
each section can get, in a 30 angle degree.
So, if the offset reaches 23.7, then we know
that it has a 30 degree angle and therefore,
exactly aligned with the spin btn*/
if(aoY < 23.89){
console.log('<<<<<<<<');
$('#spin').addClass('spin');
setTimeout(function () {
$('#spin').removeClass('spin');
}, 100);
}
}, 10);
$('#inner-wheel').css({
'transform' : 'rotate(' + totalDegree + 'deg)'
});
noY = t.offset().top;
});
});
});//DOCUMENT READY
答案 0 :(得分:0)
我们需要先找[0..360]
度的学位,所以我们计算(totalDegree % 360)
。
然后我们看到[31..90]
代表橙色,[91..150]
代表黄色,[151..210]
代表深蓝色等等......
换句话说,
[31..90] => 1, orange
[91..150] => 2, yellow
[151..210] => 3, dark blue
[211..270] => 4, blue
[271..330] => 5, cyan
[331..30] => 0, red
这让我想起了划分和舍入:(deg / 60)
并将其四舍五入为整数。例如:
31 / 60 = 0.516 ~ 1
32 / 60 = 0.533 ~ 1
...
45 / 60 = 0.750 ~ 1
...
88 / 60 = 1.466 ~ 1
89 / 60 = 1.483 ~ 1
90 / 60 = 1.500 ~ 2
91 / 60 = 1.516 ~ 2
...
所以,正如你所看到的,函数roundToInteger((deg / 60))
完全符合我们的目的!
就JavaScript而言:
var sector = ((totalDegree % 360) / 60).toFixed(0);
$("#txt").html(sector);