从头开始使用鼠标和 jquery 创建一个滑块

发布于 2024-11-04 09:38:03 字数 300 浏览 0 评论 0原文

我正在尝试创建一个类似于以下内容的滑块:

http://jqueryui.com/demos/slider/

基本上这个想法与演示中的基本滑块非常相似,不同之处在于您用鼠标抓住并滑动的手柄中的数字会在您到达某些点时发生变化。我想我可以弄清楚数字部分,但我完全不知道如何开始。

我想用 jquery 从头开始​​做...所以没有插件。如果有人知道任何教程,或者我可以开始的地方,那就太棒了。

谢谢

I'm trying to create a slider similar to:

http://jqueryui.com/demos/slider/

Basically the idea is something very similar to the basic slider in the demo, except that in the handle that you grab with the mouse to slide will be numbers that change when you get to certain points. I think I can figure out the numbers part, but I'm completely stuck on how to start.

I want to do it from scratch with jquery... so no plugins. If anyone knows of any tutorials, or somewhere I could start that would be awesome.

Thanks

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

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

发布评论

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

评论(2

够钟 2024-11-11 09:38:03

视情况而定。如果您想获取知识,我建议您查看 jQuery UI 源代码 ;)

如果你想这么做是因为 jQuery UI 太“重”,那么仅供参考,你可以 自定义您下载/使用的部分。

编辑

尝试:https://developer.mozilla.org/en/DragDrop/Drag_and_Drop

Depends. If you want to do it for the knowledge, I'd recommend having a look at the jQuery UI source code ;)

If you want to do it because jQuery UI is too "heavy" then FYI, you can customise which parts of it you download/use.

EDIT

Try: https://developer.mozilla.org/en/DragDrop/Drag_and_Drop

奶气 2024-11-11 09:38:03

这就是我所拥有的,并且有效。

//sets the current position and offset variables
    var currentPosition;
    var offset;
    var rightOffset

    //looks for the mousedown event on the slider
    $("#slider").mousedown(function(e){
        //sets the offset = to the mouse coordinate of the page - the offset of the slider
        offset = e.pageX - this.offsetLeft;
        console.log("offset: " + offset);
        $(document).bind('mousemove', mmHandler);
    }); 

    var mmHandler = function (e) {

            currentPosition = e.pageX - offset;

            //takes the mouse current position (minues the offset) and sets an absolute position
            $('#slider').css('left', currentPosition + "px");
            console.log("CURRENT POSITION: " + currentPosition);

            //checks to make sure it's within the box
            if(currentPosition <= 0){
                $('#slider').css('left', 0 + "px");
            }else if(currentPosition >= 380){
                $('#slider').css('left', 400-20 + "px");    
            }

            $("#slider").text($("#slider").css('left'));

        };


    $(document).mouseup(function(e) {
      $(document).unbind('mousemove', mmHandler);
    });

This is what I have, and it works.

//sets the current position and offset variables
    var currentPosition;
    var offset;
    var rightOffset

    //looks for the mousedown event on the slider
    $("#slider").mousedown(function(e){
        //sets the offset = to the mouse coordinate of the page - the offset of the slider
        offset = e.pageX - this.offsetLeft;
        console.log("offset: " + offset);
        $(document).bind('mousemove', mmHandler);
    }); 

    var mmHandler = function (e) {

            currentPosition = e.pageX - offset;

            //takes the mouse current position (minues the offset) and sets an absolute position
            $('#slider').css('left', currentPosition + "px");
            console.log("CURRENT POSITION: " + currentPosition);

            //checks to make sure it's within the box
            if(currentPosition <= 0){
                $('#slider').css('left', 0 + "px");
            }else if(currentPosition >= 380){
                $('#slider').css('left', 400-20 + "px");    
            }

            $("#slider").text($("#slider").css('left'));

        };


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