简体   繁体   English

如何只使一个子元素粘性而不影响其余部分?

[英]How to make only one child element sticky without affecting the rest?

The relevant elements in the code below are .top , .sticky and .other . 在下面的代码中的相关元素是.top.sticky.other

.sticky and .other are sitting inside the .navbar container. .sticky.other坐在里面.navbar容器。

Now, making the whole .navbar sticky is easy. 现在,使整个.navbar粘性很容易。 Adding position: sticky; top: 0; 添加position: sticky; top: 0; position: sticky; top: 0; to that element does that. 那个元素做到了。 That's the general behavior I want. 这是我想要的一般行为。

The challenge I'm facing is: 我面临的挑战是:

How to make only the .sticky element sticky so that everything underneath will always scroll underneath it? 如何使.sticky元素粘,以便下面的所有内容总是在它下面滚动?

If you run the current code, you can see that the .sticky element only remains sticky until the yellow .other element has scrolled underneath it. 如果您运行当前的代码,你可以看到.sticky直到黄色元素只保持粘性.other元素都被滚动在它之下。 After that the .sticky element stops being sticky. 之后, .sticky元素不再粘。

That's the problem I'm trying to solve. 这就是我想要解决的问题。 How to keep the blue .sticky element always sticky? 如何保持蓝色.sticky元素总是粘的? (after you scroll up to the point where the green .top part becomes hidden) (向上滚动到绿色.top部分隐藏的位置后)

Ideally looking for a css-only solution . 理想情况下寻找仅限CSS的解决方案 If that's not possible, a JavaScript/jQuery solution would be acceptable but has to work properly when scrolling in both directions up or down. 如果那是不可能的,那么JavaScript / jQuery解决方案是可以接受的,但是当向上或向下向两个方向滚动时必须正常工作。

UPDATE: 更新:

I know that taking the yellow .other element out of the header would solve the problem. 我知道从标题中取出黄色.other元素可以解决问题。 But the problem is that the yellow .other element needs to be part of the header. 但问题是黄色.other元素需要成为标题的一部分。 Both .sticky and .other need to be displayed next to each other on large screens and under each other on mobile screens. 无论.sticky.other需要在手机屏幕并排显示在大屏幕上,并在对方。 That's why they are currently child elements inside another container. 这就是为什么它们目前是另一个容器中的子元素。 So, moving the yellow element out of the header is not really an option I could use in this case. 因此,将黄色元素移出标题并不是我在这种情况下可以使用的选项。

 .top { min-height: 40px; background-color: darkgreen; } .navbar { /* position: sticky;*/ /* top: 0;*/ } .sticky { min-height: 50px; background-color: lightblue; position: sticky; top: 0; } .other { min-height: 90px; background-color: yellow; } .content { background-color: lightgray; min-height: 600px; } .bottom { background-color: darkgray; min-height: 200px; } 
 <div class="top"> Top </div> <div class="middle"> <header class="navbar"> <div class="header-container"> <div id="row-masthead"> <div class="sticky"> Sticky </div> <div class="other"> Other </div> </div> </div> </header> <main class="content"> <h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> </main> </div> <div class="bottom"> <h2>Footer</h2> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> </div> 

One solution is to consider the use of display:contents in the containers of the sticky elements to make them like they don't exist and you will have what you want: 一种解决方案是考虑使用display:contents sticky元素的容器中的内容,使它们不存在,你会得到你想要的:

 .top { min-height: 40px; background-color: darkgreen; } .navbar, .header-container, #row-masthead{ display:contents /* added this */ } .sticky { min-height: 50px; background-color: lightblue; position: sticky; top: 0; } .other { min-height: 90px; background-color: yellow; } .content { background-color: lightgray; min-height: 600px; } .bottom { background-color: darkgray; min-height: 200px; } 
 <div class="top"> Top </div> <div class="middle"> <header class="navbar"> <div class="header-container"> <div id="row-masthead"> <div class="sticky"> Sticky </div> <div class="other"> Other </div> </div> </div> </header> <main class="content"> <h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> </main> </div> <div class="bottom"> <h2>Footer</h2> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> </div> 

Another hacky solution is to create the needed space the sticky element using a combination of height and negative margin like below. 另一个hacky解决方案是使用如下所示的高度和负边距的组合来创建粘性元素所需的空间。 The drawback is that you need to find the correct value to avoid creating a lot of space at the bottom and to have the needed space for the sticky. 缺点是您需要找到正确的值,以避免在底部创建大量空间并为粘性提供所需的空间。

 .top { min-height: 40px; background-color: darkgreen; } div#row-masthead:after { content: ""; display: block; height: 200vh; /*big value here*/ } div#row-masthead { margin-bottom:-200vh; /*same value here*/ } .sticky { min-height: 50px; background-color: lightblue; position: sticky; top: 0; } .other { min-height: 90px; background-color: yellow; } .content { background-color: lightgray; min-height: 600px; } .bottom { background-color: darkgray; min-height: 200px; } 
 <div class="top"> Top </div> <div class="middle"> <header class="navbar"> <div class="header-container"> <div id="row-masthead"> <div class="sticky"> Sticky </div> <div class="other"> Other </div> </div> </div> </header> <main class="content"> <h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> </main> </div> <div class="bottom"> <h2>Footer</h2> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> </div> 

