简体   繁体   English

根据滚动位置设置div宽度

[英]Set div width based on scroll position

The code: http://codepen.io/anon/pen/EjrpMM 代码: http//codepen.io/anon/pen/EjrpMM

So, i'm working on an interesting problem. 所以,我正在研究一个有趣的问题。 I am working with a 2000px HTML document, that has a modal placed ontop of it. 我正在使用一个2000px的HTML文档,它有一个模态放在它上面。

The width of the div lightbox is 80%, and it's sitting positioned fixed. div灯箱的宽度为80%,坐姿固定。

The goal is, when scrolling down the page, to control the width of the div based on the scroll position. 当向下滚动页面时,目标是根据滚动位置控制div的宽度。 At the bottom of the page, it's only a third in size. 在页面底部,它只有三分之一。

I've had trouble figuring out the proper equation or formula for this, and was seeking help. 我很难找出适当的等式或公式,并且正在寻求帮助。

Currently, I've been trying to look at the window.pageYOffset, to add 2.5% to the div while increasing, and minus 2.5% when scrolling back up, to bring it back to it's 80% width. 目前,我一直在尝试查看window.pageYOffset,在增加时向div添加2.5%,在向上滚动时减去2.5%,使其恢复到80%的宽度。

However, something isn't working right. 但是,有些东西不能正常工作。 I was seeing if the community had any ideas to help solve the problem. 我看到社区是否有任何想法来帮助解决问题。

I'm not using any frameworks for this. 我没有使用任何框架。

Here's my javascript: 这是我的javascript:

var lightBox = document.getElementById('lightBox'),
count = 80,
num = window.pageYOffset;

document.addEventListener('scroll', function(e) {
  var offset = window.pageYOffset;

  num >= offset ? count += 2.5 : count -= 2.5;
  num = offset;

  lightBox.style.width = count + '%';
});

View the code here, in this codepen 在此codepen中查看此处的代码

http://codepen.io/anon/pen/EjrpMM http://codepen.io/anon/pen/EjrpMM

Thank you! 谢谢!

You just have to change 你只需要改变

+= 2.5 and -=2.5 to += 0.7 and -= 0.7

When I checked your code I did this and it worked. 当我检查你的代码时,我做了这个并且它有效。

Scroll event fired once on scroll independently on how much you've scrolled. 滚动事件在滚动时单独滚动一次。 Eg if you've scrolled 1px scrollbar, or scrolled 100px using mousewheel scroll event will be fired once. 例如,如果您滚动1px滚动条,或使用鼠标滚动滚动100px滚动事件将被触发一次。

So if you need stable results you will need to calculate your div width depending on scroll position ( window.pageYOffset ). 因此,如果需要稳定的结果,则需要根据滚动位置( window.pageYOffset )计算div宽度。

Check this codepen fork . 检查这个codepen fork I've assumed that in the end of page div width should be 50% . 我假设在页面末尾div宽度应为50%

Core part is: 核心部分是:

var lightBox = document.getElementById('lightBox');
var upperBound = 80;
var lowerBound = 50;

var tickValue = (upperBound - lowerBound) / window.innerHeight;

document.addEventListener('scroll', function(e) {
  var offset = window.pageYOffset;
  var count = upperBound - offset * tickValue;

  lightBox.style.width = count + '%';
});

Important note: for crossbrowser way to get innerHeight you can check this answer 重要提示:对于crossbrowser方式获取innerHeight您可以查看此答案

This is a simple equation. 这是一个简单的等式。 Let f : scroll |-> f(scroll) be the function that gives you the width of your div. f : scroll |-> f(scroll)是一个给你div宽度的函数。 You want f(0) = 0.8, f(2000)= 1/3 . 你想要f(0) = 0.8, f(2000)= 1/3

Let's say you want the progression to be linear so f(scroll) = a*scroll + b , you can easily deduce that b = 0.8 and a = (1/3 - 0.8)/2000 = -0.000233 . 假设您希望渐进为线性,因此f(scroll) = a*scroll + b ,您可以轻松推断出b = 0.8a = (1/3 - 0.8)/2000 = -0.000233 Now for any scroll value, you can find the width of your div. 现在,对于任何滚动值,您都可以找到div的宽度。

Now you can change the values when you want, f(scroll) = (minWidth-maxWidth)/pageLength * scroll + maxWidth . 现在您可以根据需要更改值, f(scroll) = (minWidth-maxWidth)/pageLength * scroll + maxWidth

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

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