jQuery center element to viewport using center plugin

前端 未结 2 1499
孤独总比滥情好
孤独总比滥情好 2021-01-22 21:04

I am currently using the jQuery center plugin to center my div element and so far it works to an extend where it does center it to the containing parent container. What I want

相关标签:
2条回答
  • 2021-01-22 21:28

    No plug-in is needed. This does the trick:

    $('#yourElement').css('display', 'inline-block').wrap('<table style="position:fixed;top:0;left:0;height:100%;width:100%;border-spacing:0;border-collapse:collapse"><tr><td style="vertical-align:middle;text-align:center;padding:0"></td></tr></table>');
    

    Live demo: http://jsfiddle.net/simevidas/EnrLn/1/


    Same thing, but without the TABLE element. I use DIV's with display:table and display:table-cell instead:

    Live demo: http://jsfiddle.net/simevidas/EnrLn/2/

    0 讨论(0)
  • 2021-01-22 21:35

    Use

    jQuery( "#element_id" ).css( 'top', parseInt( ( jQuery( window ).height() / 2 ) + jQuery( document ).scrollTop() - jQuery( "#element_id" ).height() ) );
    jQuery( "#element_id" ).css( 'left', parseInt( ( jQuery( document ).width() / 2 ) + jQuery( document ).scrollLeft() - jQuery( "#element_id" ).width() ) );
    
    0 讨论(0)
提交回复
热议问题