我正在尝试创建一个无限向上和向下滚动的页面。
目前,我正在使用jQuery将内容从页面顶部重新定位到底部。这样当您向下滚动时就会创建一个很好的无缝循环,但我也希望在用户向上滚动时能够实现。
问题似乎是即使内容定位在页面的负垂直空间上,滚动也不会延伸到那个空间。据我所知,没有办法覆盖这个问题,所以我正在寻找某种解决方法。
我考虑使用JavaScript来禁用滚动,并使用滚动事件重新定位元素,但页面上已经有很多绝对定位的元素和动画效果,因此我担心这样做会影响性能。
还有其他的想法吗?
我正在尝试创建一个无限向上和向下滚动的页面。
目前,我正在使用jQuery将内容从页面顶部重新定位到底部。这样当您向下滚动时就会创建一个很好的无缝循环,但我也希望在用户向上滚动时能够实现。
问题似乎是即使内容定位在页面的负垂直空间上,滚动也不会延伸到那个空间。据我所知,没有办法覆盖这个问题,所以我正在寻找某种解决方法。
我考虑使用JavaScript来禁用滚动,并使用滚动事件重新定位元素,但页面上已经有很多绝对定位的元素和动画效果,因此我担心这样做会影响性能。
还有其他的想法吗?
好的...我解决了。
我改编了这个脚本,当你滚动到页面底部时,它会立即将滚动位置重新定位到页面顶部,并在到达页面顶部时将其定位到底部。
$(window).scroll(function() {
if ( $(window).scrollTop() >= 18830 ) {
$(window).scrollTop(201);
}
else if ( $(window).scrollTop() == 0 ) {
$(window).scrollTop(18629);
}
});
然后,我确保页面底部和顶部的内容完全相同。我认为当这种重定位发生时会有闪烁或其他东西,但它是平稳的!
首先,一些背景知识。为什么我们要伪造滚动条呢?
为了在加载大文档时保持响应性,CodeMirror不会渲染整个文档,而只会渲染当前滚动到视图中的部分。这意味着它创建的DOM节点数量受到视口大小的限制,并且由文本更改触发的浏览器重排相对便宜。
接下来是更深入的内容...
然后,它监听滚轮事件,但从不在这些事件上调用preventDefault或响应它们进行滚动。相反,它通过设置超时来响应滚轮事件,以观察滚动内容的像素量,并使用它来在运行时调整其增量到像素率。
将HTML body克隆两次(或三次)(使用JavaScript或其他方式)。从中间副本开始页面而不是顶部,然后您可以按照您喜欢的方式处理滚动。
正如许多人建议的那样,如果您的页面在顶部和底部看起来不完全相同,您需要克隆您的内容以使其看起来像是一样的。我使用了这种技术制作了一个示例,效果非常流畅:
/*
Ininite looping scroll.
Tested and works well in latest Chrome, Safari and Firefox.
*/
(function (window) {
'use strict';
var doc = document,
body = doc.body,
html = doc.documentElement,
startElement = doc.getElementsByClassName('is-start')[0],
clones = doc.getElementsByClassName('is-clone'),
disableScroll = false,
docHeight,
scrollPos,
clonesHeight,
i;
function getScrollPos() {
return (window.pageYOffset || html.scrollTop) - (html.clientTop || 0);
}
function getDocHeight() {
return Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
}
function getClonesHeight() {
i = 0;
clonesHeight = 0;
for (i; i < clones.length; i += 1) {
clonesHeight = clonesHeight + clones[i].offsetHeight;
}
return clonesHeight;
}
docHeight = getDocHeight();
clonesHeight = getClonesHeight();
window.addEventListener('resize', function () {
scrollPos = getScrollPos();
docHeight = getDocHeight();
clonesHeight = getClonesHeight();
if (scrollPos <= 0) {
window.scroll(0, 1); // Scroll 1 pixel to allow upwards scrolling.
}
}, false);
window.addEventListener('scroll', function () {
if (disableScroll === false) {
scrollPos = getScrollPos();
if (clonesHeight + scrollPos >= docHeight) {
// Scroll to the top when you’ve reached the bottom
window.scroll(0, 1); // Scroll 1 pixel to allow upwards scrolling.
disableScroll = true;
} else if (scrollPos <= 0) {
// Scroll to the top of the clones when you reach the top.
window.scroll(0, docHeight - clonesHeight);
disableScroll = true;
}
if (disableScroll) {
// Disable scroll-repositioning for a while to avoid flickering.
window.setTimeout(function () {
disableScroll = false;
}, 100);
}
}
}, false);
// Needs a small delay in some browsers.
window.setTimeout(function () {
if (startElement) {
// Start at the middle of the starting block.
window.scroll(0, Math.round(startElement.getBoundingClientRect().top + document.body.scrollTop - (window.innerHeight - startElement.offsetHeight) / 2));
} else {
// Scroll 1 pixel to allow upwards scrolling.
window.scroll(0, 1);
}
});
}(this));
section {
position: relative;
text-align: center;
height: 80vh;
}
.red {
background: #FF4136;
}
.green {
background: #2ECC40;
}
.blue {
background: #0074D9;
}
.orange {
background: rebeccapurple;
}
h1 {
margin: 0;
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 100%;
font-size: 5vw;
color: #fff;
text-transform: uppercase;
}
body {
font-family: "Avenir Next", Montserrat, Helvetica, sans-serif;
font-weight: normal;
font-size: 100%;
}
::scrollbar {
display: none;
}
<section class="green">
<h1>One</h1>
</section>
<section class="red">
<h1>For</h1>
</section>
<section class="blue">
<h1>All</h1>
</section>
<section class="orange">
<h1>And</h1>
</section>
<section class="blue">
<h1>All</h1>
</section>
<section class="red">
<h1>For</h1>
</section>
<!--
These following blocks are the same as the first blocks to get that looping illusion going. You need to add clones to fill out a full viewport height.
-->
<section class="green is-clone is-start">
<h1>One</h1>
</section>
<section class="red is-clone">
<h1>For</h1>
</section>
function onScroll(){
var SAFETY_MARGIN = 50,
scrollPos = $(this).scrollTop(),
docHeight = $(document.body).height(),
winHeight = $(window).height(),
firstDiv = $('body>div:first-child')[0],
lastDiv = $('body>div:last-child')[0],
lowerLimit = SAFETY_MARGIN,
higherLimit = docHeight - SAFETY_MARGIN;
// Scrolling too high
if( scrollPos <= lowerLimit ){
// Move content to top;
$(lastDiv).prependTo(document.body);
// Adjust scroll position to compensate
// for the new content at the top
$(window).scrollTop(scrollPos + $(lastDiv).height());
}
// Scrolling too low
else if( scrollPos + winHeight >= higherLimit ){
// Move content to bottom
$(firstDiv).appendTo(document.body);
// Adjust scroll position to compensate
// for the missing content at the top
$(window).scrollTop(scrollPos - $(firstDiv).height());
}
}
$(window).scroll(onScroll);
$(window).load(function(){
var $body = $(document.body);
$(window).scrollTop($body.height() / 2);
});
</script>
</head>
<body>
<div style="height: 600px; background-color: red"> </div>
<div style="height: 600px; background-color: green"> </div>
<div style="height: 600px; background-color: blue"> </div>
</body>
</html>