Overview
When doing mobile development, often encounter internal scrolling and external scrolling problems, the following I have a good summary of this, for the future development of reference, I believe that other people also useful.
Reference: A list of the differences between height, offsetheight, clientheight, ScrollHeight, Innerheight, and Outerheight
Internal scrolling and external scrolling
On the mobile side of the internal scrolling and external scrolling, as a conclusion:
- If scrolling is part of an external scroll (such as a container that can be scrolled within a normal page), scrolling does not trigger an external scroll when scrolling, and only when scrolling to the top or bottom will trigger an outer scroll.
- If inner scrolling is a layer or mask, scrolling inside will trigger an outer scroll. (This is scrolling through, similar to click through)
Problems brought about by
1. If internal scrolling is part of an external scroll, the scrolling experience itself is good. But sometimes you need to scroll to the top or bottom without triggering an external scroll .
2. If the inner scroll is a layer or mask, this time the experience is very poor, sometimes we need no matter at any time , the internal scrolling does not trigger external scrolling .
Workaround
For problem 2, it is usually solved by swiper.js plugin or slimescroll plugin .
For problem 1, can be solved manually, the following is the solution to the idea.
Use of the concept
- Pagey: Represents the position of the mouse pointer relative to the container. (Used to determine if the mouse is sliding up or down)
- scrollheight: Represents the height of the entire element, including the height of the scrolled content.
- clientheight: Represents the visual height of an element, excluding scrolling content, borders, or margins.
- offsetheight: Represents the height of the element, including padding and borders.
- scrolltop: Indicates the distance that the scroll bar scrolls.
So there are the following relationships:
ScrollHeight = clientheight + scrolltop
If there is no border, offsetheight = ClientHeight, then there are:
ScrollHeight = offsetheight + scrolltop
Depending on the equation above, you can tell if the slider is sliding to the top or bottom.
The Pagey of jquery
In general, Pagey is obtained through Event.pagey, but for some versions of jquery, you need to get pagey with the following code:
event.originalEvent.targetTouches[0].pageY
Code
Then the logic for detecting the top is:
- ScrollTop < 1
- Fingers still swipe up (note that sliding events are not blocked if you swipe down)
Then the logic to detect the bottom is:
- ScrollHeight-(offsetheight + scrolltop) < 1
- Fingers still swipe down (note that sliding events are not blocked if you swipe down)
Finally, how to stop the slide? You can use e.preventdefault () .
The code is well understood and is directly posted:
//上下边缘检测setScroll: (select) => { let $ele = $(select), eleStart; $ele.on('touchstart', function(e) { eleStart = e.originalEvent.targetTouches[0].pageY; }); $ele.on('touchmove', function(e) { let eleTouch = e.originalEvent.targetTouches[0].pageY - boxInfoStart, scrollVal1 = $ele.scrollTop(), scrollVal2 = $ele[0].scrollHeight - $ele[0].offsetHeight - $ele.scrollTop(); //向上滑到顶部 if(eleTouch>0 && scrollVal1 < 1) { e.preventDefault(); } //向下滑到底部 if(eleTouch<0 && scrollVal2 < 1) { e.preventDefault(); } });},
Other
The above code can also be used to solve the problem of dragging the top or bottom of the page with a black block on the mobile side.
Scrolling and external scrolling in the mobile side