加载后如何在中心中心或特定高度开始页面

How to start page at center of center or at specific height after load

本文关键字:高度 开始 在中心 加载      更新时间:2023-09-26

我想在水平和垂直的中心开始页面,当它已经加载(不是在顶部),有人有什么建议吗?或者在一个特定的高度,如果可能的话。谢谢你!

你可以这样做:

$(document).ready(function() {
    $(window).scrollTop($(window).height()/2);
    $(window).scrollLeft($(window).width()/2);
});

你可以根据自己的需要改变位置。

也可以使用$(window).scrollTo($(window).width()/2, $(window).height()/2);

不知道你在问什么,但你试过相对坐标吗?

like>>

/*i put this all css selector for canceling all margins, paddings so i can remove default browser prefereces for the same*/
/*border box is just that any size od div is not changed after addinational padding*/
*{margin:0;padding:0;box-sizing:border-box;}
		body{
			position:absolute;
			width:100%;height:100%;
			background-color:red;
			padding-top:10%;
			padding-left:10%;
			padding-right:10%;
			padding-bottom:10%;
		}
		centerd{
            /*relative dimensions wont work if not display:block;*/
			display:block;
			width:100%;height:100%;
			background-color:blue;
		}
<!doctype html>
<html>
<meta charset = "UTF-8" />
<title>Center Content</title>
<!--[if lt IE 9]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<body>
	
	<centerd>
        <!--html5 element, you can create your own -->
	</centerd>
	
</body>
</html>

更多信息请访问:这是一个很酷的方式来了解足够的html, css, javascript等

虽然不确定这是否是您正在寻找的:

要使容器(例如DIV元素)水平居中,在CSS中设置固定宽度和自动左右距:

div#container
{   width: 1024px; /* a fixed width container */
    border: thin solid green; /* debug, to see */
    margin-left: auto;
    margin-right: auto;
}

然后是一个匿名JavaScript函数,在加载后使用它的margin-top属性将容器垂直居中:

function ()
{  var e = document.getElementById("container");
   var eHeight = e.offsetHeight;
   var clientHeight = document.documentElement.clientHeight;
   var marginTop = 0;
   if(clientHeight > eHeight)
   {   marginTop = (clientHeight - eHeight) >> 1; // integer divide by 2
   }
   e.style.marginTop = marginTop + "px";

}

使用jQuery的ready()函数将

添加到页面的窗口,以及HTML

<div id="container">
    hello this is page content
</div>

将容器元素居中

老问题,但我只是使用:

function Scrolldown() {
    window.scroll(250, 400);
}
window.onload = Scrolldown;