简体   繁体   English

CSS 如何为 y 轴固定 position 元件?

[英]CSS how to position element fixed just for y-axis?

I have an element( 2 anchors next to each other) inside the div(spans about 1150px so you need to scroll down to see all the contents of this div).我在 div 中有一个元素(彼此相邻的 2 个锚点)(跨度约为 1150 像素,因此您需要向下滚动才能查看此 div 的所有内容)。 This anchors are position:fixed at the top of this div so as you scroll down the div anchor will be visible all the time.这个锚是 position:固定在这个 div 的顶部,所以当你向下滚动时,div 锚将一直可见。 My problem is when you shrink the width of the browser window, I want second anchor to go below the first one as the space runs out, however until the browser window physically reaches the anchors, no wrap around is happening, so div is getting smaller and two anchors are overlapping each other.我的问题是,当您缩小浏览器 window 的宽度时,我希望第二个锚点位于第一个锚点下方的 go 下方,因为空间用完,但是直到浏览器 window 实际到达锚点之前,没有出现环绕锚点的情况。并且两个锚点相互重叠。

When I remove the position fixed, as I resizes the browser window and div's width shrinks one anchor wraps below the other one as expected.当我删除固定的 position 时,当我调整浏览器 window 的大小时,div 的宽度会按预期缩小一个锚点环绕在另一个锚点下方。 So I am guessing I just need y-axis fixed but not x-axis.所以我猜我只需要固定 y 轴而不需要 x 轴。

The position attribute applies to the element as a whole. position 属性适用于整个元素。 What you're really asking is for a new kind of position attribute.您真正要的是一种新的 position 属性。

You could apply the fixed positioning to an element that contains your two anchors, and if your anchors are set to float, they will wrap if they need to:您可以将固定定位应用于包含两个锚点的元素,如果您的锚点设置为浮动,它们将在需要时包裹:

<style type="text/css">
  #fixed {
    position: fixed;
  }
  .left {
    float: left;
  }
  .right {
    float: right;
  }
</style>

...

<div id="fixed">
  <div class="left">Lorem ipsum dolor sit amet,</div><div class="right">consectetur adipiscing elit.</div>
</div>
<div id="content">
  <p>something here...</p>
</div>

A similar question about fixed-y / scrollable-x was the subject of an earlier discussion whose conclusion was you need JavaScript to do it, as others have said.正如其他人所说,关于fixed-y / scrollable-x的类似问题是先前讨论的主题,其结论是您需要JavaScript来做到这一点。

The general approach in that answer was to look every tenth of a second or so to see if the elements should be moved, and then to move them if so.该答案中的一般方法是每隔十分之一秒左右查看元素是否应该移动,如果是,则移动它们。

Looks like this may be a possible duplicate of this question .看起来这可能是这个问题的可能重复。

In a nutshell, you can't do this with purely CSS.简而言之,你不能用纯粹的 CSS 来做到这一点。 You'll need JavaScript.您将需要 JavaScript。 I recommend jQuery .我推荐jQuery

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

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