简体   繁体   English

确定何时在滚动父容器中查看子容器的百分比

[英]Determine when a percentage of a child container is in view in a scrolling parent container

I've been playing around with this on paper for an hour or two now but seem to be getting a bit stuck. 我已经在纸上玩了一两个小时,但是似乎有点卡住了。 Given the scenario where I have a parent container which is 100% wide (fits to it's parent width which can be any size in pixels) that contains child containers that are a percentage of it's size wide (for this example 90%), how do I determine when a child container is a percentage amount in view (say 80%)? 考虑到我有一个父容器的宽度为100%(适合其父宽度,可以是任意大小的像素)的情况,该子容器包含子容器的大小占其宽度的百分比(对于此示例为90%),该怎么办我可以确定子容器的可见时间百分比(例如80%)? The end result of this is to determine what child container you're currently at, say 1/4 or 3/4 etc. The amount of child containers is variable, it may sometimes be 1, it may be 100. 最终结果是确定您当前使用的子容器为1/4或3/4等。子容器的数量是可变的,有时可能是1,可能是100。

The child containers width of it's parent can vary, but they will always be the same for every child, for example, all 90% of the parent container, or all 100% width. 它的父容器的子容器宽度可以变化,但是对于每个子容器,它们始终是相同的,例如,所有90%的父容器或所有100%宽度。

I've drawn a rough diagram to hopefully make this a bit easier to understand. 我绘制了一个粗略的图表,希望可以使它更容易理解。

在此处输入图片说明

So in the example image above, we'd currently be at slide 1 of 3. But if we scrolled to the right and 80% of the green container was now in view, we'd be on slide 2 of 3 etc. The black container in this case being the parent. 因此,在上面的示例图片中,我们当前位于幻灯片1的3中。但是,如果我们向右滚动,现在看到80%的绿色容器,我们将位于幻灯片2的3等中。黑色在这种情况下,容器是父容器。

The resulting mathematical solution will be implemented in JavaScript. 最终的数学解决方案将在JavaScript中实现。

Basically, you want to take the current position of the slide container, and divide that by the width of the parent container. 基本上,您想获取幻灯片容器的当前位置,然后将其除以父容器的宽度。

Here's what that would look like: Math.round(Math.abs(currentPosition) / containerWidth); 如下所示:Math.round(Math.abs(currentPosition)/ containerWidth);

The lowest value possible is zero, which would correspond to the first slide. 可能的最低值为零,它将与第一张幻灯片相对应。

See my comment below for an example of this in action. 请参阅我的评论,以获取有关此操作的示例。

You can calculate the percentage of a child visible with a function like this: 您可以使用以下函数计算可见的孩子的百分比:

function calculateVisiblePercentage(childLeft, childWidth, containerOffset, containerWidth){

  // Position of the left side of the child, 
  // or the container left side if the child 
  // starts before the left side of the container
  var x0 = Math.max(containerOffset, childLeft);

  // Position of the right side of the child,
  // or the container right side if the child
  // ends after the right side of the container
  var x1 = Math.min(containerOffset + containerWidth, childLeft + childWidth);

  if (x1 < x0){
    // It's not visible
    return 0;
  }

  var pixelsVisible = x1 - x0;

  // Percentage
  return pixelsVisible * 100 / childWidth;
};

Where childLeft is the position of the child's left side. 其中childLeft是孩子左侧的位置。 For example, if the children width is 100px, it would be 0 for the first child, 100 for the second, etc. And where containerOffset is the position / scroll of the container. 例如,如果子级宽度为100px,则第一个子级为0,第二个子级为100,依此类推。其中containerOffsetcontainerOffset的位置/滚动。

You can see a basic working example in this JSFiddle . 您可以在此JSFiddle中看到一个基本的工作示例。

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

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