Home > Web Front-end > JS Tutorial > How to synchronize multiple scroll bars with native JS

How to synchronize multiple scroll bars with native JS

php中世界最好的语言
Release: 2018-04-13 17:22:12
Original
1791 people have browsed it

This time I will show you how to synchronize multiple scroll bars with native JS. What are the precautions for synchronizing multiple scroll bars with native JS? . Here is a practical case. Let’s take a look. .

In some websites that support using markdown to write articles, the background writing page generally supports markdown instant preview, that is, the entire page is divided into There are two parts, the left half is the markdown text you input, and the right half instantly outputs the corresponding preview page. For example, the following is the markdown of the CSDN background writing page.

This article does not explain how to achieve this effect from 0 (it is very likely that will publish a separate article in the future,). Putting aside other things, just look at the two container elements on the left and right in the main body of the page, that is markdown Input box element and preview display box element

What this article will discuss is, when the content of the two container elements exceeds the height of the container, that is, when a scroll box appears, how to make the other element scroll when one of the container elements scrolls.

DOM structure

Since it is related to the scroll bar, first think of an attribute in

js that controls the height of the scroll bar: scrollTop. As long as you can control the value of this attribute, you can naturally control the scroll bar. It's scrolling.

For the following

DOM structure:

<p id="container">
 <p class="left"></p>
 <p class="right"></p>
</p>
Copy after login
Among them, the

.left element is the left half input box container element, the .right element is the right half display box container element, and .container is common to them. parent element.

Since overflow scrolling is required, you also need to set the corresponding style (only the key style, not all):

#container {
 display: flex;
 border: 1px solid #bbb;
}
.left, .right {
 flex: 1;
 height: 100%;
 word-wrap: break-word;
 overflow-y: scroll;
}
Copy after login
Then insert enough content into the

.left and .right elements to make scroll bars appear on them, which is the following effect: The

style comes out That's it. Now you can perform a series of operations on these

DOM.

First attempt

The general idea is to listen to the scrolling events of two container elements. When one of the elements is scrolling, get the value of the

scrollTop attribute of this element and set this value to the scrollTop of the other scrolling element. value is enough.

For example:

var l=document.querySelector('.left')
var r=document.querySelector('.right')
l.addEventListener('scroll',function(){
 r.scrollTop = l.scrollTop
})
Copy after login
It seems very good, but now I not only want the right side to scroll with the left side, but also want the left side to scroll with the right side, so I add the following code:

addEventListener('scroll',function(){
 r.scrollTop = l.scrollTop
})
Copy after login
It looks great, but nothing is that simple.

At this time, when you use the mouse wheel to scroll, you find that scrolling is a bit difficult. The scrolling of the two container elements seems to be blocked by something, making it difficult to scroll.

After careful analysis, the reason is very simple. When you scroll on the left, the scroll event on the left is triggered, so the right follows the scroll, but at the same time, the following scroll on the right is also scrolling, so the scroll on the right is also triggered, so the left also needs to Follow the right scroll...and then you enter a situation similar to mutual triggering, so you will find that scrolling is very difficult.

Solve the problem of scroll events being triggered at the same time

To solve the above problems, there are currently two solutions.

Replace the scroll event with the mousewheel event

Since the

scroll event will not only be triggered by active mouse scrolling, but also by changing the scrollTop of the container element, the active scrolling of the element is actually triggered by the mouse wheel, so can be The scroll event is replaced by an event that is sensitive to mouse scrolling rather than element scrolling: 'mousewheel', so the above listening code becomes:

addEventListener('mousewheel',function(){
  r.scrollTop = l.scrollTop
})
r.addEventListener('mousewheel',function(){
  l.scrollTop = r.scrollTop
})
Copy after login
It seems to be somewhat useful, but there are actually two problems.

When scrolling one of the container elements, although the other container element also scrolls, the scrolling is not smooth, and the height has an obvious instantaneous bounce

I searched around on the Internet and couldn't find anything related to the rolling frequency of the

wheel event. I speculate that this may be a feature of this event.

鼠标每次滚动基本上都并不是以 1px 为单位的,其最小单元远比 scroll 事件小的多,我用我的鼠标在 chrome 浏览器上滚动,每次滚过的距离都恰好是 100px ,不同的鼠标或者浏览器这个数值应该都是不一样的,而 wheel 事件其实真正监听的是鼠标滚轮滚过一个齿轮卡点的事件,这也就能解释为何会出现弹跳的现象了。一般来说,鼠标滚轮每滚过一个齿轮卡点,就能监听到一个 wheel 事件,从开始到结束,被鼠标主动滚动的元素已经滚动了 100px ,所以另外一个跟随滚动的容器元素也就瞬间跳动了 100px

而之所以上述 scroll 事件不会让跟随滚动元素出现瞬间弹跳,则是因为跟随滚动元素每次 scrollTop 发生变化时,其值不会有 100px 那么大的跨度,可能也没有小到 1px ,但由于其触发频率高,滚动跨度小,最起码在视觉上就是平滑滚动的了。

wheel 只是监听鼠标滚轮事件,但如果是用鼠标拖动滚动条,就不会触发此事件,另外的容器元素也就不会跟随滚动了

这个其实很好解决,用鼠标拖动滚动条肯定是能触发 scroll 事件的,而在这种情况下,你肯定能够很轻易地判断出这个被拖动的滚动条是属于哪个容器元素的,只需要处理这个容器的滚动事件,另外一个跟随滚动容器的滚动事件不做处理即可。

wheel 事件的兼容问题

