开发者

How to center an iframe that is created via injection

I am trying to create a bookmarklet that creates an iframe on the page you're on if you click on it. I can get the iframe to show up but unable to make it centered on the page开发者_高级运维.

Here is the code:

javascript:(function(e,a,g,h,f,c,b,d){if(!(f=e.jQuery)||g>f.fn.jquery||h(f)){c=a.createElement("script");c.type="text/javascript";c.src="http://ajax.googleapis.com/ajax/libs/jquery/"+g+"/jquery.min.js";c.onload=c.onreadystatechange=function(){if(!b&&(!(d=this.readyState)||d=="loaded"||d=="complete")){h((f=e.jQuery).noConflict(1),b=1);f(c).remove();}};a.documentElement.childNodes[0].appendChild(c);}})(window,document,"1.5.1",function($,L){
    var a = $('<iframe/>', {
        id: 'test-iframe',
        class: 'test-iframe',
        style: 'position: absolute;z-index: 99999;',
        width: '700',
        height: '500',
        src: 'http://google.com'
    });
    $('body').prepend(a);
});

Any help is appreciated.


Add margin: 0 auto; to the style.

You can also have the iframe in a container (e.g. DIV) which you can add margin: 0 auto; to.


Within the style, give it

margin: auto;


 #test-iframe {
       text-align: center;
 }


I got it solved. I added "width:700px;margin-left:-350px;left:50%" to the style. Basically just like the way I would trying to center an absolutely positioned

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