javascript-如何使粘性元素“浮动”

前端之家收集整理的这篇文章主要介绍了javascript-如何使粘性元素“浮动” 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我遇到了一个怪异的布局问题,想不出办法解决这个问题.标题可能有些混乱,但让我展示代码.

#root-container {
  display: flex;
}

#container1 {
  width: 100px;
  height: 500px;
  background-color: grey;
}

#sticky-container {
  width: 320px;
  max-height: 500px;
  position: relative;
  background-color: lightgrey;
}

#sticky-container-header {
  width: 320px;
  height: 100px;
  background-color: #2f4d92;
}

#full-height-content {
  width: 100%;
  height: 400px;
  overflow-y: scroll;
  background-color: #d67e23;
}

#sticky-content {
  width: 80%;
  height: 100px;
  position: sticky;
  bottom: 20px;
  background-color: rgba(0,0.5);
}

#bottom {
  width: 420px;
  height: 100px;
  background-color: purple;
}
<div id='root-container'>
  <div id="container1"></div>
  <div id="sticky-container">
    <div id='sticky-container-header'></div>
    <div id='full-height-content'>
      Saw yet kindness too replying whatever marianne. Old sentiments resolution admiration unaffected its mrs literature. BehavIoUr new set existence dashwoods. It satisfied to mr commanded consisted disposing engrossed. Tall snug do of till on easy. Form not calm new fail. 

His followed carriage proposal entrance directly had elegance. Greater for cottage gay parties natural. Remaining he furniture on he discourse suspected perpetual. Power dried her taken place day ought the. Four and our ham west miss. Education shameless who middleton agreement how. We in found world chief is at means weeks smile. 

Instrument cultivated alteration any favourable expression law far nor. Both new like tore but year. An from mean on with when sing pain. Oh to as principles devonshire companions unsatiable an delightful. The ourselves suffering the sincerity. Inhabit her manners adapted age certain. Debating offended at branched striking be subjects. 

Must you with him from him her were more. In eldest be it result should remark vanity square. Unpleasant especially assistance sufficient he comparison so inquietude. Branch one shy edward stairs turned has law wonder horses. Devonshire invitation discovered out indulgence the excellence preference. Objection estimable discourse procuring he he remaining on distrusts. Simplicity affronting inquietude for now sympathize age. She meant new their sex could defer child. An lose at quit to life do dull. 

Surrounded affronting favourable no mr. Lain knew like half she yet joy. Be than dull as seen very shot. Attachment ye so am travelling estimating projecting is. Off fat address attacks his besides. Suitable settling mr attended no doubtful feelings. Any over for say bore such sold five but hung. 
Lose john poor same it case do year we. Full how way even the sigh. Extremely nor furniture fat questions now provision incommode preserved. Our side fail find like now. Discovered travelling for insensible partiality unpleasing impossible she. Sudden up my excuse to suffer ladies though or. Bachelor possible marianne directly confined relation as on he. 

Unpacked reserved sir offering bed judgment may and quitting speaking. Is do be improved raptures offering required in replying raillery. Stairs ladies friend by in mutual an no. Mr hence chief he cause. Whole no doors on hoped. Mile tell if help they ye full name. 

Cultivated who resolution connection motionless did occasional. Journey promise if it colonel. Can all mirth abode nor hills added. Them men does for body pure. Far end not horses remain sister. Mr parish is to he answer roused piqued afford sussex. It abode words began enjoy years no do no. Tried spoil as heart visit blush or. Boy possible blessing sensible set but margaret interest. Off tears are day blind smile alone had. 

Spot of come to ever hand as lady meet on. Delicate contempt received two yet advanced. Gentleman as belonging he commanded believing dejection in by. On no am winding chicken so behaved. Its preserved sex enjoyment new way behavIoUr. Him yet devonshire celebrated especially. Unfeeling one provision are smallness resembled repulsive. 

Entire any had depend and figure winter. Change stairs and men likely wisdom new
    </div>
    <div id='sticky-content'></div>
  </div>
</div>
<div id="bottom">
</div>

假设视口不够高,我们需要一直滚动到底部才能看到紫色块(底部div).

我想要的是:

>当我们从上到下滚动且紫色块尚未显示时,透明块会粘到橙色块底部上方20像素处.位置:粘性.
>当紫色块开始显示时,请阻止透明块向下移动,并将其保持在橙色块底部上方20px的位置,就好像它是一个浮动按钮一样.由于粘性元素为positioned according to the normal flow of the document,此操作将失败.因此,在这种情况下,当紫色块完全显示时,透明块位于橙色块底部下方20px.