wheel 事件是 DOM Level3 的标准事件,但是除了此事件之外,还有很多非标准事件,不同的浏览器内核使用不同的标准,所以可能还需要按情况来进行兼容,具体可见MDN MouseWheelEvent

实时判断

如果你难以忍受 wheel 的弹跳,以及各种兼容,那么其实还有另外的路可以走得通,依旧是 scroll 事件,只不过需要做一些额外的工作。

scroll 事件的问题在于,没有判断当前主动滚动的是哪一个容器元素,只要确定了主动滚动的容器元素,这事就好办了,例如上述使用 wheel 事件中,用鼠标拖动滚动条之所以能够使用 scroll 事件,就是因为能够很容易地确定当前主动滚动容器元素是哪一个。

所以,问题的关键在于,如何判断出当前主动滚动的容器元素,只要解决了这个问题,剩下的就很好办了。

不论是鼠标滚轮滚动还是鼠标按在滚动条上拖动滚动条滚动,都会触发 scroll 事件,并且这个时候,在坐标系 Z 轴上,鼠标的坐标肯定是位于滚动容器元素所占的面积之内的,也就是说,在 Z 轴上,鼠标肯定是悬浮或者位于滚动容器元素之上。

鼠标在屏幕上移动的时候,是可以获取到鼠标当前坐标的。

其中, clientXclientY 就是当前鼠标相对于视口的坐标,可以认为,只要这个坐标在某个滚动容器的范围内,则认为这个容器元素就是主动滚动容器元素,容器元素的坐标范围可以使用 getBoundingClientRect 进行获取。

下面是鼠标移动到 .left 元素中的示例代码:

if (e.clientX>l.left && e.clientX<l.right && e.clientY>l.top) {
  // 进入 .left元素中
}
Copy after login

这样确实是可以的,不过考虑到两个滚动容器元素几乎占据了整个屏幕面积,所以 mousemove 所要监听的面积未免有点大,对于性能可能要求较高,所以其实可以换成 mouseover 事件,只需要监听鼠标有没有进入到某个滚动容器元素即可,也省去上述的坐标判断了。

addEventListener('mouseover',function(){
 // 进入 .left滚动容器元素内
})
Copy after login

当确定了鼠标主动滚动的容器元素是哪一个时,只需要处理这个容器的滚动事件,另外一个跟随滚动容器的滚动事件不做处理即可。嗯,效果很不错,性能也很好, perfect ,可以收工喽~

按比例滚动

上述示例全部是在两个滚动容器元素的内容高度完全一致的情况下的效果,如果这两个滚动容器元素的内容高度不同呢?

可见,由于两个滚动容器元素的内容高度不同,所以最大的 scrollTop 也就不同,就会出现当其中一个 scrollTop 值较小的元素滚到底时,另外一个元素还停留在一半,或者当其中一个 scrollTop 值较大的元素才滚到一半时,另外一个元素就已经滚到底了。

这种情况很常见,例如你用 markdown 写作时,一个一级标题标记 #编辑模式下占用的高度,一般都是小于预览模式占用的高度的,这样就出现了左右两侧滚动高度不一致的情况。

所以,如果将这种情况也考虑进来的话,那么就不能简单地为两个滚动容器元素相互设置 scrollTop 值那么简单。

虽然无法固定住滚动容器内容的高度,但是有一点可以确定,滚动条最大滚动高度,或者说 scrollTop 的值,肯定是与滚动容器内容的高度与滚动容器本身的高度呈一定的关系。

由于需要知道滚动容器内容的高度,还要存在滚动条,所以需要给此容器元素加个子元素,子元素高度不限,就是滚动容器内容的高度,容器高度固定,溢出滚动即可。

<p id="container">
 <p class="left">
	 <p class="child"></p>
 </p>
 <p class="right">
	 <p class="child"></p>
 </p>
</p>
Copy after login

通过我的观察推论与实践验证,已经确定下来了它们之间的关系,很简单,就是最基本的加减法运算:

滚动条的最大滚动高度(scrollTopMax) = 滚动容器内容的高度(即子元素高度ch) - 滚动容器本身的高度(即容器元素高度ph)
Copy after login

也就是说,如果已经确定了滚动容器内容的高度(即子元素高度ch)与滚动容器本身的高度(即容器元素高度ph),那么就一定能确定滚动条的最大滚动高度( scrollTop ),而这两个高度值基本上都是可以获取到的,所以就能得到 scrollTop

因此,想要让两个滚动元素容器等比例上下滚动,即其中一个元素滚到头或者滚到底,另外一个元素也能对应滚到头和滚到底,那么只要得到这两个滚动容器元素之间的 scrollTop 最大值的比例( scale )就行了。

确定了 scale 之后,实时滚动时,只需要获取主动滚动容器元素的 scrollTop1 ,就能得到另外一个跟随滚动的容器元素对应的 scrollTop2

很顺滑~

小结

上述本上已经实现了需求,可能在实践过程中还需要根据实际情况来进行一定的修改,例如如果你编写一个 markdown 的在线编辑和预览页面,就需要根据输入内容的高度实时更新 scale 值,不过主体已经搞定,小修小改就没什么难度了。

另外,本文所述不仅是针对两个滚动容器元素的跟随滚动,同时也可扩展开来,更多的元素间的跟随滚动都是可以根据本文思路来实现的,本文只是为了方便讲解而具体到了两个元素上。

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

JS如何实现Ajax的请求函数

JS的一些隐式转换使用总结

The above is the detailed content of How to synchronize multiple scroll bars with native JS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template