Js Implementation of the sliding effect of sharing to Page scrolling

Source: Internet
Author: User

Js Implementation of the sliding effect of sharing to Page scrolling

The page scroll up and down, and the shared module slides.

Key points:

The Code is as follows:

Var scrtop upload document.doc umentElement. scrollTop | document. body. scrollTop;
Var height = document.doc umentElement. clientHeight | document. body. clientHeight;
Var top = scrtop + (height-jb51.offsetHeight)/2;
Top = parseInt (top );


Obtain the vertical center position of the page

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

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

<! DOCTYPE html>

<Html>

<Head>

<Meta charset = "gb2312"/>

<Title> untitled document </title>

<Style>

Body {margin: 0; padding: 0; font: 12px/1.5 arial; height: 2000px ;}

# Jb51 {width: 100px; height: 200px; line-height: 200px;

Text-align: center; border: 1 p solid # ccc;

Background: # f5f5f5; position: absolute; left:-100px; top: 0 ;}

# Jb51_tit {position: absolute; right:-20px; top: 60px;

Width: 20px; height: 60px; padding: 10px 0;

Background: # 06c; text-align: center;

Line-height: 18px; color: # fff ;}

</Style>

<Script>

Window. onload = function (){

Var jb51 = document. getElementById ("jb51 ");

Jb51.onmouseover = function (){

Startrun (jb51, 0, "left ")

}

Jb51.onmouseout = function (){

Startrun (jb51,-100, "left ")

}

Window. onscroll = window. onresize = function (){

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

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

Var top = scrtop + (height-jb51.offsetHeight)/2;

Top = parseInt (top );

Startrun (jb51, top, "top ")

}

}

Var timer = null

Function startrun (obj, target, direction ){

ClearInterval (timer );

Timer = setInterval (function (){

Var speed = 0;

If (direction = "left "){

Speed = (target-obj.offsetLeft)/8;

Speed = speed> 0? Math. ceil (speed): Math. floor (speed );

If (obj. offsetLeft = target ){

ClearInterval (timer );

} Else {

Obj. style. left = obj. offsetLeft + speed + "px ";

}

}

If (direction = "top "){

Speed = (target-obj.offsetTop)/8;

Speed = speed> 0? Math. ceil (speed): Math. floor (speed );

If (obj. offsetTop = target ){

ClearInterval (timer );

} Else {

Obj. style. top = obj. offsetTop + speed + "px ";

}

Document. title = obj. offsetTop + ',' + target + ',' + speed;

}

}, 30)

}

</Script>

</Head>

<Body>

<Div id = "jb51">

Shared content

<Span id = "jb51_tit"> share to </span>

</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.