简体   繁体   English

CSS JQuery Div相对于窗口大小的起始位置

[英]CSS JQuery Div start position relative to window size

I have a long single page layout. 我有一个很长的单页面布局。 The main container div with all the content is designed to start close to the bottom of the page (white box, peeking out from the bottom just enough to see the logo, before the user has to scroll to see more). 包含所有内容的主容器div设计为靠近页面底部开始(白色框,从底部窥视刚好足以看到徽标,在用户必须滚动查看更多内容之前)。

Problem is different screen sizes and different size browsers, the main container div starts in different heights from the top, showing more or less than intended when the user first arrives. 问题是不同的屏幕尺寸和不同尺寸的浏览器,主容器div从顶部开始处于不同的高度,显示出比用户第一次到达时的预期更多或更少。

My thought is that I will need to use js to calculate the window.height, subtract the height of the amount of the container I want to initially show, and then wrap all that in an event handler that tracks the change of browser size. 我的想法是我将需要使用js来计算window.height,减去我想要最初显示的容器数量的高度,然后将所有这些包装在跟踪浏览器大小变化的事件处理程序中。

I'm a noob with Jquery however, and can't figure it out. 然而,我是Jquery的菜鸟,无法弄明白。 Any examples, or tutorials available that will lead the way? 任何可用的示例或教程都将引领潮流?

Made a fiddle for this: http://jsfiddle.net/QQaZp/1/ 为此做了一个小提琴: http//jsfiddle.net/QQaZp/1/

Basically, set whatever you want positioned (I just used div, but you should use #whateverID) to position absolute in your css. 基本上,设置你想要的任何位置(我只是使用div,但你应该使用#whateverID)在你的CSS中定位绝对值。 On load and on resize in the javascript, just set the top property to whatever the $(window).height() is (I subtracted 50px from this so it peeked up a bit) 在javascript中加载和调整大小时,只需将top属性设置为$(window).height()是什么(我从中减去了50px,所以它$(window).height()

If I understand correctly, you don't need JavaScript for this. 如果我理解正确,您不需要JavaScript。 Position the div absolutely 100% from the top and then set margin-top to a negative value. 将div从顶部完全放置100%,然后将margin-top设置为负值。

CSS: CSS:

body, html {height: 100%;}

.section-1 {
    margin-top: -60px; /* Whatever value you want peeking out */
    position: absolute;
    top: 100%;
}

See a working JSFiddle here: http://jsfiddle.net/dsundy/5kt8P/1 . 在这里查看一个有效的JSFiddle: http//jsfiddle.net/dsundy/5kt8P/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM