【j2ee】div浮动层拖拽
背景:近期项目中需要实现弹出浮层增加数据,并且浮动层可以拖拽
解决步骤:1、浮动层实现 2、拖拽实现
多方查资料,基本实现功能,现做demo,便于以后使用
先上图片大体展示实现效果:
再上代码,展示我的实现思路:
DragAndDrop.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>浮动层拖拽</title>
<script src="DragDiv.js" type="text/javascript" language="javascript"
charset="UTF-8"></script>
<link href="UploadFile.css" rel="stylesheet" type="text/css" /> <script type="text/javascript">
function openFuCeng() {
document.getElementById("moveFile").style.display = "block";
document.getElementById("shadow2").style.display = "block"; }
function closeDiv() {
document.getElementById("moveFile").style.display = "none";
document.getElementById("shadow2").style.display = "none"; }
</script>
</head>
<body>
<div id="dragAndDrop">
<!-- 浮层开始 -->
<div id="moveFile" class="moveFile">
<div class="toolbarHeight" onmousedown="down('moveFile')">
<label>鼠标拖拽可移动</label>
</div>
<div style="width:100%;height:85px; ">
<p>浮动层</p>
</div>
<div class="fucengDiv" >
<input class="toobarBtn" type="button" value="关闭" onclick="javascript:closeDiv()"/>
</div>
</div>
<div id="shadow2"></div>
<!-- 浮层结束 -->
<div></div>
<input type="button" value="点击展示浮层" onclick="javascript:openFuCeng()" style="margin-top: 20px;">
<p>底层页面</p>
</div>
</body>
</html>
DragDiv.js
/**
* 浮层拖拽
*/
var px = 0;
var py = 0;
var begin = false;
//是否要开启透明效果
var enableOpacity = false; // 默认不允许
var myDragDiv;
/**
* 浮层拖拽
* @param divid 待拖拽对象id
*/
function down(divid) {
myDragDiv=document.getElementById(divid);//获得被拖拽对象
begin = true;
myDragDiv.style.cursor = "hand";
event.srcElement.setCapture();
px = myDragDiv.style.pixelLeft - event.x;//pixelLeft
py = myDragDiv.style.pixelTop - event.y;
} document.onmousemove=function() {
if (myDragDiv != null && typeof (myDragDiv) != "undefined") {
if (begin&&(px + event.x)>10) {
if (enableOpacity) { myDragDiv.style.filter = "Alpha(opacity=30)"; } // 滤镜
myDragDiv.style.pixelLeft = px + event.x;
myDragDiv.style.pixelTop = py + event.y;
}
}
} document.onmouseup=function() {
if (myDragDiv != null && typeof (myDragDiv) != "undefined") {
begin = false;
if (enableOpacity) { myDragDiv.style.filter = "Alpha(opacity=100)"; } // 滤镜
myDragDiv.style.cursor = "default";
event.srcElement.releaseCapture();
myDragDiv = null;
}
}
UploadFile.css
@CHARSET "UTF-8";
#dragAndDrop{
margin: 0 auto;
width: 300px;
height: 200px;
margin-top:200px;
text-align:center;
background:#98fb98 ;
border: 2px solid black;
}
/*文件整体大小*/
#shadow2{
display: none;
background:white;
opacity:0.2;
filter:alpha(opacity=20);
position:absolute;
left:0px;
top:0px;
width:100%;
height:100%;
z-index:996;/*z-index position display 共同实现层的浮动 */
margin: 0 auto;
}
/*移动浮层*/
.moveFile{
display: none;
width: 150px;
height: 160px;
position: absolute;
z-index:997;
border: 2px solid black;
background:#FFF;
top:50%;
margin-top:-50px;
left:50%;
margin-left:-50px;
}
.toolbarHeight{
height: 25px;
width: 100%;
border-bottom:#b7e3f2 1px solid;
background:#98fb98 ;
}
.toobarBtn{
float: right;
margin-right: 10px;
} .fucengDiv{
width:100%;
height: 30px;
padding-top: 5px;
border-top:#b7e3f2 1px solid;
}
注:拖拽的实现是在网上查资料找到的,做了一些小的改动,大意了忘记留下学习地址了
以上使用到的代码,看着还是比较简单的,但是你理解吗?至少我还是有问题的
问题:
首次拖拽总是不灵活,需要多晃动几次鼠标才有效,为什么呢?待解决
【j2ee】div浮动层拖拽的更多相关文章
- js div浮动层拖拽效果代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- jQuery 学习笔记3 点击弹出一个div并允许拖拽移动
这里我看了下http://qings.blog.51cto.com/4857138/998878/ 的文章,感谢他的分享. 首先我们有一个a标签和一个div,div默认是不显示的,当用户点击时改为显示 ...
- 用JavaScript实现div的鼠标拖拽效果
实现原理鼠标按下时根据onmousemove事件来动态获取鼠标坐标位置以此来更新div的位置,实现的前提时div要有一个定位效果,不然的话是移动不了它的. HTML <div class=&qu ...
- div与div之间的拖拽
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 使div弹窗可拖拽指令
在项目开发过程中,有些情况dialog弹窗,直接使用div模拟弹窗效果,并需要支持div可拖拽. div模拟弹窗效果: (1)在用于存放指令的文件夹内,新建js文件,命名为:drag.js.具体代码如 ...
- div/dom元素拖拽缩放插件,纯js实现拖拽缩放,不依赖jQuery~
产品需求,需要用到对div(dom)进行拖拽缩放操作,看到有好多插件,要么依赖jQuery,要么文件太大. 封装了一个插件,不压缩状态下5KB. html <!DOCTYPE html> ...
- DIV 实现可拖拽 功能(留档)
//可拖拽 功能 $.fn.extend({ //用法:$(element).jqDrag(); //element需要具备定位属性,需要手动调整层叠样式,这里只是修改鼠标拖动效果 ...
- DIV浮动层被OCX控件遮蔽解决方案
在开发中需要在网页中嵌入OCX控件,但是控件嵌入后,总是会出现在网页最顶层,页面中的浮动DIV总是不能正常显示,会被遮蔽掉,那么这里就需要特殊处理一下: OBJECT会遮蔽掉页面内容,但是IFRAME ...
- jquery弹出层拖拽
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <met ...
随机推荐
- [C#]窗体切换--避免开启多个线程
先说说这个多窗体的界面的解决的办法: 用到的方法很简单,就是程序运行就建立一个MainForm,在这个MainForm中设立一个Panel,同时设立几个按钮,按下每个按钮都在这个Panel中载入不同的 ...
- win64位 apache2.4 php5.4 mysql5.6
apache2.4 php5.4 mysql5.6 源文件下载 +以前的配置数据参考 链接:http://pan.baidu.com/s/1skfmGyT 密码:hqtp 比较好的参考资料 http: ...
- matlab之kmeans聚类用法
kmeans函数用法如下: [IDX,C,sumd,D] = kmeans(X,2,'Distance','city','Replicates',5,'Options',opts); 参数含义如下:I ...
- (IOS)数据持久化
1.属性列表序列化 2.模型对象归档 3.嵌入式SQLite3 4.Core Data 5.应用程序设置 6.UIDocument管理文档存储 7.iCloud Demo界面: 1.属性列表序列化 即 ...
- sorl6.0+jetty+mysql
sorl6.0+jetty+mysql搭建solr服务 1.下载solr 官网:http://lucene.apache.org/solr/ v2.目录结构如下 v3.启动solr(默认使用jetty ...
- QNetworkAccessManager跳转URL处理(使用QNetworkRequest::RedirectionTargetAttribute获得跳转URL)
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*))); void Mai ...
- pageContext.request.contextPath 和 request.getContextPath()
作用是取出部署的应用程序名,这样不管如何部署,所用路径都是正确的. El表达式的写法:${pageContext.request.contextPath} jsp的写法:<%=request.g ...
- victim是什么意思_victim在线翻译_英语_读音_用法_例句_海词词典
victim是什么意思_victim在线翻译_英语_读音_用法_例句_海词词典 victim
- 复习知识点:GCD多线程
GCD的基础 #pragma mark - 使用GCD 创建一个 串行 队列 // 第一种:系统提供的创建串行队列的方法 // 在真正的开发中如果需要创建串行队列,比较习惯用这种 // dispatc ...
- Android 开机动画源码分析
Android系统在启动SystemServer进程时,通过两个阶段来启动系统所有服务,在第一阶段启动本地服务,如SurfaceFlinger,SensorService等,在第二阶段则启动一系列的J ...