简体   繁体   English

非图像div上的无限垂直滚动

[英]infinite vertical scroll on a non-image div

I'm using a library called ParticlesJS for part of the background of my website - this library dynamically generates a canvas element sized according to its parent, and fills it with animated particle effects, creating a neat effect. 我在网站背景的一部分中使用了一个名为ParticlesJS的库-该库动态生成一个根据其父级调整大小的画布元素,并用动画粒子效果填充它,从而创建一个整洁的效果。 With that said, I have run into some practical issues when trying to use it as the background: 话虽如此,当尝试使用它作为背景时,我遇到了一些实际问题:

  1. If the canvas element is the same size as the content, the visuals become pixelated and distorted if the height changes, such as with the addition of new content. 如果canvas元素与内容的大小相同,则在高度变化(例如添加新内容)时,视觉效果会变得像素化并失真。 Reloading the library is not a solution to this as it creates a visually distracting effect. 重新加载库不是解决方案,因为它会产生视觉干扰的效果。
  2. If the canvas element is an arbitrary extreme height and not sized according to the content (with the overflow simply hidden), the performance of the website suffers, as the library consumes excessive CPU power. 如果canvas元素是一个任意的极端高度,并且没有根据内容调整大小(只是隐藏了溢出部分),则该网站的性能会受到影响,因为该库会消耗过多的CPU能力。
  3. If the canvas element is simply given a fixed position in CSS, performance is good and it sticks, but it looks out of place as everything behind it moves during scrolling. 如果将canvas元素简单地在CSS中fixed位置,则性能会很好并且会保持不变,但是在滚动过程中,其后面的所有内容都会移动,因此看起来不适当。

After some consideration, it seems like the best way to make it work is to give it a modest size (like 200% page height), and then make it repeat infinitely during scrolling - performance would be acceptable, and there wouldn't be any distortion. 经过一番考虑,似乎使其最佳工作方式是给它适当的大小(例如200%的页面高度),然后使其在滚动过程中无限次重复-性能是可以接受的,并且不会有任何变化失真。 However, I can't find any way to do this - I'm aware that there's a background-repeat property in CSS, but that seems to only work for images. 但是,我找不到任何方法可以做到这一点-我知道CSS中有一个background-repeat属性,但这似乎仅适用于图像。

Is there any way to do what I'm trying to accomplish? 有什么方法可以做我想完成的事情吗? Both CSS and JS based answers are welcome. 欢迎使用基于CSS和JS的答案。

After some trial and error, it looks like the only means of accomplishing what I'm trying to do is as follows: 经过一番尝试和错误之后,看来完成我想做的唯一方法如下:

  1. Create 3 or so background divs, each the size of the view port, and stack them vertically 创建3个左右的div,每个div大小与视口大小相同,并垂直堆叠它们
  2. Record user scrolling activity, and set a trigger for when a user has scrolled a height equal to the height of the view port 记录用户的滚动活动,并为用户滚动的高度设置为等于视口高度的触发器
  3. when the trigger is hit, place the div that just left the view port at the end of the list, and insert an empty spacer div where it used to be 按下触发器后,将刚离开视口的div放在列表的末尾,并在以前的位置插入一个空的div间隔

If done correctly, this creates an effect where the user is apparently scrolling through an infinite background, when it's really just the same 3 or so divs being shuffled over and over. 如果做得正确,就会创建一种效果,用户似乎在无限的背景中滚动,而实际上恰好是3个div不断地重复播放。 Going in reverse is the same principle. 反向是相同的原理。

Not sure how to make this work with in a system that also has scroll position restoration, but it could probably be done by waiting for page loads and then dynamically inserting enough spacers to move the background divs to the appropriate position in the view port. 不确定如何在还具有滚动位置恢复的系统中使用此功能,但是可以通过等待页面加载,然后动态插入足够的间隔符将背景div移动到视口中的适当位置来完成。

The downside to using animated effects that rely on viewport dimensions is that the user may resize the browser and wreck your animation so you have no choice but to catch any viewport resizing in which case you may have to reload everything or recalculate! 使用依赖视口尺寸的动画效果的不利之处在于,用户可能会调整浏览器的大小并破坏动画,因此您别无选择,只能捕捉任何视口的大小,在这种情况下,您可能必须重新加载所有内容或重新计算!

You can't have the cake and the cherry on top unfortunately, so you'll either have to abandon the idea of "impressive effects" because they are impractical or take action... 不幸的是,您不能将蛋糕和樱桃放在首位,所以您要么因为不切实际而放弃“印象深刻的效果”,要么采取行动。

document.body.onresize=function(){Adjustments();};


function Adjustments(){

 var W=Container.offsetWidth, H=Container.offsetHeight;


 // You've now got the new resolution so go for your life!

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM