How to adjust the layer size by dragging the mouse in javascript

Source: Internet
Author: User

How to adjust the layer size by dragging the mouse in javascript

This example describes how to change the layer size by dragging the mouse in javascript. Share it with you for your reference. The specific implementation method is as follows:

?

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

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

<Html>

<Head>

<Title> drag to change the layer size </title>

<Meta content = "text/html; charset = gb2312" http-equiv = "Content-Type">

<Style> {

Box-sizing: border-box; moz-box-sizing: border-box

}

# TestDiv {background-color: buttonface; background-repeat: repeat;

Background-attachment: scroll; color: # rja5; height: 300px;

Left: 30px; overflow: hidden; width: 500; z-index: 2;

Border: 2px outset white; margin: 0px; padding: 2px;

Background-position: 0% 50%}

Body {font-family: Verdana; font-size: 9pt}

# InnerNice {background-color: white; background-repeat: repeat;

Background-attachment:

Scroll; color: #3969A5; height: 100%; overflow: auto;

Width: 100%; border: 2px inset white; padding: 8px;

Background-position: 0% 50%}

</Style>

<SCRIPT language = javascript>

Var theobject = null;

Function resizeObject (){

This. el = null; // pointer to the object

This. dir = ""; // type of current resize (n, s, e, w, ne, nw, se, sw)

This. grabx = null; // Some useful values

This. graby = null;

This. width = null;

This. height = null;

This. left = null;

This. top = null;

}

Function getDirection (el ){

Var xPos, yPos, offset, dir;

Dir = "";

XPos = window. event. offsetX;

YPos = window. event. offsetY;

Offset = 8; // The distance from the edge in pixels

If (yPos <offset) dir + = "n ";

Else if (yPos> el. offsetHeight-offset) dir + = "s ";

If (xPos <offset) dir + = "w ";

Else if (xPos> el. offsetWidth-offset) dir + = "e ";

Return dir;

}

Function doDown (){

Var el = getReal (event. srcElement, "className", "resizeMe ");

If (el = null ){

Theobject = null;

Return;

}

Dir = getDirection (el );

If (dir = "") return;

Theobject = new resizeObject ();

Theobject. el = el;

Theobject. dir = dir;

Theobject. grabx = window. event. clientX;

Theobject. graby = window. event. clientY;

Theobject. width = el. offsetWidth;

Theobject. height = el. offsetHeight;

Theobject. left = el. offsetLeft;

Theobject. top = el. offsetTop;

Window. event. returnValue = false;

Window. event. cancelBubble = true;

}

Function doUp (){

If (theobject! = Null ){

Theobject = null;

}

}

Function doMove (){

Var el, xPos, yPos, str, xMin, yMin;

XMin = 8; // The smallest width possible

YMin = 8; // height

El = getReal (event. srcElement, "className", "resizeMe ");

If (el. className = "resizeMe "){

Str = getDirection (el );

// Fix the cursor

If (str = "") str = "default ";

Else str + = "-resize ";

El. style. cursor = str;

}

// Dragging starts here

If (theobject! = Null ){

If (dir. indexOf ("e ")! =-1)

Theobject. el. style. width = Math. max (xMin, theobject. width + window. event. clientX-theobject. grabx) + "px ";

If (dir. indexOf ("s ")! =-1)

Theobject. el. style. height = Math. max (yMin, theobject. height + window. event. clientY-theobject. graby) + "px ";

If (dir. indexOf ("w ")! =-1 ){

Theobject. el. style. left = Math. min (theobject. left + window. event. clientX-theobject. grabx, theobject. left + theobject. width-xMin) + "px ";

Theobject. el. style. width = Math. max (xMin, theobject. width-window. event. clientX + theobject. grabx) + "px ";

}

If (dir. indexOf ("n ")! =-1 ){

Theobject. el. style. top = Math. min (theobject. top + window. event. clientY-theobject. graby, theobject. top + theobject. height-yMin) + "px ";

Theobject. el. style. height = Math. max (yMin, theobject. height-window. event. clientY + theobject. graby) + "px ";

}

Window. event. returnValue = false;

Window. event. cancelBubble = true;

}

}

Function getReal (el, type, value ){

Temp = el;

While (temp! = Null) & (temp. tagName! = "BODY ")){

If (eval ("temp." + type) = value ){

El = temp;

Return el;

}

Temp = temp. parentElement;

}

Return el;

}

Document. onmousedown = doDown;

Document. onmouseup = doUp;

Document. onmousemove = doMove;

</SCRIPT>

</Head>

<Body>

<Div class = "resizeMe" id = "testDiv">

<Div id = "innerNice">

<P align = "center"> </p>

<P align = "center">

Please drag the mouse at the border </p>

<P> </p>

<P> </p>

<P> </p>

</Div>

</Div>

</Body>

</Html>

I hope this article will help you design javascript programs.

Related Article

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.