让 div 在页面底部固定,当屏幕高度较小时,并在滚动时保持特定位置。

huangapple go评论67阅读模式
英文:

Make div fixed at the bottom of page with small height screen and take his specific position when scrolling

问题

我有一个用于编辑个人资料的网页,上面有一个保存设置按钮。页面很长,我希望保存设置按钮在页面底部保持固定。在向下滚动时,它会回到正常位置。我可以使其在滚动时保持在底部,但我无法使其在滚动时停留在正常位置。

在小高度的移动屏幕上的设计:

让 div 在页面底部固定,当屏幕高度较小时,并在滚动时保持特定位置。

在向下滚动时,我希望它粘在页脚前面:

让 div 在页面底部固定,当屏幕高度较小时,并在滚动时保持特定位置。

我在底部容器save-settings中添加了以下CSS,但这个样式会使它一直固定在底部。

.save-settings {
  box-shadow: 0px -2px 10px rgba(23, 29, 26, 0.1);
  padding: 14px 24px;
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  background-color: #fcfcfc;
  z-index: 100;
}

我认为这个问题与我的问题相似,但我无法理解,因为该网站示例不再有效。所以有人能以更详细的方式帮助我吗?

编辑

以下是我期望的示例代码:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.header {
  height: 100px;
  background-color: green;
}
.page-holder {
  height: 600px;
  background-color: red;
}
.save-settings {
  box-shadow: 0px -2px 10px rgba(23, 29, 26, 0.1);
  padding: 14px 24px;
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  background-color: #fcfcfc;
  z-index: 100;
}
button.primary-btn {
  padding: 12px 20px;
  border-radius: 10px;
  color: #fcfcfc;
  font-size: 14px;
  line-height: 21px;
  font-weight: 600;
  background: linear-gradient(100.96deg, #15c077 8.41%, #2dc015 88.4%);
  border: 0;
  width: 100%;
}

.footer {
  height: 300px;
  background-color: orange;
}

<!-- language: lang-html -->

<div class="header">
  <h2>Header</h2>
</div>
<div class="page-holder">
  <h1>Main Content</h1>
  <div class="save-settings">
    <button class="primary-btn">Save Setting</button>
  </div>
</div>
<div class="footer">
  <h2>Footer</h2>
</div>

<!-- end snippet -->

所以我希望save-settings位于页面底部,但在滚动时,它会粘在页脚前面,保持在它正常的DOM位置。

英文:

I have a save setting button for a profile edit web page. The page is long and I want the save setting button fixed at bottom of the page. On scrolling down it take her normal position. I can make it fixed at bottom on scrolling but I can't make it stick on the normal position when scrolling.

The design on small height mobile screen

让 div 在页面底部固定,当屏幕高度较小时,并在滚动时保持特定位置。

And on scrolling down, I expect to stick before the footer

让 div 在页面底部固定,当屏幕高度较小时,并在滚动时保持特定位置。

I add this css to the bottom container save-setting but this style make it fixed at the bottom all the time.

.save-settings {
  box-shadow: 0px -2px 10px rgba(23, 29, 26, 0.1);
  padding: 14px 24px;
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  background-color: #fcfcfc;
  z-index: 100;
}

I think this question is similar to my question but I can't understand because the website example isn't working anymore. So anybody can help me in more details.

Edit
This is a example code for what I expect

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.header {
  height: 100px;
  background-color: green;
}
.page-holder {
  height: 600px;
  background-color: red;
}
.save-settings {
  box-shadow: 0px -2px 10px rgba(23, 29, 26, 0.1);
  padding: 14px 24px;
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  background-color: #fcfcfc;
  z-index: 100;
}
button.primary-btn {
  padding: 12px 20px;
  border-radius: 10px;
  color: #fcfcfc;
  font-size: 14px;
  line-height: 21px;
  font-weight: 600;
  background: linear-gradient(100.96deg, #15c077 8.41%, #2dc015 88.4%);
  border: 0;
  width: 100%;
}

.footer {
  height: 300px;
  background-color: orange;
}

<!-- language: lang-html -->

&lt;div class=&quot;header&quot;&gt;
  &lt;h2&gt;Header&lt;/h2&gt;
&lt;/div&gt;
&lt;div class=&quot;page-holder&quot;&gt;
  &lt;h1&gt;Main Content&lt;/h1&gt;
  &lt;div class=&quot;save-settings&quot;&gt;
    &lt;button class=&quot;primary-btn&quot;&gt;Save Setting&lt;/button&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;footer&quot;&gt;
  &lt;h2&gt;footer&lt;/h2&gt;
&lt;/div&gt;

<!-- end snippet -->

So I want the save-settings at the bottom page but on scrolling it stick before the footer on his normal DOM position.

答案1

得分: 1

你可以使用position: sticky将页脚固定在底部,然后可以将内容放在其下面,使其保持在页脚上方。

.wrapper {
  height: 100vh;
  position: relative;
}

.content {
  background: green;
}

.save-settings {
  box-shadow: 0px -2px 10px rgba(23, 29, 26, 0.1);
  padding: 14px 24px;
  position: sticky;
  left: 0;
  bottom: 0;
  width: 100%;
  background-color: #fcfcfc;
  z-index: 100;
}

.under-settings {
  background: blue;
}
英文:

You can use position: sticky to fix the footer to the bottom, you can then place content below it, to have it stick to it.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.wrapper {
  height: 100vh;
  position: relative;
}

.content {
  background: green;
}

.save-settings {
  box-shadow: 0px -2px 10px rgba(23, 29, 26, 0.1);
  padding: 14px 24px;
  position: sticky;
  left: 0;
  bottom: 0;
  width: 100%;
  background-color: #fcfcfc;
  z-index: 100;
}

.under-settings {
  background: blue;
}

<!-- language: lang-html -->

&lt;div class=&quot;wrapper&quot;&gt;
  &lt;div class=&quot;content&quot;&gt;
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi vitae condimentum nisl. Morbi lorem felis, placerat vitae gravida sed, imperdiet eu ante. Vestibulum molestie felis ac felis hendrerit, vel tincidunt diam fermentum. Donec interdum eleifend
    faucibus. Mauris id magna pulvinar, tempus metus ut, rhoncus nisi. Aenean quis lectus est. Proin tortor velit, tristique sed congue nec, consequat non urna. Donec nibh purus, faucibus at lobortis eget, congue sed elit. Ut quis ex felis. Vivamus finibus
    lacinia interdum. Maecenas rutrum semper varius. Cras rutrum rhoncus augue ut sodales. Vivamus consectetur dignissim purus vitae suscipit. Fusce vitae nibh condimentum, rhoncus erat ut, fringilla tellus. Vestibulum vel ligula ac arcu pharetra euismod
    id vitae magna. Nam mauris turpis, iaculis nec varius eget, varius et magna.
    &lt;br /&gt;&lt;br /&gt; Morbi lobortis nunc ipsum, sed ullamcorper est finibus sit amet. Proin in erat sapien. Morbi semper feugiat ipsum ac facilisis. Donec feugiat sit amet odio in molestie. Nullam auctor lobortis sapien vel elementum. Lorem ipsum dolor sit
    amet, consectetur adipiscing elit. Fusce hendrerit, massa id convallis eleifend, neque lorem cursus lectus, non rhoncus sem odio efficitur lorem. Nulla ut massa sodales, tempus ipsum vitae, facilisis nulla. Pellentesque vulputate mauris ac volutpat
    dictum.
    &lt;br /&gt;&lt;br /&gt; Pellentesque lorem ipsum, posuere sed facilisis a, malesuada ut lorem. Vestibulum at sagittis metus, ut rhoncus ligula. Integer sit amet bibendum risus, sed tempor elit. Phasellus leo leo, elementum eget urna eleifend, fermentum semper
    urna. Vestibulum condimentum interdum sem, id dapibus leo ornare eu. Quisque lacus nunc, interdum in iaculis id, gravida non urna. Quisque pulvinar augue eget felis posuere, quis ultricies lectus pellentesque.
    &lt;br /&gt;&lt;br /&gt; Nunc vel dolor vulputate, commodo sem nec, placerat nulla. Nullam quis nibh tempus, iaculis purus in, congue nisi. Vestibulum id aliquam augue. Integer varius rutrum efficitur. Nunc sit amet elementum lectus. Praesent ut sodales ante.
    Nulla non posuere arcu, ut molestie lectus.
    &lt;br /&gt;&lt;br /&gt; Morbi vestibulum imperdiet eros eget fermentum. Duis suscipit malesuada lectus. Nam sagittis, est vel pulvinar iaculis, purus justo ultrices lacus, eget tincidunt massa ipsum id dolor. Mauris congue placerat lobortis. Sed non laoreet
    nulla. Aenean consequat ante ac gravida molestie. Sed nisi felis, auctor quis blandit nec, volutpat et ligula. Donec facilisis est a sapien lobortis vulputate.&lt;br /&gt;
  &lt;/div&gt;
  &lt;div class=&quot;save-settings&quot;&gt;
    Save Settings
  &lt;/div&gt;
  &lt;div class=&quot;under-settings&quot;&gt;
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi vitae condimentum nisl. Morbi lorem felis, placerat vitae gravida sed, imperdiet eu ante. Vestibulum molestie felis ac felis hendrerit, vel tincidunt diam fermentum. Donec interdum eleifend
    faucibus. Mauris id magna pulvinar, tempus metus ut, rhoncus nisi. Aenean quis lectus est. Proin tortor velit, tristique sed congue nec, consequat non urna. Donec nibh purus, faucibus at lobortis eget, congue sed elit. Ut quis ex felis. Vivamus finibus
    lacinia interdum. Maecenas rutrum semper varius. Cras rutrum rhoncus augue ut sodales. Vivamus consectetur dignissim purus vitae suscipit. Fusce vitae nibh condimentum, rhoncus erat ut, fringilla tellus. Vestibulum vel ligula ac arcu pharetra euismod
    id vitae magna. Nam mauris turpis, iaculis nec varius eget, varius et magna.
    &lt;br /&gt;&lt;br /&gt; Morbi lobortis nunc ipsum, sed ullamcorper est finibus sit amet. Proin in erat sapien. Morbi semper feugiat ipsum ac facilisis. Donec feugiat sit amet odio in molestie. Nullam auctor lobortis sapien vel elementum. Lorem ipsum dolor sit
    amet, consectetur adipiscing elit. Fusce hendrerit, massa id convallis eleifend, neque lorem cursus lectus, non rhoncus sem odio efficitur lorem. Nulla ut massa sodales, tempus ipsum vitae, facilisis nulla. Pellentesque vulputate mauris ac volutpat
    dictum.
    &lt;br /&gt;&lt;br /&gt; Pellentesque lorem ipsum, posuere sed facilisis a, malesuada ut lorem. Vestibulum at sagittis metus, ut rhoncus ligula. Integer sit amet bibendum risus, sed tempor elit. Phasellus leo leo, elementum eget urna eleifend, fermentum semper
    urna. Vestibulum condimentum interdum sem, id dapibus leo ornare eu. Quisque lacus nunc, interdum in iaculis id, gravida non urna. Quisque pulvinar augue eget felis posuere, quis ultricies lectus pellentesque.
    &lt;br /&gt;&lt;br /&gt; Nunc vel dolor vulputate, commodo sem nec, placerat nulla. Nullam quis nibh tempus, iaculis purus in, congue nisi. Vestibulum id aliquam augue. Integer varius rutrum efficitur. Nunc sit amet elementum lectus. Praesent ut sodales ante.
    Nulla non posuere arcu, ut molestie lectus.
    &lt;br /&gt;&lt;br /&gt; Morbi vestibulum imperdiet eros eget fermentum. Duis suscipit malesuada lectus. Nam sagittis, est vel pulvinar iaculis, purus justo ultrices lacus, eget tincidunt massa ipsum id dolor. Mauris congue placerat lobortis. Sed non laoreet
    nulla. Aenean consequat ante ac gravida molestie. Sed nisi felis, auctor quis blandit nec, volutpat et ligula. Donec facilisis est a sapien lobortis vulputate.&lt;br /&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

以下是翻译好的部分:

Make the .save-settings element a direct child of the body element.
In this case, the position: sticky; will work without adding any additional position property.

英文:

Following your case in details, it turns out that your HTML structure needs some tweaking:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.page-holder {
  height: 100vh;
}

.save-settings {
  text-align: center;
  border-top: 1px solid black;
  padding: 20px;
  position: sticky;
  bottom: 0;
  right: 0;
  left: 0;
}

<!-- language: lang-html -->

&lt;div class=&quot;header&quot;&gt;
  &lt;h2&gt;Header&lt;/h2&gt;
&lt;/div&gt;
&lt;div class=&quot;page-holder&quot;&gt;
  &lt;h1&gt;Main Content&lt;/h1&gt;
&lt;/div&gt;
&lt;div class=&quot;save-settings&quot;&gt;
  &lt;button class=&quot;primary-btn&quot;&gt;Save Setting&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;footer&quot;&gt;
  &lt;h2&gt;footer&lt;/h2&gt;
&lt;/div&gt;

<!-- end snippet -->

Make the .save-settings element a direct child of the body element.
In this case, the position: sticky; will work without adding any additional position property.

huangapple
  • 本文由 发表于 2023年2月8日 23:05:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定