【问题标题】:scroll step is different in Firefox - with mouse wheelFirefox 中的滚动步骤不同 - 使用鼠标滚轮
【发布时间】:2021-03-18 03:15:39
【问题描述】:

我正在制作一个屏幕上有大量文本的网站,我希望能够同时滚动两个 div。所以这部分是有效的。 我实际上可以滚动 div,但 jumps#back div 上真的太大了。 (它们似乎实际上与窗口高度相同所以也许你需要截取全屏才能完全理解我的意思)。

更新:在与朋友进行一些测试后,问题似乎出在 windows 上的 firefox 上。它在 mac 和 linux 上运行良好。 here is a fiddle so you can see with a increased height

这里有两个 gif,所以你可能会看到奇怪的效果。 每次页面移动 = 在我的鼠标上向下滚动一个滚轮(它也适用于触控板,因为它不是鼠标滚轮)。

VS 当我在我的 css 中删除两个 overflow 之一时,我下面的黑色方块停止工作,但滚动再次正常:


重要编辑:这是 Firefox 中的一个问题,但它似乎在 chrome & Brave 中正常工作。尽管如此,我仍在寻找一种让它发挥作用的方法。

所以,我注意到当我在 css 中设置两个 overflows 时会发生这种情况,实际上,如果您删除一个,脚本将不再工作,但滚动错误也会停止。 以下是有 bug 的示例:

let back_innerHeight = $("#back").height()
let back_scrollHeight = document.querySelector("#back").scrollHeight
 
let front_innerHeight = $("#front").innerHeight()
let front_scrollHeight = $("#front")[0].scrollHeight

$("#back").on("scroll", function () {
  
  // Get how many pixels were scrolled on #back
  let back_scrolled = $(this).scrollTop()
  
  // Calculate the scrolled percentage
  let percentage_back = back_scrolled / (back_scrollHeight - back_innerHeight)
  
  // Calculate how many pixels to scroll on #front
  let scrollIT = (percentage_back * (front_scrollHeight - front_innerHeight))
  
  // Just to validate that the percentage is applied correctly...
  let percentage_front = scrollIT / (front_scrollHeight - front_innerHeight)
  
  
  // Apply the scroll
  $("#front").scrollTop(scrollIT);
});

window.onresize = function(){
  back_innerHeight = $("#back").height()
  back_scrollHeight = document.querySelector("#back").scrollHeight
  front_innerHeight = $("#front").innerHeight()
  front_scrollHeight = $("#front")[0].scrollHeight
}
.scroll {
  position: absolute;
  display: block;
  top: 0;
  height: 100%;
}
#front {
  overflow: hidden;
  position: absolute;
  background-color: black;
  color: white;
  right: 0;
  left: 0;
  bottom: 0;
  top: 0;
  margin: auto auto auto auto;
  width: 25%;
  height: 35%;
  font-size: 3rem;
}
#back {
  overflow: auto;
  font-size: 8rem;
}

p {
  margin: 0;
  padding: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class ="scroll" id="back">
  <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Autem, quam similique quibusdam libero voluptatum laboriosam, sunt possimus non nobis recusandae, excepturi ex voluptates! Neque veniam, sapiente magnam fuga unde autem.
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugiat consequatur consectetur laudantium voluptatibus, iusto molestiae fugit inventore rerum, sit sed dolor ratione perferendis beatae molestias. Asperiores odio mollitia quisquam voluptates.</p>
</div>

<div class ="scroll" id="front">
  <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Autem, quam similique quibusdam libero voluptatum laboriosam, sunt possimus non nobis recusandae, excepturi ex voluptates! Neque veniam, sapiente magnam fuga unde autem.
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugiat consequatur consectetur laudantium voluptatibus, iusto molestiae fugit inventore rerum, sit sed dolor ratione perferendis beatae molestias. Asperiores odio mollitia quisquam voluptates.</p>
</div>

【问题讨论】:

  • 只是我自己尝试的一个练习 :)
  • 我会在我的帖子中添加一个小提琴,你是对的。另外,我不太确定这与我的问题有什么关系...
  • 抱歉,我删除了我的 cmets,因为我认为它们没有帮助。
  • 我删除了 HTML 标签以添加 FIREFOX 一个...因为您的问题与 FF 行为有关。
  • 是的,你是对的,谢谢!

标签: javascript jquery css firefox scroll


【解决方案1】:

编辑

下面的内容不是问题的答案,现在更具体的是 FF 行为,而不是两个 div 之间的滚动同步。

用于滚动同步

