Js simple click to return the top effect implementation method

Source: Internet
Author: User

Js simple click to return the top effect implementation method

When the page is particularly long, you must scroll back to the top of the page several times before returning to the top. If there is a "Back to Top" button in the bottom right corner of the page, click, you can return to the top, which is a good experience for users.

Implementation principle: When the page is loaded, the element is located in the lower-right corner of the page. When the page is rolling, the element remains in the lower-right corner. When the user clicks, the page returns to the top.

Key Aspect 1: document.doc umentElement. clientWidth | document. body. clientWidth; obtain the width of the visible area. Chrome compatibility is followed by compatibility with other browsers.

Key Aspect 2: oTop. style. left = screenw-oTop. offsetWidth + "px"; when loading a page, place the element on the rightmost side of the page, and subtract the width of the element from the visible area.

Key Aspect 3: oTop. style. top = screenh-oTop. offsetHeight + scrolltop + "px"; when the page is rolling, the Y coordinate position of the element is equal to the height of the visible area minus the height of the element, plus the scroll distance.

Key Aspect 4: document.doc umentElement. scrollTop = document. body. scrollTop = 0. When you click an element, set the page scroll distance to 0. Write the two to ensure compatibility.

Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

<! DOCTYPE html>

<Html>

<Head>

<Meta charset = "UTF-8"/>

<Title> untitled document </title>

<Style>

Body {margin: 0; padding: 0}

# To_top {width: 30px; height: 40px; padding: 20px; font: 14px/20px arial; text-align: center; background: # 06c; position: absolute; cursor: pointer; color: # fff}

</Style>

<Script>

Window. onload = function (){

Var oTop = document. getElementById ("to_top ");

Var screenw = document.doc umentElement. clientWidth | document. body. clientWidth;

Var screenh = document.doc umentElement. clientHeight | document. body. clientHeight;

OTop. style. left = screenw-oTop. offsetWidth + "px ";

OTop. style. top = screenh-oTop. offsetHeight + "px ";

Window. onscroll = function (){

Var scrolltop = document.doc umentElement. scrollTop | document. body. scrollTop;

OTop. style. top = screenh-oTop. offsetHeight + scrolltop + "px ";

}

OTop. onclick = function (){

Document.doc umentElement. scrollTop = document. body. scrollTop = 0;

}

}

</Script>

</Head>

<Body style = "height: 1000px;">

<H1> Back to Top

<Div id = "to_top"> return to the top </div>

</Body>

</Html>

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.