CSS3 Transition from normal flow position to absolute

微笑、不失礼 提交于 2019-12-04 10:19:08

Yes, it would be cool that we could be able to transition from position static to absolute, but realistically I don't think it's coming soon (if ever). I'd be happy with just being able to transition from height: px to height: auto;.

My guess is that there is some sort of tradeoff when the browser has to make a calculation to interpolate between two 'incompatible' values. So, if you set height: 20px and then want to transition to height: auto, then the browser would have to imagine what would happend if it had position auto and the calculations could get intensive. It's not implemented in jQuery either, so I guess it breaks some tests, or it's just plain hacky.

If you architect your css knowing that you will need position absolute to always refer to the window, then the javascript is drastically more simple: you just have to toggle between its offset and 0, 0.

$(".hover").on("mouseover", function(){
    $(this).css({
        top: $(this).offset().top * -1,
        left: $(this).offset().left * -1
    })
}); 

http://jsfiddle.net/crUFY/

A more robust solution would involve cloning the dom element and hiding the original, then animating the clone to the top of the window. This way you can apply position: relative to the parent elements.

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