更多的是关于一些数学......以及使用哪些值。

您必须计算已滚动内容与可滚动内容的百分比。这是关键!可滚动的是不是元素的整个高度,因为总有一部分是可见的。

这使得#back 需要考虑三个值:

  • A:div 的可见部分,您可以使用.innerHeight()
  • B:滚动像素的数量,使用.scrollTop() 获得
  • C:div 的全高,包括已经滚动的部分(顶部以上)和要滚动的部分(底部以下)。那就是scrollHeight 属性。

所以要得到正确的百分比,公式是:B/(C-A)

然后,在#front“可滚动像素”上使用这个百分比,这又是全高减去可见高度。

你去吧!

let back_innerHeight = $("#back").height()
let back_scrollHeight = document.querySelector("#back").scrollHeight
 
let front_innerHeight = $("#front").innerHeight()
let front_scrollHeight = $("#front")[0].scrollHeight

$("#back").on("scroll", function () {
  
  // Get how many pixels were scrolled on #back
  let back_scrolled = $(this).scrollTop()
  
  // Calculate the scrolled percentage
  let percentage_back = back_scrolled / (back_scrollHeight - back_innerHeight)
  
  // Calculate how many pixels to scroll on #front
  let scrollIT = (percentage_back * (front_scrollHeight - front_innerHeight))
  
  // Just to validate that the percentage is applied correctly...
  let percentage_front = scrollIT / (front_scrollHeight - front_innerHeight)
  
  console.log("Scrolled % BACK:", percentage_back, "FRONT:", percentage_front)
  
  // Apply the scroll
  $("#front").scrollTop(scrollIT);
});

window.onresize = function(){
  back_innerHeight = $("#back").height()
  back_scrollHeight = document.querySelector("#back").scrollHeight
  front_innerHeight = $("#front").innerHeight()
  front_scrollHeight = $("#front")[0].scrollHeight
}
div {
}
#front {
  overflow: hidden;
  position: fixed;
  background-color: black;
  color: white;
  right: 0;
  left: 0;
  bottom: 0;
  top: 0;
  margin: auto auto auto auto;
  width: 25%;
  height: 35%;
  font-size: 3rem;
}
#back {
  height: 95vh;
  overflow: auto;
  overflow-x: hidden;
  overflow-y: auto;
  font-size: 8rem;
}

p{
  margin:0;
  padding:0;
}

/* Just for this demo here... to limit the SO console's height */
.as-console-wrapper{
  height: 1.4em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="back">
  <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Autem, quam similique quibusdam libero voluptatum laboriosam, sunt possimus non nobis recusandae, excepturi ex voluptates! Neque veniam, sapiente magnam fuga unde autem.
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugiat consequatur consectetur laudantium voluptatibus, iusto molestiae fugit inventore rerum, sit sed dolor ratione perferendis beatae molestias. Asperiores odio mollitia quisquam voluptates.</p>

</div>

<div id="front">
  <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Autem, quam similique quibusdam libero voluptatum laboriosam, sunt possimus non nobis recusandae, excepturi ex voluptates! Neque veniam, sapiente magnam fuga unde autem.
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugiat consequatur consectetur laudantium voluptatibus, iusto molestiae fugit inventore rerum, sit sed dolor ratione perferendis beatae molestias. Asperiores odio mollitia quisquam voluptates.</p>
</div>

一些文档:

【讨论】:

  • 嗨!非常感谢,但我可能会出错,对我来说,在 Firefox 上,页面仍然会跳转而不是逐行滚动......我会添加一个 gif。实际上它与名为front 的黑色方块以及与back 的同步无关。这只是我在 back 上定义它的高度和溢出后的滚动效果
  • mmm...我只是在FF下尝试了我的答案,没有看到任何特别的东西。我认为您的问题是从上到下完美同步滚动。 ;)
  • 我刚刚添加了一个 onresize 处理程序,因为全屏 sn-p 已关闭。
  • 谢谢,我想这对我以后会有帮助!我还用两张图片更新了我的帖子,这样你就可以看到鼠标滚轮在做什么和它应该做什么:)
  • 好的,我现在明白了……这似乎是一种 FF 行为。我发现要更改的所有内容都是关于更改设置...但这对 JS 来说是无法访问的。
猜你喜欢
  • 1970-01-01
  • 2012-07-09
  • 1970-01-01
  • 1970-01-01
  • 2013-04-20
  • 1970-01-01
  • 2012-10-27
  • 1970-01-01
  • 2011-09-29
相关资源
最近更新 更多