滚动Y到窗口位置

huangapple go评论77阅读模式
英文:

ScrollY to Window Position

问题

我正在寻求关于JS中scrollY的建议。

情况看起来是这样的:

我有一个元素,它通过window.scrollY进行缩放,并且我正在使用滚动平滑脚本。

不幸的是,这个脚本“减慢”了视口但没有减慢滚动值。

我希望这个可缩放的元素在视口位置上工作,而不是在滚动值上,因为现在它不是平滑缩放,只有在滚动滚轮时才“跳跃”到大小。

这是用于缩放对象的脚本:

const div = document.querySelector('#text-panel');

window.addEventListener('scroll', () => {
  console.log(window.scrollX)
  div.style.transform = `scale(${0 + window.scrollY/500})`;
});

这是我用于平滑滚动的脚本:
https://sudip-bhowmick.github.io/butterjs/butter.js

我应该使用什么来实现平滑缩放。

我曾尝试在CSS中使用一些过渡效果,但在这种类型的元素上不起作用。

此外,我发现了一些像“viewportOffset.top”之类的东西,但不知何故它不起作用。

英文:

I am looking for advice in JS about scrollY.

Situation is looking like that:

I have element that is scalling by window.scrollY, and I am using scroll smoothing script.

Unfortunetly this script "slows" viewport but not scroll value.

I want this scalable element to work on viewport position not on scroll value because right now it is not scalling smooth, it only "jumps" to size when moving scroll wheel.

This is script for scalling object:

    const div = document.querySelector('#text-panel');
    
    window.addEventListener('scroll', () => {
      console.log(window.scrollX)
      div.style.transform = `scale(${0 + window.scrollY/500})`;
    });

This is script I am using to smooth scrolling:
https://sudip-bhowmick.github.io/butterjs/butter.js

What should I use to change this to smooth scaling.

I was trying to use some transition in Css but it is not working on this type of element.

Additionaly i found something like "viewportOffset.top" but somehow it is not working.

答案1

得分: 0

CSS过渡:
CSS过渡可以用于实现平滑动画,无需复杂的JavaScript计算。

#text-panel {
    transition: transform 0.3s ease; /* 添加对transform属性的过渡效果 */
}

JavaScript缩放:
您可以使用JavaScript根据当前视口位置来更新元素的缩放比例。不使用window.scrollY,而是使用window.pageYOffset,它返回当前滚动位置的垂直像素值。

const div = document.querySelector('#text-panel');

window.addEventListener('scroll', () => {
    const viewportPosition = window.innerHeight - div.getBoundingClientRect().top;
    const scaleFactor = 1 + (viewportPosition / 500); // 根据需要调整缩放强度的除数值
    div.style.transform = `scale(${scaleFactor})`;
});

viewportPosition计算了元素在视口中可见的部分。元素距离视口顶部越近,viewportPosition就越大。然后,scaleFactor根据这个位置调整缩放比例。

英文:

If you want to achieve smooth scaling of an element based on the viewport position rather than the scroll value, you can use a combination of CSS transitions and JavaScript to create a smooth effect. Using a library like butter.js for smooth scrolling might conflict with the effect you're trying to achieve. Here's how you can do it:

CSS Transitions:
CSS transitions can be used to achieve smooth animations without the need for complex JavaScript calculations.

css

#text-panel {
    transition: transform 0.3s ease; /* Add transition to the transform property */
}

JavaScript for Scaling:
You can use JavaScript to update the scale of the element based on the current viewport position. Instead of using the window.scrollY, you can use window.pageYOffset which returns the vertical pixel value of the current scroll position.

javascript

const div = document.querySelector('#text-panel');

window.addEventListener('scroll', () => {
    const viewportPosition = window.innerHeight - div.getBoundingClientRect().top;
    const scaleFactor = 1 + (viewportPosition / 500); // Adjust the division value for scaling intensity
    div.style.transform = `scale(${scaleFactor})`;
});

viewportPosition calculates how much of the element is visible in the viewport. The closer the element is to the top of the viewport, the larger the viewportPosition will be. The scaleFactor then adjusts the scaling based on this position.

huangapple
  • 本文由 发表于 2023年8月10日 17:10:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定