With some JS you can dynamically and correctly calculate the value: 使用一些JS,您可以动态,正确地计算值:

 var h = document.querySelector('.middle').offsetHeight - document.querySelector('.navbar').offsetHeight; document.querySelector('#row-masthead').style.setProperty("--h", h+"px"); window.onresize = function(event) { h = document.querySelector('.middle').offsetHeight - document.querySelector('.navbar').offsetHeight; document.querySelector('#row-masthead').style.setProperty("--h", h+"px"); } 
 .top { min-height: 40px; background-color: darkgreen; } div#row-masthead:after { content: ""; display: block; height: var(--h,0); } div#row-masthead { margin-bottom:calc(-1*var(--h,0)); } .sticky { min-height: 50px; background-color: lightblue; position: sticky; top: 0; } .other { min-height: 90px; background-color: yellow; } .content { background-color: lightgray; min-height: 600px; } .bottom { background-color: darkgray; min-height: 200px; } 
 <div class="top"> Top </div> <div class="middle"> <header class="navbar"> <div class="header-container"> <div id="row-masthead"> <div class="sticky"> Sticky </div> <div class="other"> Other </div> </div> </div> </header> <main class="content"> <h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> </main> </div> <div class="bottom"> <h2>Footer</h2> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> </div> 

From what I understand from position:sticky , you would have to redefine your HTML structure and move element with class .other out of the header. 从我对位置的理解:粘性 ,你将不得不重新定义你的HTML结构并移动元素与类。其他从标题。 And also, uncomment your css code regarding the .navbar class. 此外,取消注释关于.navbar类的css代码。

Something like this: 像这样的东西:

 .top { min-height: 40px; background-color: darkgreen; } .navbar { position: sticky; top: 0; } .sticky { min-height: 50px; background-color: lightblue; position: sticky; top: 0; } .other { min-height: 90px; background-color: yellow; } .content { background-color: lightgray; min-height: 600px; } .bottom { background-color: darkgray; min-height: 200px; } 
  <div class="top"> Top </div> <div class="middle"> <header class="navbar"> <div class="header-container"> <div id="row-masthead"> <div class="sticky"> Sticky </div> </div> </div> </header> <div class="other"> Other </div> <main class="content"> <h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> </main> </div> <div class="bottom"> <h2>Footer</h2> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> </div> 

check the following code, it may help you. 检查以下代码,它可能会对您有所帮助。 Check it into full page or full screen mode, in the desktop mode sticky and other sections are side by side and in mobile view other div is under the sticky div. 将其检查为整页或全屏模式,在桌面模式下粘贴,其他部分并排,在移动视图中,其他div位于粘性div下。 It needs media queries for mobile view. 它需要移动视图的媒体查询。

 .top { min-height: 40px; background-color: darkgreen; } .navbar { height:auto; } .sticky { min-height: 90px; background-color: lightblue; width:50%; float:left; } .row_masthead { position: fixed; top: 0; width:100%; } .other { min-height: 90px; background-color: yellow; width:50%; float:left; } .content { background-color: lightgray; min-height: 600px; margin-top:4%; } .bottom { background-color: darkgray; min-height: 200px; } @media only screen and (max-width:768px) and (min-width:200px) { .sticky { width:100%; } .other { width:100%; float:left; } .content { margin-top:43%; } } 
 <div class="top"> Top </div> <div class="middle"> <header class="navbar"> <div class="header-container"> <div id="row-masthead" class="row_masthead"> <div class="sticky"> Sticky </div> <div class="other"> Other </div> </div> </div> </header> <main class="content"> <h2> Content </h2> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> </main> </div> <div class="bottom"> <h2>Footer</h2> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus non vero quam suscipit veniam ex praesentium id sunt, voluptatibus ad ipsa asperiores enim quis omnis alias maxime saepe aut. Iste. </p> </div> 

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

相关问题 如何在不影响父元素的情况下为子元素设置不同的 onClick 事件? - How to have a different onClick event for the child element without affecting the parent one? 如何在不影响父元素的情况下设置子元素的 contenteditable 属性? - How to set child's contenteditable property without affecting the parent element? 如何使子元素粘在父底部而忽略顶部溢出? - How to make child element sticky to parent bottom ignoring top overflow? 如何仅使用 HTML 和 CSS(无 JS)制作“粘性”导航栏? - How to make a “sticky” navbar using only HTML and CSS (without JS)? 如何在不影响元素的 rest 和任何事件侦听器的情况下覆盖元素文本的一部分 - How can I overwrite a portion of an element's text without affecting the rest of the element and any event listeners 如何在不影响其子元素的情况下更改元素的文本? - How can I change the text of an element without affecting its child element? 使用jQuery切换仅影响一个元素 - Affecting only one element with jQuery toggle 如何使粘性元素“浮动” - How to Make a sticky element 'float' 如何在不影响模板的情况下使用角度模拟REST - How to mock REST with angular without affecting templates 如何仅拖放特定选定的节点而不影响gojs图表API中的子节点 - How to drag and drop only particular selected node without affecting child nodes in gojs chart API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM