Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jQuery draggable with fixed position?

It works perfect in firefox, but in ie, chrome and opera it doesn't work.

<div> has position:fixed, and is .draggable()

and it doesn't work except firefox

like image 229
rafaello Avatar asked Aug 04 '10 11:08

rafaello


2 Answers

don't set fixed in CSS: it works in firefox, chromium, safari, iexplore

var div = $('#id');
div.resizable(
{
    stop: function(event, ui)
    {                       
        var top = getTop(ui.helper);
        ui.helper.css('position', 'fixed');
        ui.helper.css('top', top+"px");         
    }       
});
div.draggable(
{
    stop: function(event, ui)
    {           
        var top = getTop(ui.helper);
        ui.helper.css('position', 'fixed');
        ui.helper.css('top', top+"px");
    }
});

function getTop(ele)
{
    var eTop = ele.offset().top;
    var wTop = $(window).scrollTop();
    var top = eTop - wTop;

    return top; 
}
like image 166
ZiTAL Avatar answered Oct 22 '22 19:10

ZiTAL


Just use in your CSS:

#draggable{
    position:fixed !important;
}

If you are using both draggable and rezisable delete the "!important" from the CSS, and then set the stop option (the callback when the dragging and resizing stops) to this function:

function stop(event){
    if(event.type === "resizestop"){
        var topOff = $(this).offset().top - $(window).scrollTop()
        $(this).css("top",topOff)
    }
    $(this).css("position","fixed")     
}   
like image 45
Ivan Castellanos Avatar answered Oct 22 '22 17:10

Ivan Castellanos