在页面顶部插入内容而不滚动
有没有一种方法可以让我在网页的开头插入内容而不会使页面给人向上滚动的印象。
我正在尝试实现类似无限滚动的东西,但我需要能够无限向上和向下滚动(我还在另一端卸载内容,以便滚动条不会变得无限小并且应用程序不会占用太多内存)。
我很高兴使用 javascript,我宁愿不使用库(我试图保持比库更轻的重量)。
有什么想法吗?
Is there a way that I can insert content at the beginning of a webpage without causing the page to give the impression of scrolling up.
I'm trying to implement something kind of like infinite scrolling but I need to be able to scroll up infinitely as well as down (I'm also unloading content on the other end so that the scroll bar doesn't become infinitesimal and the app doesn't take up too much memory).
I'm happy to use javascript, I'd rather not use a library (I'm trying to stay lighter weight than that).
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在执行代码来创建元素之前,只需执行以下操作:
请记住,如果您已经有一个
onscroll
函数,则在此之后您将需要重新分配该函数...Before executing the code to create your element, simply do something like this:
Keep in mind that, if you already have an
onscroll
function, you will need to reassign the function after this...在我的例子中,布局是这样的:
我认为它对所有浏览器的默认行为(我可能是错的) - 如果在焦点元素浏览器滚动页面上方的 DOM 中添加内容以便焦点元素位于屏幕上。如果在焦点元素下方添加的内容未发生滚动,并且焦点元素也在屏幕上。
document.activeElement.blur()
的内容,一切都很好。In my case layout was something like this:
I think its default behavior for all browsers (I may be wrong) - if content added in DOM above the focused element browser scroll page in order to focused element was on screen. If content added below the focused element scroll not happened and the focused element also on the screen.
document.activeElement.blur()
before add cards to the DOM and all was fine.您可以将物品放在反向容器的底部,而不是将它们放在容器的顶部。结果在视觉上是相同的。
为了实现这一点,您可以定义一个反向列弹性框,然后将项目附加到其底部(视觉上位于顶部)。
这是一个示例(在此处以 jsfiddle 形式提供: https://jsfiddle.net/u91cd38e/1/ )
Instead of putting items on top of a container, you can put them on the bottom of a reverse container. The result will be visually the same.
To achieve that you can define a reverse-column flexbox and then append items on the bottom of it (visually top).
Here's an example (available as jsfiddle here: https://jsfiddle.net/u91cd38e/1/ )
一种可能的想法是完全绕过滚动机制并自行完成。
基本上,使用
display:fixed
定位每个元素。然后,您可以使用负位置加载屏幕上方的元素。您必须同步文档的高度(只需添加空白),以便文档滚动条正确。然后,捕获滚动事件并调整页面内所有元素的固定位置。
我不确定它会有多顺利,但我相当确定您可以获得您想要的效果。
One possible idea is to bypass the scroll mechanism completely and do your own.
Basically, position every element with
display: fixed
. You can then load elements above the screen using negative positions.You'll have to sync the height of the document (just by adding white space) so the document scrollbars are correct. Then, you trap the scroll event and adjust the fixed positioning of all the elements within your page.
I'm not sure how smooth it will be, but I'm fairly certain you could get the effect you're looking for.
您可以在添加内容时使用
window.scrollBy(x, y)
向下滚动(您必须计算添加内容的高度)。You can use
window.scrollBy(x, y)
to scroll down when you add content (you have to calculate the height of what you add).我通过将容器的第一个元素保存在变量中解决了这个问题,然后在插入后调用“scrollIntoView”。对我来说看起来很光滑。
I solved it by saving the first element of the container in a variable and then after inserting I called "scrollIntoView" on it. Looks smooth to me.