javascript实现可拖动变色并关闭层窗口实例


Posted in Javascript onMay 15, 2015

本文实例讲述了javascript实现可拖动变色并关闭层窗口的方法。分享给大家供大家参考。具体分析如下:

这是一款基于javascript+CSS实现层拖动的代码,不同的是它在拖动的时候窗口会变色,使操作体验更好一些,你可以运行代码查看效果。它还可以显示/隐藏或关闭打开的效果,没事的时候推敲一下

<html>
<head>
<title>拖动窗口</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;
//开始拖动;
function startDrag(obj)
{
if(event.button==1)
{
//锁定标题栏;
obj.setCapture();
//定义对象;
var win = obj.parentNode;
var sha = win.nextSibling;
//记录鼠标和层位置;
x0 = event.clientX;
y0 = event.clientY;
x1 = parseInt(win.style.left);
y1 = parseInt(win.style.top);
//记录颜色;
normal = obj.style.backgroundColor;
//改变风格;
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;
}
}
//拖动;
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;
}
}
//停止拖动;
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;
}
}
//获得焦点;
function getFocus(obj)
{
if(obj.style.zIndex!=index)
{
index = index + 2;
var idx = index;
obj.style.zIndex=idx;
obj.nextSibling.style.zIndex=idx-1;
}
}
//最小化;
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";
}
}
//创建一个对象;
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();
}
//初始化;
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);
}
//显示隐藏窗口
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,"窗口1","xWin Demo");
var b = new xWin("2",240,200,100,100,"窗口2","Welcome"); 
var c = new xWin("3",200,160,250,50,"窗口3","Copyright");
ShowHide("1","none");//隐藏窗口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>

希望本文所述对大家的javascript程序设计有所帮助。

Javascript 相关文章推荐
网站页面自动跳转实现方法PHP、JSP(上)
Aug 01 Javascript
JavaScript闭包 懂不懂由你反正我是懂了
Oct 21 Javascript
JavaScript fontsize方法入门实例(按照指定的尺寸来显示字符串)
Oct 17 Javascript
Javascript中arguments用法实例分析
Jun 13 Javascript
javascript实现网页子页面遍历回调的方法(涉及 window.frames、递归函数、函数上下文)
Jul 27 Javascript
vue.js指令和组件详细介绍及实例
Apr 06 Javascript
利用Vue.js实现求职在线之职位查询功能
Jul 03 Javascript
基于AngularJS的拖拽文件上传的实例代码
Jul 15 Javascript
微信小程序实现保存图片到相册功能
Nov 30 Javascript
关于layui 下拉列表的change事件详解
Sep 20 Javascript
Vue如何实现验证码输入交互
Dec 07 Vue.js
JavaScript 事件捕获冒泡与捕获详情
Nov 11 Javascript
JavaScript实现简单的数字倒计时
May 15 #Javascript
JavaScript生成福利彩票双色球号码
May 15 #Javascript
JavaScript实现列表分页功能特效
May 15 #Javascript
基于jQuery插件实现环形图标菜单旋转切换特效
May 15 #Javascript
js生成验证码并直接在前端判断
May 15 #Javascript
javascript实现表格增删改操作实例详解
May 15 #Javascript
javascript实现可全选、反选及删除表格的方法
May 15 #Javascript
You might like
PHP+MYSQL开发工具及资源收藏
2007/01/02 PHP
一贴学会PHP 新手入门教程
2009/08/03 PHP
php 不同编码下的字符串长度区分
2009/09/26 PHP
PHP 写文本日志实现代码
2010/05/18 PHP
PHP函数eval()介绍和使用示例
2014/08/20 PHP
php实现用于计算执行时间的类实例
2015/04/18 PHP
YII Framework框架教程之安全方案详解
2016/03/14 PHP
thinkPHP3.2简单实现文件上传的方法
2016/05/16 PHP
用javascript实现的图片马赛克后显示并切换加文字功能
2007/04/21 Javascript
Js 获取当前日期时间及其它操作实现代码
2021/03/04 Javascript
JS仿百度搜索自动提示框匹配查询功能
2013/11/21 Javascript
js判断某个方法是否存在实例代码
2015/01/10 Javascript
javascript实现回到顶部特效
2015/05/06 Javascript
详解基于Bootstrap扁平化的后台框架Ace
2015/11/27 Javascript
nodejs 十六进制字符串型数据与btye型数据相互转换
2018/07/30 NodeJs
nodejs更新package.json中的dependencies依赖到最新版本的方法
2018/10/10 NodeJs
layui监听select变化,以及设置radio选中的方法
2019/09/24 Javascript
js验证账户名是否重复
2020/05/26 Javascript
[01:00:44]DOTA2上海特级锦标赛主赛事日 - 3 败者组第三轮#1COL VS Alliance第三局
2016/03/04 DOTA
python获取当前日期和时间的方法
2015/04/30 Python
Python时间模块datetime、time、calendar的使用方法
2016/01/13 Python
浅析python中SQLAlchemy排序的一个坑
2017/02/24 Python
人工神经网络算法知识点总结
2019/06/11 Python
Pyqt5实现英文学习词典
2019/06/24 Python
Python使用Pandas库实现MySQL数据库的读写
2019/07/06 Python
python通过TimedRotatingFileHandler按时间切割日志
2019/07/17 Python
python list多级排序知识点总结
2019/10/23 Python
解决pycharm导入本地py文件时,模块下方出现红色波浪线的问题
2020/06/01 Python
aec加密 php_php aes加密解密类(兼容php5、php7)
2021/03/14 PHP
落实八项规定专题民主生活会对照检查材料
2014/09/15 职场文书
2015年初中元旦晚会活动总结
2014/11/28 职场文书
工程部主管岗位职责
2015/02/12 职场文书
勤俭节约倡议书范文
2015/04/29 职场文书
2016春季运动会开幕词
2016/03/04 职场文书
教你用Python爬取英雄联盟皮肤原画
2021/06/13 Python
PostgreSQL常用字符串分割函数整理汇总
2022/07/07 PostgreSQL