swati
swati

Reputation: 129

make a div draggable on if condition is true?

I have a div which should be a draggable. Below is the code,

$( "#SpeedDraggable" ). draggable
({
    axis: "y",
    containment:'parent',
        drag: function(event, ui)
        {
           var Startpos = $(this).position();
           $("#SpeedText").text(184-Startpos.top).css('color','#DDDDDD');              
        },
    stop: function(event, ui) 
    {
       var Stoppos = $(this).position();
       $("#SpeedText").text(184-Stoppos.top).css('color','#DDDDDD');
    }
    });

The Requirement is that above logic should happen under this if condition

if($("#imgManualSetting_Pressed").is(":visible"))
{

}

If this condition is not true, Then the div should not be draggable. Kindly help me how to do this... Thanks in advance !!

Upvotes: 1

Views: 2365

Answers (2)

swati
swati

Reputation: 129

$( "#SpeedDraggable" ). draggable
({
    axis: "y",
    containment:'parent',
    drag: function(event, ui)
    {
        if($("#imgManualSetting_Pressed").is(":visible"))
        {
            var Startpos = $(this).position();
                $("#SpeedText").text(184-Startpos.top).css('color','#DDDDDD');  
        }
        else
       {
                return false;            
       }                    
    }

});

Upvotes: 2

cwdoh
cwdoh

Reputation: 196

if($("#imgManualSetting_Pressed").is(":visible")) {
    // set to draggable widget
    $( "#SpeedDraggable" ).draggable({
        axis: "y",
        containment:'parent',
            drag: function(event, ui)
            {
               var Startpos = $(this).position();
               $("#SpeedText").text(184-Startpos.top).css('color','#DDDDDD');              
            },
        stop: function(event, ui) 
        {
           var Stoppos = $(this).position();
           $("#SpeedText").text(184-Stoppos.top).css('color','#DDDDDD');
        }
    });
}
else {
    // destroy draggable if not
    $( "#SpeedDraggable" ).draggable( "destroy" );
}

Upvotes: 1

Related Questions