简体   繁体   English

如何使用 Javascript 解决多个粘性标题问题?

[英]How to solve Multiple Sticky Header problem with Javascript?

I have a two headers.我有两个标题。

-----------------
   Header 1
----------------

----------------
     Slider
----------------

----------------
   Header 2
----------------

On scroll Header 1 will be sticky.在滚动标题 1 将是粘性的。 And after crossing Slider Header 2 will be sticky with Header 1.并且在穿过 Slider Header 2 后将与 Header 1 粘在一起。

-----------------
   Header 1
----------------
----------------
   Header 2
----------------

How is this possible?这怎么可能?

I have done some part:我做了一些部分:

window.onscroll = function() {
    myFunction()
};

var header2 = document.getElementById("masthead2");
var header3 = document.getElementById("myHeader");

var sticky2 = header2.offsetTop;
var sticky3 = header3.offsetTop;


function myFunction() {

  if(window.pageYOffset > sticky2)
  {
    header2.classList.add("sticky");
  }
  else
  {
    header2.classList.remove("sticky");
  }


  if (window.pageYOffset > sticky3) {
    header3.classList.add("sticky1");
  } 
  else {
    header3.classList.remove("sticky1");
  }
}

But in this code, Header 2 is going to top: 0 and then make it sticky with Header 1. I need while window will cross the slider then it will be sticky with Header 1.但是在这段代码中,Header 2 将top: 0 ,然后使其与 Header 1 粘在一起。我需要当窗口将穿过滑块时,它将与 Header 1 粘在一起。

Given code in codepen.io codepen.io 中的给定代码

https://codepen.io/blackbird36/pen/PowwyRJ https://codepen.io/blackbird36/pen/PowwyRJ

You can relay only on CSS via position:sticky;您只能通过position:sticky;在 CSS 上进行中继position:sticky;

example :例子 :

 .header { padding: 10px 16px; background: #555; color: #f1f1f1; z-index: 999999; } .sticky { position: sticky; top: 0; width: 100%; } .sticky1 { position: sticky; top: 40px; width: 100%; }
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/> <div class="container"> <header id="masthead2" class="header-default affix-top header sticky"> <div class="row"> <div class="header-menu col-sm-12 tm-table"> <ul class="nav main-menu col-md-10"> <li class="menu-item has-children"> <a href="index.html">Home</a> </li> <li class="menu-item has-children"> <a href="rooms.html">Rooms</a> </li> </ul> </div> </div> </header> <img src="https://dummyimage.com/600x400/000/fff" alt=""> <div class="header sticky1" id="myHeader"> <h2>My Header</h2> </div> <div class="content"> <h3>On Scroll Sticky Header</h3> <p>The header will stick to the top when you reach its scroll position.</p> <p>Scroll back up to remove the sticky effect.</p> <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p> <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p> <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p> <h3>On Scroll Sticky Header</h3> <p>The header will stick to the top when you reach its scroll position.</p> <p>Scroll back up to remove the sticky effect.</p> <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p> <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p> <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p> </div> </div>

You can check support on sticky here : https://caniuse.com/#search=sticky您可以在此处查看对粘性的支持: https : //caniuse.com/#search=sticky

https://developer.mozilla.org/en-US/docs/Web/CSS/position#sticky https://developer.mozilla.org/en-US/docs/Web/CSS/position#sticky

sticky The element is positioned according to the normal flow of the document, and then offset relative to its nearest scrolling ancestor and containing block (nearest block-level ancestor), including table-related elements, based on the values of top, right, bottom, and left. sticky元素按照文档的正常流向定位,然后相对于它最近的滚动祖先和包含块(最近的块级祖先)进行偏移,包括与表格相关的元素,基于top、right、bottom的值, 走了。 The offset does not affect the position of any other elements.偏移量不会影响任何其他元素的位置。

Please remove your jquery and replace below CSS请删除您的 jquery 并替换下面的 CSS

.header {
  padding: 10px 16px;
  background: #555;
  color: #f1f1f1;
  z-index: 999999;
  position: sticky;
   top: 0;
}

#myHeader{
  top: 44px;
}

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

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