Javascript to implement drag-and-color and close Layer window instances

Source: Internet
Author: User

Javascript to implement drag-and-color and close Layer window instances

The example in this article describes how to implement drag-and-color and close layer windows in javascript. Share it with you for your reference. The specific analysis is as follows:

This is a javascript + CSS Implementation Layer-based Drag code, the difference is that it will change color when dragging the window, so that the operation experience is better, you can run the code to view the effect. It can also display/hide or close the open effect. When it's okay, repeat it.

?

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

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

<Html>

<Head>

<Title> drag a window </title>

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

<Style type = 'text/css '>

<! --

Body {font-size: 12px ;}

A: visited {text-decoration: none; color: slategray ;}

A: hover {text-decoration: underline; color: slategray ;}

A: link {text-decoration: none; color: slategray ;}

-->

</Style>

<Script language = JScript>

<! --

Var x0 = 0, y0 = 0, x1 = 0, y1 = 0;

Var offx = 6, offy = 6;

Var moveable = false;

Var hover = 'Orange ', normal =' #336699 '; // color;

Var index = 10000; // z-index;

// Start dragging;

Function startDrag (obj)

{

If (event. button = 1)

{

// Lock the title bar;

Obj. setCapture ();

// Define the object;

Var win = obj. parentNode;

Var sha = win. nextSibling;

// Record the mouse and layer position;

X0 = event. clientX;

Y0 = event. clientY;

X1 = parseInt (win. style. left );

Y1 = parseInt (win. style. top );

// Record the color;

Normal = obj. style. backgroundColor;

// Change the style;

Obj. style. backgroundColor = hover;

Win. style. borderColor = hover;

Obj. nextSibling. style. color = hover;

Sha. style. left = x1 + offx;

Sha. style. top = y1 + offy;

Moveable = true;

}

}

// Drag;

Function drag (obj)

{

If (moveable)

{

Var win = obj. parentNode;

Var sha = win. nextSibling;

Win. style. left = x1 + event. clientX-x0;

Win. style. top = y1 + event. clientY-y0;

Sha. style. left = parseInt (win. style. left) + offx;

Sha. style. top = parseInt (win. style. top) + offy;

}

}

// Stop dragging;

Function stopDrag (obj)

{

If (moveable)

{

Var win = obj. parentNode;

Var sha = win. nextSibling;

Var msg = obj. nextSibling;

Win. style. borderColor = normal;

Obj. style. backgroundColor = normal;

Msg. style. color = normal;

Sha. style. left = obj. parentNode. style. left;

Sha. style. top = obj. parentNode. style. top;

Obj. releaseCapture ();

Moveable = false;

}

}

// Obtain the focus;

Function getFocus (obj)

{

If (obj. style. zIndex! = Index)

{

Index = index + 2;

Var idx = index;

Obj. style. zIndex = idx;

Obj. nextSibling. style. zIndex = idx-1;

}

}

// Minimize;

Function min (obj)

{

Var win = obj. parentNode. parentNode;

Var sha = win. nextSibling;

Var tit = obj. parentNode;

Var msg = tit. nextSibling;

Var flg = msg. style. display = "none ";

If (flg)

{

Win. style. height = parseInt (msg. style. height) + parseInt (tit. style. height) + 2*2;

Sha. style. height = win. style. height;

Msg. style. display = "block ";

Obj. innerHTML = "0 ";

}

Else

{

Win. style. height = parseInt (tit. style. height) + 2*2;

Sha. style. height = win. style. height;

Obj. innerHTML = "2 ";

Msg. style. display = "none ";

}

}

// Create an object;

Function xWin (id, w, h, l, t, tit, msg)

{

Index = index + 2;

This. id = id;

This. width = w;

This. height = h;

This. left = l;

This. top = t;

This. zIndex = index;

This. title = tit;

This. message = msg;

This. obj = null;

This. bulid = bulid;

This. bulid ();

}

// Initialization;

Function bulid ()

{

Var str = ""

+ "<Div id = xMsg" + this. id + ""

+ "Style = '"

+ "Z-index:" + this. zIndex + ";"

+ "Width:" + this. width + ";"

+ "Height:" + this. height + ";"

+ "Left:" + this. left + ";"

+ "Top:" + this. top + ";"

+ "Background-color:" + normal + ";"

+ "Color:" + normal + ";"

+ "Font-size: 8pt ;"

+ "Font-family: Tahoma ;"

+ "Position: absolute ;"

+ "Cursor: default ;"

+ "Border: 2px solid" + normal + ";"

+ "'"

+ "Onmousedown = 'getfocus (this) '>"

+ "<Div"

+ "Style = '"

+ "Background-color:" + normal + ";"

+ "Width:" + (this. width-2*2) + ";"

+ "Height: 20 ;"

+ "Color: white ;"

+ "'"

+ "Onmousedown = 'startdrag (this )'"

+ "Onmouseup = 'stopdrag (this )'"

+ "Onmousemove = 'drag (this )'"

+ "Ondblclick = 'min (this. childNodes [1]) '"

+ ">"

+ "<Span style = 'width:" + (this. width-2*12-4) + "; padding-left: 3px; '>" + this. title + "</span>"

+ "<Span style = 'width: 12; border-width: 0px; color: white; font-family: webdings; 'onclick = 'min (this) '> 0 </span>"

+ "<Span style = 'width: 12; border-width: 0px; color: white; font-family: webdings; 'onclick = 'showhide (\" "+ this. id + "\", null) '> r </span>"

+ "</Div>"

+ "<Div style = '"

+ "Width: 100% ;"

+ "Height:" + (this. height-20-4) + ";"

+ "Background-color: white ;"

+ "Line-height: 14px ;"

+ "Word-break: break-all ;"

+ "Padding: 3px ;"

+ "'>" + This. message + "</div>"

+ "</Div>"

+ "<Div id = xMsg" + this. id + "bg style = '"

+ "Width:" + this. width + ";"

+ "Height:" + this. height + ";"

+ "Top:" + this. top + ";"

+ "Left:" + this. left + ";"

+ "Z-index:" + (this. zIndex-1) + ";"

+ "Position: absolute ;"

+ "Background-color: black ;"

+ "Filter: alpha (opacity = 40 );"

+ "'> </Div> ";

Document. body. insertAdjacentHTML ("beforeEnd", str );

}

// Display the hidden window

Function ShowHide (id, dis ){

Var bdisplay = (dis = null )? (Document. getElementById ("xMsg" + id). style. display = "")? "None": ""): dis

Document. getElementById ("xMsg" + id). style. display = bdisplay;

Document. getElementById ("xMsg" + id + "bg"). style. display = bdisplay;

}

// -->

</Script>

<Script language = 'jscript'>

<! --

Function initialize ()

{

Var a = new xWin ("1", 160,200,200,200, "Window 1", "xWin Demo ");

Var B = new xWin ("2", 240,200,100,100, "window 2", "Welcome ");

Var c = new xWin ("3", 200,160,250, 50, "window 3", "Copyright ");

ShowHide ("1", "none"); // hide window 1

}

Window. onload = initialize;

// -->

</Script>

</Head>

<Base target = "_ blank">

<Body onselectstart = 'Return false' oncontextmenu = 'Return false'>

<A onclick = "ShowHide ('1', null); return false;" href = ""> Windows 1 </a>

<A onclick = "ShowHide ('2', null); return false;" href = ""> Windows 2 </a>

<A onclick = "ShowHide ('3', null); return false;" href = ""> Windows 3 </a>

</Body>

</Html>

I hope this article will help you design javascript programs.

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.