我想知道是否有任何方法可以实现两者.如果我们仅用CSS就能实现这一点,那就太好了.但是简单的JS代码也是首选.谢谢!

最佳答案
一种想法是将粘性元素移动到容器的顶部而不是底部,并依靠负边距来避免正常的流动行为(当它在顶部时),然后在到达容器的底部时停止.

检查下面的评论以更好地理解.唯一的缺点是需要根据元素的高度手动设置一些值.您还将注意到顶部的值,也需要根据屏幕尺寸手动设置顶部的值,以模拟底部棒的行为.

#root-container {
  display: flex;
}

#container1 {
  width: 100px;
  height: 500px;
  background-color: grey;
}

#sticky-container {
  width: 320px;
  max-height: 500px;
  position: relative;
  background-color: lightgrey;
}

#sticky-container-header {
  width: 320px;
  height: 100px;
  background-color: #2f4d92;
}

#full-height-content {
  width: 100%;
  height: 400px;
  overflow-y: scroll;
  background-color: #d67e23;
  margin-top:-20px; /*same as the margin-bottom*/
}

#sticky-content {
  width: 80%;
  height: 100px;
  margin-top:-100px; /*same as height*/
  margin-bottom:20px; /*to avoid going under the bottom:20px we want*/
  position: sticky;
  top:calc(100vh - 100px - 20px);
  background-color: rgba(0,0.5);
}

#bottom {
  width: 420px;
  height: 100px;
  background-color: purple;
}

body {
  margin:0;
}
<div id='root-container'>
  <div id="container1"></div>
  <div id="sticky-container">
    <div id='sticky-container-header'></div>
    <div id='sticky-content'></div>
    <div id='full-height-content'>
      Saw yet kindness too replying whatever marianne. Old sentiments resolution admiration unaffected its mrs literature. BehavIoUr new set existence dashwoods. It satisfied to mr commanded consisted disposing engrossed. Tall snug do of till on easy. Form not calm new fail. 

His followed carriage proposal entrance directly had elegance. Greater for cottage gay parties natural. Remaining he furniture on he discourse suspected perpetual. Power dried her taken place day ought the. Four and our ham west miss. Education shameless who middleton agreement how. We in found world chief is at means weeks smile. 

Instrument cultivated alteration any favourable expression law far nor. Both new like tore but year. An from mean on with when sing pain. Oh to as principles devonshire companions unsatiable an delightful. The ourselves suffering the sincerity. Inhabit her manners adapted age certain. Debating offended at branched striking be subjects. 

Must you with him from him her were more. In eldest be it result should remark vanity square. Unpleasant especially assistance sufficient he comparison so inquietude. Branch one shy edward stairs turned has law wonder horses. Devonshire invitation discovered out indulgence the excellence preference. Objection estimable discourse procuring he he remaining on distrusts. Simplicity affronting inquietude for now sympathize age. She meant new their sex could defer child. An lose at quit to life do dull. 

Surrounded affronting favourable no mr. Lain knew like half she yet joy. Be than dull as seen very shot. Attachment ye so am travelling estimating projecting is. Off fat address attacks his besides. Suitable settling mr attended no doubtful feelings. Any over for say bore such sold five but hung. 
Lose john poor same it case do year we. Full how way even the sigh. Extremely nor furniture fat questions now provision incommode preserved. Our side fail find like now. Discovered travelling for insensible partiality unpleasing impossible she. Sudden up my excuse to suffer ladies though or. Bachelor possible marianne directly confined relation as on he. 

Unpacked reserved sir offering bed judgment may and quitting speaking. Is do be improved raptures offering required in replying raillery. Stairs ladies friend by in mutual an no. Mr hence chief he cause. Whole no doors on hoped. Mile tell if help they ye full name. 

Cultivated who resolution connection motionless did occasional. Journey promise if it colonel. Can all mirth abode nor hills added. Them men does for body pure. Far end not horses remain sister. Mr parish is to he answer roused piqued afford sussex. It abode words began enjoy years no do no. Tried spoil as heart visit blush or. Boy possible blessing sensible set but margaret interest. Off tears are day blind smile alone had. 

Spot of come to ever hand as lady meet on. Delicate contempt received two yet advanced. Gentleman as belonging he commanded believing dejection in by. On no am winding chicken so behaved. Its preserved sex enjoyment new way behavIoUr. Him yet devonshire celebrated especially. Unfeeling one provision are smallness resembled repulsive. 

Entire any had depend and figure winter. Change stairs and men likely wisdom new
    </div>
  </div>
</div>
<div id="bottom">
</div>
原文链接:https://www.f2er.com/html/530437.html

猜你在找的HTML相关文章