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 ...
随机推荐
- C++中基于Crt的内存泄漏检测(重载new和delete,记录在Map里)
尽管这个概念已经让人说滥了 ,还是想简单记录一下, 以备以后查询. #ifdef _DEBUG#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FIL ...
- QT 让窗口(或控件)居中
代码如下: XXX::XXX(QWidget *parent /* = 0 */) { .................. //注意,resize一定要放在这段代码的前面 resize(300, 3 ...
- c++ string类型转换为char *类型
string 是c++标准库里面其中一个,封装了对字符串的操作 把string转换为char* 有3中方法: 1.data 如: string str="abc"; char *p ...
- jzp线性筛及其简单应用
前言: 很久以前看过了线性筛,没怎么注意原理,但是后来发现线性筛还有很有用的.. 比如上次做的一道题就需要找出每个数的最小质因子,先筛再找就太慢了..一看线性筛发现就可以直接在筛的过程中处理出来了! ...
- Git分支学习总结
思维导图: 总结: Git分支:分为2类合计为5种分支. 第一类:主分支和开发分支. 第二类:特性分支,热补丁分支,版本分支.
- mybatis dao无实现类的配置
spring的配置文件 添加: <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> ...
- storm 事务和DRPC结合
示例代码: package com.lky.topology; import backtype.storm.Config; import backtype.storm.LocalCluster; im ...
- Beanstalkd(ubuntu安装)
安装Beanstalkd # apt-get install beanstalkd Unubtu 开启beanstalkd的持久化选项 #vim /etc/default/beanstalkd 把S ...
- 百度全站变https
今天打开百度首页,突然发现,百度的网址变为 https://www.baidu.com/,如下图: 好嘛,以后再也不怕别人使用抓包工具查看我在百度的搜索数据了.当年的Duck Duck Go就是因为是 ...
- MYSQL数据备份与还原学习笔记
数据备份与还原 1.mysqldump 1.1 文件地址: E:\xampp\mysql\bin 文件名:mysqldump.exe CMD下进入mysqldump.exe cd E:\xampp ...