Question

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.net/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.com" 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');

});
Was it helpful?

Solution

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

OTHER TIPS

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

Instead of making complete 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.com" 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.com" 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.net/tr4cU/6/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top