Scrolling and external scrolling in the mobile side

Source: Internet
Author: User

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:

    1. 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.
    2. 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:

    1. ScrollTop < 1
    2. Fingers still swipe up (note that sliding events are not blocked if you swipe down)

Then the logic to detect the bottom is:

    1. ScrollHeight-(offsetheight + scrolltop) < 1
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.