我过去做过这样的事情。
http://jsfiddle.net/gTDdp/16/
(通过一些调整应该可以创建一个旋钮。)我为移动设备添加了触摸事件
var dragging = false
$(function() {
init();
var target = $('#target')
var offset_x = -256;
var offset_y = 356;
var angle = 0;
var startAngle;
var slices = 8;
var sliceAngle = 360 / slices;
target.mousedown(function(e) {
var mouse_x = e.pageX;
var mouse_y = e.pageY;
var radians = Math.atan2(mouse_x - offset_x, mouse_y - offset_y);
dragging = true
startAngle = ((radians * (180 / Math.PI) * -1) + 90) - angle;
})
$(document).mouseup(function() {
dragging = false;
var slice = (angle + (sliceAngle/2)) / sliceAngle;
if(slice < 0){
slice = 12 + slice;
}else if(slice > 12){
slice = 12 - slice;
}
// Move circle in perfect position
angle = (Math.floor(slice) * (360 / slices));
target.css('-moz-transform', 'rotate(' + angle + 'deg)');
target.css('-moz-transform-origin', '50% 50%');
target.css('-webkit-transform', 'rotate(' + angle + 'deg)');
target.css('-webkit-transform-origin', '50%, 50%');
target.css('-o-transform', 'rotate(' + angle + 'deg)');
target.css('-o-transform-origin', '50% 50%');
target.css('-ms-transform', 'rotate(' + angle + 'deg)');
target.css('-ms-transform-origin', '50% 50%');
alert(Math.floor(slice));
})
$(document).mousemove(function(e) {
if (dragging) {
var mouse_x = e.pageX;
var mouse_y = e.pageY;
var radians = Math.atan2(mouse_x - offset_x, mouse_y - offset_y);
var degree = (radians * (180 / Math.PI) * -1) + 90;
angle = degree - startAngle;
if(angle < 0){
angle = 360 + angle;
}else if(angle > 360){
angle = 360 - angle;
}
target.css('-moz-transform', 'rotate(' + angle + 'deg)');
target.css('-moz-transform-origin', '50% 50%');
target.css('-webkit-transform', 'rotate(' + angle + 'deg)');
target.css('-webkit-transform-origin', '50%, 50%');
target.css('-o-transform', 'rotate(' + angle + 'deg)');
target.css('-o-transform-origin', '50% 50%');
target.css('-ms-transform', 'rotate(' + angle + 'deg)');
target.css('-ms-transform-origin', '50% 50%');
}
})
})
function touchHandler(event)
{
var touches = event.changedTouches,
first = touches[0],
type = "";
switch(event.type)
{
case "touchstart": type = "mousedown"; break;
case "touchmove": type="mousemove"; break;
case "touchend": type="mouseup"; break;
default: return;
}
var simulatedEvent = document.createEvent("MouseEvent");
simulatedEvent.initMouseEvent(type, true, true, window, 1,
first.screenX, first.screenY,
first.clientX, first.clientY, false,
false, false, false, 0/*left*/, null);
first.target.dispatchEvent(simulatedEvent);
event.preventDefault();
}
function init()
{
document.addEventListener("touchstart", touchHandler, true);
document.addEventListener("touchmove", touchHandler, true);
document.addEventListener("touchend", touchHandler, true);
document.addEventListener("touchcancel", touchHandler, true);
}