简体   繁体   English

如何将元素锚定到窗口的左侧

[英]How to anchor an element to the left side of the window

I have a page with two elements, a header and a footer. 我有一个包含两个元素的页面,一个页眉和一个页脚。

 html, body, main { width: 100%; height: 100%; background: #888; margin: 0; padding: 0; } header { width: 100%; height: 20%; background: rgba(0, 255, 0, .1); } footer { width: 10000px; height: 80%; background: rgba(255, 0, 0, .1); } 
 <main> <header></header> <footer></footer> </main> 

Please check this codepen for a live example. 请查看此Codepen的实时示例。


When I scroll the window horizontally, I need the header to always be centered in the middle of the screen. 当我水平滚动窗口时,我需要标题始终位于屏幕中间。

I can't use position: fixed because I need this element to be in the page flow. 我无法使用position: fixed因为我需要此元素位于页面流中。

position: sticky does exactly what I need, but unfortunately I can't use it because the parent element's width is the same as the viewport width. position: sticky完全满足我的需要,但是不幸的是我无法使用它,因为父元素的宽度与视口的宽度相同。 If I set the parent width to a width that is bigger than the viewport itself, I achieve what I want, but I'm hoping for a better solution. 如果将父级宽度设置为大于视口本身的宽度,则可以实现所需的效果,但是我希望有一个更好的解决方案。

I'd prefer a CSS-only solution but I'm open to a JS solution. 我希望使用仅CSS的解决方案,但可以使用JS解决方案。


One of the many things I've tried so far is listening for the scroll event and adding a margin left to the element equal to window.scrollX , the idea being that it would stay anchored to the left edge of the window. 到目前为止,我尝试过的许多操作之一是侦听滚动事件,并在元素上添加等于window.scrollX左边距,其想法是它会保持锚定在窗口的左边缘。 However that doesn't actually work, I'm not sure why. 但是,这实际上不起作用,我不确定为什么。

In the example, if you try to set position: sticky; left: 0; 在示例中,如果您尝试设置position: sticky; left: 0; position: sticky; left: 0; in the header, then give a width: 10000px; 在标题中输入width: 10000px; to the main element, you'll see my desired layout. 在主要元素上,您将看到我想要的布局。

Is it possible to achieve this same layout, but without having to set a width? 是否可以实现相同的布局,而不必设置宽度?

Does the footer need to have a hardcoded width ? 页脚是否需要具有硬编码的width That seems to be where you'll have the most issues. 这似乎是您遇到最多问题的地方。 If you simply have content in the footer that needs to be displayed and may be wider than the header, then I would recommend utilizing a solution like the following: 如果您仅在页脚中有需要显示的内容,并且内容可能比页眉宽,那么我建议您使用如下解决方案:

 html, body, main { width: 100%; height: 100%; background: #888; margin: 0; padding: 0; } header { width: 100%; height: 20%; background: rgba(0,255,0,.1); } footer { display: flex; overflow-x: scroll; height: 80%; background: rgba(255,0,0,.1); } h1 { margin-left: 70px; } 
 <main> <header>Header</header> <footer> <h1>Content</h1> <h1>Content</h1> <h1>Content</h1> <h1>Content</h1> <h1>Content</h1> <h1>Content</h1> <h1>Content</h1> <h1>Content</h1> <h1>Content</h1> <h1>Content</h1> </footer> </main> 


Change the header css to this 将标头CSS更改为此

header {
  width: 100%;
  height: 20%;
  background: rgba(0,255,0,.1);
  position: fixed;
  float-left: auto;
  float-right: auto;
}

Fixed elements are relative to the html document and not the parent container, and aren't affected by scrolling. Fixed元素相对于html文档而不是父容器,并且不受滚动影响。 Float left and right auto center it on the page. 在页面上Float left and right auto居中。

If you want the footer to go over the header on vertical scroll you should use z-index property. 如果希望footer越过垂直滚动的header ,则应使用z-index属性。

footer {
  width: 10000px;
  height: 80%;
  background: rgba(255,0,0,.1);
  position: fixed;
  z-index: 1
}

Also you can fiddle between fixed and absolute absolute is relative to the parent container unlike fixed 你也可以在fixedabsolute绝对之间进行相对于父容器的fixed

Your solution of writing javascript function that will handle the marginLeft value of the header was correct. 您编写的将处理标头的marginLeft值的javascript函数的解决方案是正确的。
I dont know why it did not work for you because you did not showed us your JS code, but here one way to achieve that: 我不知道为什么它对您不起作用,因为您没有向我们展示您的JS代码,但是这里有一种实现方法:

LIVE DEMO 现场演示

<script>
  window.addEventListener("scroll", function () {
    var doc = document.documentElement;
    var left = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0);
    document.getElementById("myheader").style.marginLeft = left.toString() + "px";
  }, false);
</script>

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

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