简体   繁体   中英

Jquery Draggable - How do I make a dragged element snap in pixel increments

For my application, after an element is dragged and snapped to the droppable zone I need to continue to allows for that dragged item to be moved vertically only. I have already gone ahead and done this, but I also need for that item to only be dragged and snapped to pixel increments. How can this be done?

For example, I have a timeline and I need the elements to snap to 15 minute increments. I need to know if this is possible with the draggable function or do I need to come up with some slider hybrid? I'd rather not use some hybrid approach if possible.

Code Snippet:

$(".activity_link").draggable({
                "snap": ".trek_slider",
                "snapMode": "inner",
                // "snapTolerance" : 30,
                "revert": function (event, ui) {
                    // on older version of jQuery use "draggable"
                    // $(this).data("draggable")
                    // on 2.x versions of jQuery use "ui-draggable"
                    // $(this).data("ui-draggable")
                    $(this).data("uiDraggable").originalPosition = {
                        top: 0,
                        left: 0
                    };

                    $(this).draggable("option", "axis", false);

                    // return boolean
                    return !event;
                    // that evaluate like this:
                    // return event !== false ? false : true;
                }
            });

            $(".trek_slider").droppable({
                "tolerance": "touch",
                "over": function (event,
                    ui) {
                    var left_position = parseInt($(ui.draggable).css("left"));

                    $(ui.draggable).find(".activity_caret").show();

                    $(ui.draggable).draggable("option", "axis", "y");
                }
            });

Try this

$( ".your_div" ).draggable({ grid: [ 1,1 ] });

In above example it will be draggedx by only one pixel in each direction

Please Note: Do not forget to add your other options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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