How to use jQuery draggable with fixed position?

你说的曾经没有我的故事 提交于 2019-12-10 02:56:18

问题


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


回答1:


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; 
}



回答2:


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")     
}   



回答3:


if you put a break point on the ...draggable(...); you'll see that it is added position:fixed to the element.style.

just undo that with .style.position ="";

my code clears the style so it would fall back on the css declaration. with older jquery ui version, you may need to do the drag stop handler. which i doubt. but definitely not true for the latest.



来源:https://stackoverflow.com/questions/3404907/how-to-use-jquery-draggable-with-fixed-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!