javascript - Prevent `click` while the link is dragged - Stack Overflow

admin2025-04-19  0

I'm using gridster to make a grid of links. The link should work normal when click on it. Problem is it's also get clicked after dragged. How can I stop it from being clicked after dragged?

Please check: /

<div class="gridster">
    <ul id="reszable">
        <li data-row="1" data-col="1" data-sizex="1" data-sizey="1"></li>
        <li data-row="2" data-col="1" data-sizex="1" data-sizey="1"></li>
        <li data-row="3" data-col="1" data-sizex="1" data-sizey="1"><a href="" target="_blank">LINK</a></li>
        <li data-row="1" data-col="2" data-sizex="2" data-sizey="1"></li>
        <li data-row="2" data-col="2" data-sizex="2" data-sizey="2"></li>
        <li data-row="1" data-col="4" data-sizex="1" data-sizey="1"></li>
    </ul>
</div>

Js:

$(function(){

    $(".gridster ul").gridster({
        widget_margins: [5, 5],
        widget_base_dimensions: [100, 100]
    });

    var gridster = $(".gridster ul").gridster().data('gridster');

});

I'm using gridster to make a grid of links. The link should work normal when click on it. Problem is it's also get clicked after dragged. How can I stop it from being clicked after dragged?

Please check: http://jsfiddle/b_m_h/tr4cU/

<div class="gridster">
    <ul id="reszable">
        <li data-row="1" data-col="1" data-sizex="1" data-sizey="1"></li>
        <li data-row="2" data-col="1" data-sizex="1" data-sizey="1"></li>
        <li data-row="3" data-col="1" data-sizex="1" data-sizey="1"><a href="http://google." target="_blank">LINK</a></li>
        <li data-row="1" data-col="2" data-sizex="2" data-sizey="1"></li>
        <li data-row="2" data-col="2" data-sizex="2" data-sizey="2"></li>
        <li data-row="1" data-col="4" data-sizex="1" data-sizey="1"></li>
    </ul>
</div>

Js:

$(function(){

    $(".gridster ul").gridster({
        widget_margins: [5, 5],
        widget_base_dimensions: [100, 100]
    });

    var gridster = $(".gridster ul").gridster().data('gridster');

});
Share Improve this question asked Aug 19, 2013 at 10:36 BMHBMH 4,3401 gold badge20 silver badges19 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 5

Don't know if there is something built in as jQuery draggable has options for this, but couldn't find anything similar for gridster.

You could always create the functionality yourself:

$(".gridster ul").gridster({
    widget_margins: [5, 5],
    widget_base_dimensions: [100, 100]
}).on({
    mousedown: function(e) {
        $(this).data({top: e.pageX, left: e.pageY});
    },
    mouseup: function(e) {
        var top   = e.pageX,
            left  = e.pageY,
            ptop  = $(this).data('top'),
            pleft = $(this).data('left');

        $(this).data('dragged', Math.abs(top - ptop) > 15 || Math.abs(left - pleft) > 15);
    },
    click: function(e) {
        if ( $(this).data('dragged') ) e.preventDefault();
    }
}, 'a');

FIDDLE

I'm not sure this could help, but just for an idea

Instead of making plete griddle as clickable, why not use only Link as clickable, what i mean is

<li data-row="3" data-col="1" data-sizex="1" data-sizey="1">
    <p> <a href="http://google." target="_blank">LINK</a></p></li>

Doing this will fulfill what you needed, have tried this and it works

    <div class="gridster">
    <ul id="reszable">
<li data-row="1" data-col="1" data-sizex="2" data-sizey="1"></li>
<li data-row="2" data-col="1" data-sizex="1" data-sizey="1"></li>
<li data-row="3" data-col="1" data-sizex="1" data-sizey="1">
    <p> <a href="http://google." target="_blank">LINK</a></p></li>
<li data-row="1" data-col="2" data-sizex="2" data-sizey="1"></li>
<li data-row="2" data-col="2" data-sizex="2" data-sizey="2"></li>
<li data-row="1" data-col="4" data-sizex="1" data-sizey="1"></li>

draggable: {
        start: function(event, ui) {

            dragged = 1;
            // DO SEOMETHING
        }
    }

...
    if(!dragged){
        // DO SOMETHING
    }
    // RESET DRAGGED SINCE CLICK EVENT IS FIRED AFTER drag stop
dragged = 0

How do I avoid a click event firing after dragging a gridster.js widget with clickable content?

I think that link answers the same question

$(function(){ //DOM Ready

    $(".gridster ul").gridster({
        widget_margins: [5, 5],
        widget_base_dimensions: [100, 100],
        draggable: {
            start:  function(event, ui){ 
              $("a").click(function(event) { event.preventDefault(); }) // prevent the click event when the drag started 

            },
        }

    });

    $("#reszable > li").mouseleave(function(){
        $("a").unbind('click');  // bind the click event again when the drag stopped 
    });

    var gridster = $(".gridster ul").gridster().data('gridster');

});

I have updated your fiddle http://jsfiddle/tr4cU/6/

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745003535a279374.html

最新回复(0)