简体   繁体   English

用鼠标旋转轮盘

[英]Rotate a Roulette Wheel with the Mouse

I'm looking at this example ( jsfiddle ). 我正在看这个示例( jsfiddle )。 It's almost what I need, but I need the user to "grab" the roulette with the mouse, then spin it, like you would do with a real one with your hand. 这几乎是我所需要的,但是我需要用户用鼠标“抓住”轮盘,然后旋转它,就像用一只手来制作轮盘一样。

Like, you click and hold on the wheel, it "sticks" to your mouse, then you move your mouse to left or right, and release the button, and the wheel starts to spin until it stops. 就像,您单击并按住滚轮,它“粘在”鼠标上,然后向左或向右移动鼠标,然后释放按钮,滚轮开始旋转直到停止。

Another question is, even if the user is doing that, can I choose a predetermined order to the wheel stops? 另一个问题是,即使用户正在这样做,我也可以选择预定的轮挡顺序吗?

This is the jsFiddle: 这是jsFiddle:

$(function(){

            var overWheel = false;
            var mouseDown = false;
            var lastMousePos = 0;

            $('.wheel').on('mouseover', function(){
                overWheel = true;
            }).on('mouseout', function(){
                overWheel = false;
            });


            $(document).on('mousedown', function(e){
                if(overWheel){
                    lastMousePos = e.offsetY;
                    mouseDown = true;
                }
            }).on('mouseup', function(){
                mouseDown = false;
            });


            $(document).on('mousemove', function(e){
                if(overWheel && mouseDown){
                    handleWheel(e);
                }
            });


            function handleWheel(e) {

                var yPos = e.offsetY;
                var direction = 0;

                var deg = getRotationDegrees($('.wheel'));


                if(yPos < lastMousePos){ // mouse is going up, move against the clock
                    console.log(yPos);
                    direction = -2;

                } else { //mouse is going down, move with the clock

                    direction = 2;

                }

                $('.wheel').css({'-webkit-transform': 'rotate(' + (deg + (direction)) + 'deg)'});
            }

            function getRotationDegrees(obj){
                var matrix = obj.css("-webkit-transform");
                if(matrix !== 'none') {
                    var values = matrix.split('(')[1].split(')')[0].split(',');
                    var a = values[0];
                    var b = values[1];
                    var angle = Math.round(Math.atan2(b, a) * (180/Math.PI));
                } else { var angle = 0; }
                return angle;
            }

        });​

I've managed to work. 我已经设法工作了。 I Used the jQuery Rotate library. 我使用了jQuery Rotate库。

Thanks! 谢谢!

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

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