在页面顶部插入内容而不滚动

发布于 2024-12-20 20:05:03 字数 190 浏览 2 评论 0原文

有没有一种方法可以让我在网页的开头插入内容而不会使页面给人向上滚动的印象。

我正在尝试实现类似无限滚动的东西,但我需要能够无限向上和向下滚动(我还在另一端卸载内容,以便滚动条不会变得无限小并且应用程序不会占用太多内存)。

我很高兴使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

囚你心 2024-12-27 20:05:03

在执行代码来创建元素之前,只需执行以下操作:

var scrollY = window.scrollY;

window.onscroll = function(){
    window.scrollTo(0, scrollY);
    window.onscroll = null;
};

请记住,如果您已经有一个 onscroll 函数,则在此之后您将需要重新分配该函数...

Before executing the code to create your element, simply do something like this:

var scrollY = window.scrollY;

window.onscroll = function(){
    window.scrollTo(0, scrollY);
    window.onscroll = null;
};

Keep in mind that, if you already have an onscroll function, you will need to reassign the function after this...

脸赞 2024-12-27 20:05:03

在我的例子中,布局是这样的:

<div class='container'>
   <div class='list'>
      product cards...
   </div>
   <button>Load more</button>
</div>
  1. 通过单击按钮,我想从服务器获取数据,使用该数据动态创建产品卡并将该卡添加到 .list
  2. 问题是,当动态卡添加到 DOM 时,屏幕会自动滚动,我只能看到最后添加的卡片,但看不到最先添加的卡片。
    我认为它对所有浏览器的默认行为(我可能是错的) - 如果在焦点元素浏览器滚动页面上方的 DOM 中添加内容以便焦点元素位于屏幕上。如果在焦点元素下方添加的内容未发生滚动,并且焦点元素也在屏幕上。
  3. 为了解决这个问题,我只需在将卡片添加到 DOM 之前添加类似 document.activeElement.blur() 的内容,一切都很好。

In my case layout was something like this:

<div class='container'>
   <div class='list'>
      product cards...
   </div>
   <button>Load more</button>
</div>
  1. By clicking on button I want fetch data from server, dynamically create product cards with that data and add this cards to .list
  2. Problem was that when dynamically cards added to the DOM, screen automaticaly scroll and I see only last added cards, but not first added.
    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.
  3. To solve this problem I just add something like document.activeElement.blur() before add cards to the DOM and all was fine.
请远离我 2024-12-27 20:05:03

您可以将物品放在反向容器的底部,而不是将它们放在容器的顶部。结果在视觉上是相同的。

为了实现这一点,您可以定义一个反向列弹性框,然后将项目附加到其底部(视觉上位于顶部)。

这是一个示例(在此处以 jsfiddle 形式提供: https://jsfiddle.net/u91cd38e/1/

const el = document.querySelector(".container");
let i = 3;

setInterval(() => el.innerHTML += `<div> Item ${i++} </div>`, 500);
.container {
  display: flex;
  flex-direction: column-reverse;
  height: 200px;
  width: 200px;
  overflow-y: auto;
}

.container div {
  flex-shrink: 0;
  height: 100px;
}
<div class="container">
   <div> Item 1 </div>
   <div> Item 2 </div>
   <div> Item 3 </div>
</div>

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/ )

const el = document.querySelector(".container");
let i = 3;

setInterval(() => el.innerHTML += `<div> Item ${i++} </div>`, 500);
.container {
  display: flex;
  flex-direction: column-reverse;
  height: 200px;
  width: 200px;
  overflow-y: auto;
}

.container div {
  flex-shrink: 0;
  height: 100px;
}
<div class="container">
   <div> Item 1 </div>
   <div> Item 2 </div>
   <div> Item 3 </div>
</div>

可是我不能没有你 2024-12-27 20:05:03

一种可能的想法是完全绕过滚动机制并自行完成。

基本上,使用 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.

最初的梦 2024-12-27 20:05:03

您可以在添加内容时使用 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).

孤檠 2024-12-27 20:05:03

我通过将容器的第一个元素保存在变量中解决了这个问题,然后在插入后调用“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文