English 中文(简体)
隔膜需要垂直的屏幕中心
原标题:Div need vertical center of the screen after page scrolling

我有<条码>div,需要在网页浏览后垂直放在屏幕上。

http://jsfiddle.net/JtZ7F/“rel=“nofollow” http://jsfiddle.net/JtZ7F/。

<div style="height:1000px; border:1px solid red;">
    scroll
    <span style="height:150px; width:100px; float:right; border:1px solid green; display:block; position:fixed;top:50%; margin-top:-75px;">
        need vertical center of the screen after page scroling
    </span>
</div>

现在,我使用<代码> :固定;,它不是解决办法。

我想,当我停止把我的绿箱上滚动的网页时,我会顺利地转移到屏幕的垂直中心。

我怎么能够这样做呢?

问题回答

在你的领导下增加这一职能:

jQuery.fn.center = function () {   
this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");    
 this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");     return this; } 

在“测试”之前添加一个字句,并在文件中写下以下文字。 准备就绪

$(document).ready(function(){    
$( #test ).center(); 
});

这里,我如何做,在 Chrome和安检;IE7+,见jsfiddle

$.fn.center = function () {
                    return this.css({
                         left : ($(window).width() / 2) - $(this).width() / 2,
                         top : ($(window).height() / 2) - $(this).height() / 2,
                         position :  fixed 
                    });
                };

使用:

$(document).ready(function(){ 
    $(element).center();
});




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签