我对如何做到这一点有些茫然。
我有一个网页。我希望用户往下滚动,然后在距离顶部特定的距离处,鼠标滚轮可以影响一个元素的位置(让它看起来像是在滚动)。当该元素达到某个位置(例如 top: -500)时,我希望滚动再次应用于主网页。您有什么想法吗?
我正在制作一个演示(fiddle),但没有任何进展,我将在有展示内容的情况下发布。
编辑:解决方案/伪代码的开端:https://jsfiddle.net/vk0jk37v/23/
附上一张我正在应用此方法的区域的图片。
//pageFeature.style.backgroundPosition = "0px " + parseInt(-y / 6) + 'px');
var element = document.getElementById('container').getBoundingClientRect();
var elementTop = element.top //distance from top 720;
// variable to stop function from being replayed on scroll when scrolling image
var isScrollingImage = false;
// disables scroll on body
var disableScroll = function() {
document.body.style.overflow='hidden';
}
// enables scroll on body
var enableScroll = function() {
document.body.style.overflow='auto';
}
//change position of background along y axis with scroll
var scrollImage = function() {
console.log("called");
isScrollingImage = true;
var pageFeature = document.getElementById("inner");
var pageFeaturePosition;
pageFeature.style.backgroundPositionY=parseInt(-scrollY / 10) + "px";
//if (background is scrolled to bottom) {
// enableScroll();
// }
}
//when element gets to center of viewport and animation is scroll is not on element
//call scrollImage()
function checkPosition() {
if (scrollY > 720 && !isScrollingImage) {
disableScroll();
scrollImage();
}
//isScrollingImage = false;
}
//once out of view port will have to bring the image back down,
//scroll image will only happen on the way down
document.addEventListener('scroll', checkPosition);