[英]Unstick a fixed element based on page scroll
I have a calculator on my site (written in php), and in the end of this calculator there is a div with the results. 我的网站上有一个计算器(用php编写),该计算器的末尾有一个div及其结果。
On desktop it works fine, but on mobile, i want to fix the results div to the bottom until scrolling the original position of the div. 在桌面上,它工作正常,但在移动设备上,我想将结果div固定在底部,直到滚动div的原始位置。 After that i want to change its position to static.
之后,我想将其位置更改为静态。
In other words, there is a fixed (to bottom) element on the page and after scrolling, at the original position of the div, i want to unstick it. 换句话说,页面上有一个固定(至底部)的元素,滚动后,在div的原始位置,我要取消粘贴。
I hope, its understandable, but there is an example page - Look at the MOBILE version!!! 我希望它是可以理解的,但是这里有一个示例页面 -看一下MOBILE版本!!!
How can i do that? 我怎样才能做到这一点?
Thanks in advance! 提前致谢!
Here's how I've done it in the past: 这是我过去的做法:
Example: 例:
// Get the document element to use later. var doc = document.documentElement; // Get your important message. var importantMsg = document.getElementById('important-msg'); // Find out how far the bottom of our message is from the top of the page while it's not fixed. var originalMsgBottom = importantMsg.getBoundingClientRect().bottom; // Get the window height. var windowHeight = Math.max(doc.clientHeight, window.innerHeight || 0); // Get the scroll position we need to check for while srolling. var scrollPosition = originalMsgBottom - windowHeight; // Now make your message fixed by default. importantMsg.className = 'important-msg-fixed'; // Create the function to get the current scroll and see if it is greater than or equal to the position we defined earlier. var scrollFunction = function() { var scrollTop = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0); if (scrollTop >= scrollPosition) { importantMsg.className = ''; // If it is, remove the fixed class. } else { importantMsg.className = 'important-msg-fixed'; // If it's not, then add it. } } // Make our function run everytime the window is scrolled. window.addEventListener('scroll', scrollFunction);
#important-msg { background: #f00; color: #fff; padding: 10px; } .important-msg-fixed { position: fixed; right: 0; bottom: 0; left: 0; margin: 0; }
<h1>Lorem Ipsum</h1> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p>Scroll Down</p> <p id="important-msg">Important Message</p> <p>More Stuff</p> <p>More Stuff</p> <p>More Stuff</p> <p>More Stuff</p> <p>More Stuff</p> <p>More Stuff</p> <p>More Stuff</p> <p>More Stuff</p> <p>More Stuff</p>
If you don't care about IE browsers (or if you're reading this in 2022 :D) 如果您不在乎IE浏览器(或者如果您在2022年阅读此内容:D)
you can use position: sticky
您可以使用的
position: sticky
.sticky{ position: sticky; bottom: 0; background: red; }
<div class="content"> <p style="border:4px dashed;height:150vh;">Scroll down</p> <div class="sticky">I'M STICKY</div> </div> <div class="content"> <p style="border:4px dashed;height:150vh;">Scroll up</p> </div>
https://caniuse.com/#feat=css-sticky https://caniuse.com/#feat=css-sticky
If you want a ( kind-of? ) fallback you could: 如果您想要( 某种? )后备,您可以:
.sticky{
position: absolute; /* fallback to absolute */
width: 100%; /* fallback width */
position: sticky;
bottom: 0;
background: red;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.