jQuery 圆形滑块?

发布于 2024-12-02 19:28:31 字数 3550 浏览 0 评论 0 原文

我的网站上有此滑块。我想将滑块移动到 360 度。我如何更改以下脚本来执行此操作?

$(document).ready(function() {
    /*Slider */
    $('.slider-input').each(function() {
        var currVal = $(this).val();
        if(currVal < 0){
            currVal = 0;
        }
        $(this).parent().children('.slider-content').slider({
            'animate': true,
            'min': -1,
            'max': 201,
            'value' : 201,
            'orientation' : 'vertical',
            'stop': function(e, ui){
                //$(this).prev('.slider-input').val(ui.value); //Set actual input field val, done during slide instead

                //pop handle back to top if we went out of bounds at bottom
                /*
                if ( ui.value == -1 ) {
                    ui.value = 201;
                    $(this).children('.ui-slider-handle').css('bottom','100%');
                }
                */
            },
            'slide': function(e, ui){
                var percentLeft;
                var submitValue;
                var Y = ui.value - 100; //Find center of Circle (We're using a max value and height of 200)
                var R = 100; //Circle's radius
                var skip = false;

                $(this).children('.ui-slider-handle').attr('href',' UI.val = ' + ui.value);

                //Show default/disabled/out of bounds state
                if ( ui.value > 0 && ui.value < 201 ) { //if in the valid slide rang
                    $(this).children('.ui-slider-handle').addClass('is-active');
                }
                else {
                    $(this).children('.ui-slider-handle').removeClass('is-active');
                }

                //Calculate slider's path on circle, put it there, by setting background-position
                if ( ui.value >= 0 && ui.value <= 200 ) { //if in valid range, these are one inside the min and max
                    var X = Math.sqrt((R*R) - (Y*Y)); //X^2 + Y^2 = R^2. Find X.
                    if ( X == 'NaN' ) {
                        percentLeft = 0;
                    }
                    else {
                        percentLeft = X;
                    }
                }
                else if ( ui.value == -1 || ui.value == 201 ) {
                    percentLeft = 0;
                    skip = true;
                }
                else {
                    percentLeft = 0;
                }

                //Move handle
                if ( percentLeft > 100 ) { percentLeft = 100; }
                $(this).children('.ui-slider-handle').css('background-position',percentLeft +'% 100%'); //set css sprite

                //Figure out and set input value
                if ( skip == true ) {
                    submitValue = 'keine Seite';
                    $(this).children('.ui-slider-handle').css('background-position',percentLeft +'% 0%'); //reset css sprite
                }
                else {
                    submitValue = Math.round(ui.value / 2); //Clamp input value to range 0 - 100
                }
                $('#display-only input').val(submitValue); //display selected value, demo only
                $('#slider-display').text(submitValue); //display selected value, demo only
                $(this).prev('.slider-input').val(ui.value); //Set actual input field val. jQuery UI hid it for us, but it will be submitted.
            }
        });
    });
});

滑块的图像也必须旋转 360 度。

I have this slider on my website. I want to move the slider to 360 degrees. How can I change the following script to do so?

