---
title: "拖曳层!"
author: "hipeace87"
date: "2008-10-10T13:15:00Z"
tags: []
categories: ["非技术"]
slug: "untitled"
draft: false
lastmod: "2009-02-05T09:22:11Z"
id: 323183
url: "https://hipeace87.iteye.com/blog/323183"
---

001. &lt;html&gt;
002. &lt;head&gt;
003. &lt;title&gt;&lt;/title&gt;
004. &lt;/head&gt;
005. &lt;body&gt;
006. &lt;p onclick = "ShowDivWin()"&gt;点下我
007. &lt;/body&gt;
008. &lt;script language="javascript" type="text/javascript"&gt;
009. /* @author Nieger Dai
010. @date 2007.11.15
011. \*/
012. var isIe = ( document.all ) ? true : false;
013. var moveable = false;
014. var topDivBorderColor = "#336699";
015. // 提示窗口的边框颜色
016. var topDivBgColor = "#6795B4";
017. // 提示窗口的标题的背景颜色
018. var contentBgColor = "white";
019. // 内容显示窗口的背景颜色
020. var contentFontColor = "black";
021. // 内容显示窗口字体颜色
022. var titleFontColor = "white";
023. // 弹出窗口标题字体颜色
024. var index = 10000;
025. // z - index;
026. // 创建弹出窗口，构造函数
027. function DivWindow( id, title, w, h, content )
028. {
029. this.id = id;
030. // 窗口id
031. this.zIndex = index + 2;
032. this.title = title;
033. // 弹出窗口名称
034. this.message = content;
035. // 弹出窗口内容
036. this.width = w;
037. // 弹出窗口宽度
038. this.height = h;
039. // 弹出窗口高度
040. this.left = ( document.body.clientWidth ) ? ( document.body.clientWidth - this.width ) / 2 : 0;
041. // 弹出窗口位置，距屏幕左边的位置
042. this.top = ( document.body.clientHeight ) ? ( document.body.clientHeight - this.height ) / 2 : 0;
043. // 弹出窗口位置，距屏幕上边的位置
044. // this.init = init;
045. // this.init();
046. }
047. //  ---------------------------------------
048. // 根据构造函数设定初始值，创建弹出窗口
049. DivWindow.prototype =
050. {
051. // 设置弹出窗口标题字体颜色
052. setPopupTopTitleFontColor : function( tFontColor )
053. {
054. titleFontColor = tFontColor;
055. }
056. ,
057. // 设置弹出窗口标题背景颜色
058. setPopupTopBgColor : function( tBgColor )
059. {
060. topDivBgColor = tBgColor;
061. }
062. ,
063. // 设置弹出窗口风格, 包括标题栏的背景，弹出窗口边框颜色，内容窗体背景颜色，内容窗体字体颜色
064. setPopupColor : function( borderColor, bgColor, tFontColor, cBgColor, fColor )
065. {
066. topDivBorderColor = borderColor;
067. topDivBgColor = bgColor;
068. titleFontColor = tFontColor;
069. contentBgColor = cBgColor;
070. contentFontColor = fColor;
071. }
072. ,
073. // 打开一个弹出窗口
074. open : function()
075. {
076. var sWidth, sHeight;
077. sWidth = document.body.clientWidth;
078. sHeight = document.body.clientHeight;
079. var bgObj = document.createElement( "div" );
080. bgObj.setAttribute( 'id', 'window' + this.id );
081. var styleStr = "top:0px;left:0px;position:absolute;background:#245;width:" + sWidth + "px;height:" + sHeight + "px;";
082. styleStr += ( isIe ) ? "filter:alpha(opacity=0);" : "opacity:0;";
083. bgObj.style.cssText = styleStr;
084. document.body.appendChild( bgObj );
085. // 让背景逐渐变暗
086. //showBackground( bgObj, 25 );
087. // 弹出窗口框体背景容器
088. var windowTopBgDiv = document.createElement( "div" );
089. windowTopBgDiv.setAttribute( 'id', 'windowTopBg' + this.id );
090. windowTopBgDiv.style.position = "absolute";
091. windowTopBgDiv.style.zIndex = this.zIndex ;
092. windowTopBgDiv.style.width = this.width ;
093. windowTopBgDiv.style.height = this.height;
094. windowTopBgDiv.style.left = this.left;
095. windowTopBgDiv.style.top = this.top;
096. windowTopBgDiv.style.background = topDivBgColor;
097. windowTopBgDiv.style.fontSize = "9pt";
098. windowTopBgDiv.style.cursor = "default";
099. windowTopBgDiv.style.border = "1px solid " + topDivBorderColor;
100. windowTopBgDiv.attachEvent( "onmousedown", function()
101. {
102. if( windowTopBgDiv.style.zIndex != index )
103. {
104. indexindex = index + 2;
105. var idx = index;
106. windowTopBgDiv.style.zIndex = idx;
107. }
108. }
109. );
110. // 弹出窗口头部框体
111. var windowTopDiv = document.createElement( "div" );
112. windowTopDiv.setAttribute( 'id', 'windowTop' + this.id );
113. windowTopDiv.style.position = "absolute";
114. windowTopDiv.style.background = topDivBgColor;
115. // "white";
116. windowTopDiv.style.color = titleFontColor;
117. windowTopDiv.style.cursor = "move";
118. windowTopDiv.style.height = 20;
119. windowTopDiv.style.width = this.width - 2 * 2;
120. // 开始拖动;
121. windowTopDiv.attachEvent( "onmousedown", function()
122. {
123. if( event.button == 1 )
124. {
125. // 锁定标题栏;
126. windowTopDiv.setCapture();
127. // 定义对象;
128. var win = windowTopDiv.parentNode;
129. // 记录鼠标和层位置;
130. x0 = event.clientX;
131. y0 = event.clientY;
132. x1 = parseInt( win.style.left );
133. y1 = parseInt( win.style.top );
134. // 记录颜色;
135. // topDivBgColor = windowTopDiv.style.backgroundColor;
136. // 改变风格;
137. // windowTopDiv.style.backgroundColor = topDivBorderColor;
138. win.style.borderColor = topDivBorderColor;
139. moveable = true;
140. }
141. }
142. );
143. // 停止拖动
144. windowTopDiv.attachEvent( "onmouseup", function()
145. {
146. if( moveable )
147. {
148. var win = windowTopDiv.parentNode;
149. win.style.borderColor = topDivBgColor;
150. windowTopDiv.style.backgroundColor = topDivBgColor;
151. windowTopDiv.releaseCapture();
152. moveable = false;
153. }
154. }
155. );
156. // 开始拖动
157. windowTopDiv.attachEvent( "onmousemove", function()
158. {
159. if( moveable )
160. {
161. var win = windowTopDiv.parentNode;
162. win.style.left = x1 + event.clientX - x0;
163. win.style.top = y1 + event.clientY - y0;
164. var objRith = parseInt(win.style.left,10) + parseInt(win.style.width,10);
165. var objButton = parseInt(win.style.top,10) + parseInt(win.style.height,10);
166. if(parseInt(win.style.left,10) &lt;= 0)
167. {
168. win.style.left = 1 ;
169. }
170. if(parseInt(win.style.top,10) &lt;= 0)
171. {
172. win.style.top = 1 ;
173. }
174. if ( objRith &gt;= document.body.clientWidth )
175. {
176. win.style.left = document.body.clientWidth - parseInt(win.style.width) - 1;
177. }
178. if ( objButton &gt;= document.body.clientHeight )
179. {
180. win.style.top = document.body.clientHeight - parseInt(win.style.height) - 1;
181. }
182. }
183. }
184. );
185. // 双击弹出窗口
186. windowTopDiv.attachEvent( "ondblclick", function()
187. {
188. maxOrMinPopupDiv( windowTopOperateSpan, windowContentDiv );
189. }
190. );
191. // 增加一个弹出窗口标题的显示
192. var windowTopTitleSpan = document.createElement( "span" );
193. windowTopTitleSpan.setAttribute( 'id', 'windowTopTitle' + this.id );
194. windowTopTitleSpan.style.width = this.width - 2 * 12 - 4;
195. windowTopTitleSpan.style.paddingLeft = "3px";
196. windowTopTitleSpan.innerHTML = this.title;
197. // 增加一个弹出窗口最小化，最大化的操作
198. var windowTopOperateSpan = document.createElement( "span" );
199. windowTopOperateSpan.setAttribute( 'id', 'windowTopOperate' + this.id );
200. windowTopOperateSpan.style.width = 12;
201. windowTopOperateSpan.style.borderWidth = "0px";
202. windowTopOperateSpan.style.color = titleFontColor;
203. // "white";
204. windowTopOperateSpan.style.fontFamily = "webdings";
205. windowTopOperateSpan.style.cursor = "default";
206. windowTopOperateSpan.innerHTML = "0";
207. // 最大化或者最小化弹出窗口操作
208. windowTopOperateSpan.attachEvent( "onclick", function()
209. {
210. maxOrMinPopupDiv( windowTopOperateSpan, windowContentDiv );
211. }
212. );
213. // 增加一个弹出窗口关闭的操作
214. var windowTopCloseSpan = document.createElement( "span" );
215. windowTopCloseSpan.setAttribute( 'id', 'windowTopClose' + this.id );
216. windowTopCloseSpan.style.width = 12;
217. windowTopCloseSpan.style.borderWidth = "0px";
218. windowTopCloseSpan.style.color = titleFontColor;
219. // "white";
220. windowTopCloseSpan.style.fontFamily = "webdings";
221. windowTopCloseSpan.style.cursor = "default";
222. windowTopCloseSpan.innerHTML = "r";
223. // 关闭窗口
224. windowTopCloseSpan.attachEvent( "onclick", function()
225. {
226. windowTopDiv.removeChild( windowTopTitleSpan );
227. windowTopDiv.removeChild( windowTopOperateSpan );
228. windowTopDiv.removeChild( windowTopCloseSpan );
229. windowTopBgDiv.removeChild( windowTopDiv );
230. windowTopBgDiv.removeChild( windowContentDiv );
231. document.body.removeChild( windowTopBgDiv );
232. document.body.removeChild( bgObj );
233. }
234. );
235. // 内容
236. var windowContentDiv = document.createElement( "div" );
237. windowContentDiv.setAttribute( 'id', 'windowContent' + this.id );
238. windowContentDiv.style.background = contentBgColor;
239. windowContentDiv.style.color = contentFontColor;
240. windowContentDiv.style.cursor = "default";
241. windowContentDiv.style.height = ( this.height - 20 - 6 );
242. windowContentDiv.style.width = "100%";
243. windowContentDiv.style.position = "relative";
244. windowContentDiv.style.left = 0;
245. windowContentDiv.style.top = 24;
246. windowContentDiv.style.lineHeight = "20px";
247. windowContentDiv.style.fontSize = "10pt";
248. windowContentDiv.style.wordBreak = "break-all";
249. windowContentDiv.style.padding = "3px";
250. windowContentDiv.innerHTML = this.message;
251. // 将内容写入到文件中
252. windowTopDiv.appendChild( windowTopTitleSpan );
253. windowTopDiv.appendChild( windowTopOperateSpan );
254. windowTopDiv.appendChild( windowTopCloseSpan );
255. windowTopBgDiv.appendChild( windowTopDiv );
256. windowTopBgDiv.appendChild( windowContentDiv );
257. document.body.appendChild( windowTopBgDiv );
258. }
259. }
260. //  ---------------------------------------
261. // 最大或者最小化弹出窗口
262. function maxOrMinPopupDiv( windowTopOperateSpan, windowContentDiv )
263. {
264. var win = windowTopOperateSpan.parentNode.parentNode;
265. var tit = windowTopOperateSpan.parentNode;
266. var flg = windowContentDiv.style.display == "none";
267. if( flg )
268. {
269. win.style.height = parseInt( windowContentDiv.style.height ) + parseInt( tit.style.height ) + 2 * 2;
270. windowContentDiv.style.display = "block";
271. windowTopOperateSpan.innerHTML = "0";
272. }
273. else
274. {
275. win.style.height = parseInt( tit.style.height ) + 2 * 2;
276. windowTopOperateSpan.innerHTML = "2";
277. windowContentDiv.style.display = "none";
278. }
279. }
280. //  ---------------------------------------
281. // 让背景渐渐变暗
282. function showBackground( obj, endInt )
283. {
284. if( isIe )
285. {
286. obj.filters.alpha.opacity += 1;
287. if( obj.filters.alpha.opacity &lt; endInt )
288. {
289. setTimeout( function()
290. {
291. this.showBackground( obj, endInt )
292. }
293. , 5 );
294. }
295. }
296. else
297. {
298. var al = parseFloat( obj.style.opacity );
299. al += 0.01;
300. obj.style.opacity = al;
301. if( al &lt; ( endInt / 100 ) )
302. {
303. setTimeout( function()
304. {
305. this.showBackground( obj, endInt )
306. }
307. , 5 );
308. }
309. }
310. }
311. //  ---------------------------------------
312. // 关闭弹出窗口
313. function closeDivWindow( id )
314. {
315. var windowTopTitleSpan = document.getElementById( "windowTopTitle" + id );
316. var windowTopOperateSpan = document.getElementById( "windowTopOperate" + id );
317. var windowTopCloseSpan = document.getElementById( "windowTopClose" + id );
318. var windowTopDiv = document.getElementById( "windowTop" + id );
319. var windowTopBgDiv = document.getElementById( "windowTopBg" + id );
320. var windowContentDiv = document.getElementById( "windowContent" + id );
321. var bgObj = document.getElementById( "window" + id );
322. windowTopDiv.removeChild( windowTopTitleSpan );
323. windowTopDiv.removeChild( windowTopOperateSpan );
324. windowTopDiv.removeChild( windowTopCloseSpan );
325. windowTopBgDiv.removeChild( windowTopDiv );
326. windowTopBgDiv.removeChild( windowContentDiv );
327. document.body.removeChild( windowTopBgDiv );
328. document.body.removeChild( bgObj );
329. }
330. //  ---------------------------------------
331. function ShowDivWin()
332. {
333. var w = new DivWindow("DivWin","标题",400,300,"按住标题可拖动 &lt;br&gt; 双击标题试一下?&lt;br&gt;       不能把我移出边框吧!! O(∩\_∩)O");
334. w.open();
335. }
336. &lt;/script&gt;
337. &lt;/html&gt;