js拖动层
模仿网易彩票网(http://caipiao.163.com/)的登陆框自己做了一个拖动层,不过有点小问题——在谷歌浏览拖动的时候鼠标状态变成了文字状态(cursor:text;)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js拖动层</title>
<style>
body{padding:0; margin:0; width:100%; height:100%;}
* html,* html body{background-image:url(about:blank);background-attachment:fixed}
#dragWp{width:100%; height:100%; position:fixed; left:0; top:0; z-index:99999999; _position:absolute;_left:expression(eval(document.documentElement.scrollLeft));_top:expression(eval(document.documentElement.scrollTop))}
#dragBg{ width:100%; height:100%; background-color:#000; position:absolute; left:0; top:0; opacity:0.4; filter:alpha(opacity=40);}
#dragMain{width:200px;height:100px;position:absolute; border:1px solid #999;line-height:30px;font-size:16px;text-align:center; background-color:#FFF;}
#dragTitle{ width:100%; height:40px; line-height:40px; background-color:#ccc; cursor:move;}
#close{float:right; margin-right:5px; z-index:999; font-size:12px; font-style:normal; cursor:pointer;}
</style>
</head>
<body>
<div style="height:1200px; text-align:center;">
<h1>hello world</h1>
</div>
<div id="dragWp">
<div id="dragBg"></div>
<div id="dragMain">
<div id="dragTitle">
<em id="close">关闭</em>拖动层
</div>
hello world
</div>
</div>
<script>
window.onload=function(){
var dragWp=document.getElementById("dragWp");
var dragMainId=document.getElementById("dragMain");
var w=dragWp.offsetWidth;
var h=dragWp.offsetHeight;
dragMainId.style.left=(w-dragMainId.offsetWidth)/2+"px";
dragMainId.style.top=(h-dragMainId.offsetHeight)/2+"px";
} function dragTemp(dragWp,dragMainId,dragTitleId,closeId,opacity){
var dragWp=document.getElementById(dragWp);
var dragMainId=document.getElementById(dragMainId);
var dragTitleId=document.getElementById(dragTitleId);
var closeId=document.getElementById(closeId);
var posX,posY,x,y;
dragTitleId.onmousedown=function(e){
var e = e || window.event;
posX=e.clientX-dragMainId.offsetLeft; //鼠标距离拖动层左侧的距离
posY=e.clientY-dragMainId.offsetTop; //鼠标距离拖动层上侧的距离
dragMainId.style.opacity=opacity;
dragMainId.style.filter="alpha(opacity="+opacity*100+")";
document.onmousemove=function(e){
var e = e || window.event;
x=e.clientX-posX;
y=e.clientY-posY;
// if(x<=0) x=0; //不允许移出左边
// else if(x>=document.body.clientWidth-dragMainId.offsetWidth) x=document.body.clientWidth-dragMainId.offsetWidth; //不允许移出右边
dragMainId.style.left=x+"px";
dragMainId.style.top=y+"px";
dragTitleId.style.cursor="move";
document.onselectstart=function(){ return false } //拖动不选中文字
}
};
document.onmouseup=function(){
document.onmousemove=null;
dragMainId.style.opacity=1;
dragMainId.style.filter="alpha(opacity=100)";
}
if(closeId){
closeId.onclick=function(){
dragWp.style.display="none";
}
}
}
function drag(args){
dragTemp(args.dragWp,
args.dragMainId,
args.dragTitleId,
args.closeId || null,
args.opacity || 1)
}
//调用
drag({dragWp:"dragWp",dragMainId:"dragMain",dragTitleId:"dragTitle",closeId:"close",opacity:0.5});
</script>
</body>
</html>
js拖动层的更多相关文章
- js拖动层原形版
脚本文件: function JzDrag(moveDivId, moveDivHandle) { // var me = this; this.M = false; //是否在移动对象 this.D ...
- JQuery 拖动层
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- JS拖动DIV布局
方法一: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- 在chrome下鼠标拖动层变文本形状的问题
学JQ也有一段时间了,想自己写个鼠标拖动层移动的效果(很简单,只是为了练习而已)于是就写下了下面的代码 <!DOCTYPE html> <html> <head> ...
- h5滑动方向、手机拖动层
做h5时需对手指滑动方向判断及拖动浮动层,本文代码适用于手机端h5页面,pc页面可使用onMouseDown.onMouseUp.onMouseMove.(本方法仅为功能实现原理和演示,可根据自己的需 ...
- 微信小程序-06-详解介绍.js 逻辑层文件-注册页面
上一篇介绍的是 app.js 逻辑层文件中注册程序,对应的每个分页面都会有的 js 文件中 page() 函数注册页面 微信小程序-06-详解介绍.js 逻辑层文件-注册页面 宝典官方文档: http ...
- 微信小程序-05-详解介绍.js 逻辑层文件
上一篇介绍了关于.json 的配置文件,本篇介绍关于.js 逻辑层文件 微信小程序-05-详解介绍.js 逻辑层文件 宝典官方文档: https://developers.weixin.qq.com/ ...
- JS 拖动原理
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 拖动层 拖动div 封装js 貌似不兼容FF,郁闷
原文发布时间为:2009-12-02 -- 来源于本人的百度文章 [由搬家工具导入] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...
随机推荐
- 操作css样式
<script type="text/javascript"> //产生一个四位的验证码. function createCode(){ var datas = ['A ...
- 存储过程中拼接sql的示例
create or replace package body CRF_CMS_DOCTOR_PKG is --根据医院查询医生PROCEDURE P_HOSPITALBYDOCTOR_LIST ( v ...
- 『软件介绍』SQLServer2008 基本操作
0x 01 连接数据库 Win7下,先打开SQLServer管理工具(开始菜单/所有程序/Microsoft SQL Server 2008/SQL Server Management Studio) ...
- 转:C语言申请内存时堆栈大小限制
一直都有一个疑问,一个进程可以使用多大的内存空间,swap交换空间以及物理内存的大小,ulimit的stack size对进程的内存使用有怎样的限制?今天特亲自动手实验了一次,总结如下: 开辟一片内存 ...
- 转:窗口启用/禁用功能函数EnableWindow的使用
在非MFC环境中如何使控件或者窗口禁用呢?起初是想通过发送消息来实现,但找来找去都木有找到控件禁用的消息(也是是博主木有找到的缘故),所以只能另辟蹊径,使用 EnableWindow这个函数, 该函数 ...
- 转:为什么需要htons(), ntohl(), ntohs(),htons() 函数
为什么需要htons(), ntohl(), ntohs(),htons() 函数: 在C/C++写网络程序的时候,往往会遇到字节的网络顺序和主机顺序的问题.这是就可能用到htons(), ntohl ...
- c语言判断用户是否输入-非阻塞函数kbhit
一.基础研究 要从地址读取数据,肯定是要定义一个指针变量p,用它来实现变换地址和取值的功能.另外程序是当两个条件中的某一个出现时才停止,所以应该用while~do循环语句循环输出n和d,并用while ...
- [LeetCode 114] - 将树平面化为链表(Flatten Binary Tree to Linked List)
问题 给出一个二叉树,将其原地平面化为链表. 例如,给出: 1 / \ 2 5 / \ \ 3 4 6 平面化后的树看起来应该是这样: 1 \ 2 \ ...
- 关于Android读取不同位置(drawable,asset,SDCard)的图片资源的总结
方式一: 已将图片保存到drawable目录下,通过图片id获得Drawable或者Bitmap,此方式最常用.(若只知道图片的名称,还可以通过图片的名称获得图片的id) (1)通过图片id获得Dra ...
- Linux内核源代码解析——TCP状态转移图以及其实现
本文原创为freas_1990,转载请标明出处http://blog.csdn.net/freas_1990/article/details/10223581 TCP状态转移的原理并不高深,但是处理逻 ...