$(document).ready(function() {
    /*Slider */
    $('.slider-input').each(function() {
        var currVal = $(this).val();
        if(currVal < 0){
            currVal = 0;
        }
        $(this).parent().children('.slider-content').slider({
            'animate': true,
            'min': -1,
            'max': 201,
            'value' : 201,
            'orientation' : 'vertical',
            'stop': function(e, ui){
                //$(this).prev('.slider-input').val(ui.value); //Set actual input field val, done during slide instead

                //pop handle back to top if we went out of bounds at bottom
                /*
                if ( ui.value == -1 ) {
                    ui.value = 201;
                    $(this).children('.ui-slider-handle').css('bottom','100%');
                }
                */
            },
            'slide': function(e, ui){
                var percentLeft;
                var submitValue;
                var Y = ui.value - 100; //Find center of Circle (We're using a max value and height of 200)
                var R = 100; //Circle's radius
                var skip = false;

                $(this).children('.ui-slider-handle').attr('href',' UI.val = ' + ui.value);

                //Show default/disabled/out of bounds state
                if ( ui.value > 0 && ui.value < 201 ) { //if in the valid slide rang
                    $(this).children('.ui-slider-handle').addClass('is-active');
                }
                else {
                    $(this).children('.ui-slider-handle').removeClass('is-active');
                }

                //Calculate slider's path on circle, put it there, by setting background-position
                if ( ui.value >= 0 && ui.value <= 200 ) { //if in valid range, these are one inside the min and max
                    var X = Math.sqrt((R*R) - (Y*Y)); //X^2 + Y^2 = R^2. Find X.
                    if ( X == 'NaN' ) {
                        percentLeft = 0;
                    }
                    else {
                        percentLeft = X;
                    }
                }
                else if ( ui.value == -1 || ui.value == 201 ) {
                    percentLeft = 0;
                    skip = true;
                }
                else {
                    percentLeft = 0;
                }

                //Move handle
                if ( percentLeft > 100 ) { percentLeft = 100; }
                $(this).children('.ui-slider-handle').css('background-position',percentLeft +'% 100%'); //set css sprite

                //Figure out and set input value
                if ( skip == true ) {
                    submitValue = 'keine Seite';
                    $(this).children('.ui-slider-handle').css('background-position',percentLeft +'% 0%'); //reset css sprite
                }
                else {
                    submitValue = Math.round(ui.value / 2); //Clamp input value to range 0 - 100
                }
                $('#display-only input').val(submitValue); //display selected value, demo only
                $('#slider-display').text(submitValue); //display selected value, demo only
                $(this).prev('.slider-input').val(ui.value); //Set actual input field val. jQuery UI hid it for us, but it will be submitted.
            }
        });
    });
});

The image for the slider must also rotate through 360 degrees.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卖梦商人 2024-12-09 19:28:31

要计算圆,您可以使用以下公式。

precenttop = (-(cos(ui.value/(100/pi))-1))*50)
percentleft = (sin(ui.value/(100/pi))*50)+50

然后它应该绕圆旋转.. 201 fo ui 的值将与 1 处于相同的位置,-1 与 199 相同。

上面的解释是:

cos(ui.value/(100/pi)) <-- ui value ranges from 0 to 200 but the cosine 
                           period is 2pi so devide the ui value so its 
                           somewhere between 0 and 2pi
-1                     <-- result ranges from 1 to -1 and i prefer 0 to 2 so 
                           minus 1 makes it 0 to -2 therefore
-()                    <-- we invert the whole... now it 0 to 2
*50                    <-- since you are using percent 0*50 = 0 and 2*50 = 100
                           ergo it now bounces between 0 and 100.

对于 sin,它几乎是相同的,只是这里我们确实希望结果介于 -1 和 1 之间。我们只需乘以 50(-50 到 50)并加上 50(0 - 100)。

现在 ui.value 等于 0%top 的结果将是 0,percentleft 将是 50。因此

ui.value = 100 50  150 200
top =      100 50  50  0
left =     50  100 0   50

:circle。

To calculate a circle you can use the following formula.

precenttop = (-(cos(ui.value/(100/pi))-1))*50)
percentleft = (sin(ui.value/(100/pi))*50)+50

Then it should rotate around the circle.. a value of 201 fo ui will be in the same position as 1 and -1 is the same as 199.

The explanation of the above is:

cos(ui.value/(100/pi)) <-- ui value ranges from 0 to 200 but the cosine 
                           period is 2pi so devide the ui value so its 
                           somewhere between 0 and 2pi
-1                     <-- result ranges from 1 to -1 and i prefer 0 to 2 so 
                           minus 1 makes it 0 to -2 therefore
-()                    <-- we invert the whole... now it 0 to 2
*50                    <-- since you are using percent 0*50 = 0 and 2*50 = 100
                           ergo it now bounces between 0 and 100.

For the sin, it is almost the same except here we do want the result to be between -1 and 1. We just multiply by 50 (-50 to 50) and add 50 (0 - 100).

Now the result for ui.value equal to 0 percenttop will be 0 and percentleft will be 50. And at

ui.value = 100 50  150 200
top =      100 50  50  0
left =     50  100 0   50

Ergo: circle.

趴在窗边数星星i 2024-12-09 19:28:31

我认为 jQuery roundSlider 插件适合这个需求。

有关更多详细信息,请查看 演示文档页面。

I think jQuery roundSlider plugin is suitable for this requirement.

For more details check the demos and documentation page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文