Convert absolute position to relative

前端 未结 5 1284
一生所求
一生所求 2021-01-19 00:52

Is it possible to change DIV position from absolute to relative (and from relative to absolute)? DIV should remain on same place.

5条回答
  •  日久生厌
    2021-01-19 01:42

    Because formatting in comments is not work I will publish solution here

    $(object).css({position: 'absolute',top: dy, left:dx});
    // dy, dx - some coordinates
    $(object).css({position: 'relative'});
    

    Does not work: element position after changing to relative is different.

    But when I stored offset and set it again after changing to relative, position is the same:

    $(object).css({position: 'absolute',top: dy, left:dx});
    var x = $(object).offset().left;
    var y = $(object).offset().top;
    $(object).css({position: 'relative'});
    $(object).offset({ top: y, left: x }); 
    

提交回复
热议问题