<!DOCTYPE html>
<html>
<head>
<title>
</title>
<style media="screen" type="text/css">
*{
margin: 0;
padding: 0;
}
.box{
background: red;
width: 100px;
height: 100px;
position: absolute;
top: 0;
left: 100px;
}
</style>
</head>
<body>
<div class="box">
111
</div>
<div class="box" style="left:300px;top: 300px">
222
</div>
<div class="box" style="left:500px;top: 10px">
333
</div>
<script>
document.addEventListener("mousedown",drag);
document.addEventListener("mousemove",drag);
document.addEventListener("mouseup",drag);
var flag=false;
var x1,y1,stx,sty,el;
function drag(ev) {
var ev=ev || window.event;
ev.preventDefault();
switch(ev.type){
case "mousedown":
console.log(ev.target.className);
if(ev.target.className==='box'){
flag=true;
el=ev.target;
el.style.cursor="move";
el.style.zIndex='9';
//鼠标点中目标元素时鼠标在页面中的位置
x1=ev.clientX;
y1=ev.clientY;
// console.log('鼠标在页面上的位置'+x1);
//鼠标点中目标元素时元素的位置
// console.log('元素在页面中的横坐标位置'+el.offsetLeft)
stx=x1-el.offsetLeft;
sty=y1-el.offsetTop;
// console.log('鼠标在元素上的位置'+stx);

console.log('鼠标按下');
}

break;
case "mousemove":
if(flag){
x1=ev.clientX;
y1=ev.clientY;
var left,top;
left=x1-stx;
top=y1-sty;

if(left<0){
left=0;
}else if(left>window.innerWidth-el.offsetWidth){
left=window.innerWidth-el.offsetWidth;
}

if(top<0){
top=0;
}else if(top>window.innerHeight-el.offsetHeight){
top=window.innerHeight-el.offsetHeight;
}

el.style.left=left+'px';
el.style.top=top+'px';

}

break;
case "mouseup":
flag=false;
el.style.cursor="inherit";
console.log('鼠标抬起');
document.onmousemove = null;
document.onmouseup = null;
break;
}
}
</script>
</body>
</html>

js 鼠标拖拽元素移动的更多相关文章

  1. js 鼠标拖拽元素

    基础知识 event.clientX.event.clientY 鼠标相对于浏览器窗口可视区域的X,Y坐标(窗口坐标),可视区域不包括工具栏和滚动条.IE事件和标准事件都定义了这2个属性 event. ...

  2. js鼠标拖拽

    html <div id="box"> </div> css ;;} #box{width:200px;height:200px;background:cy ...

  3. js 鼠标拖拽效果实现

    效果: 源码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  4. 关于js拖拽功能,拖拽元素的position:fixed;left:0;right:0;样式引起左右拖动元素会出现落后鼠标移动距离的问题

    被拖拽元素的样式如果为:position:fixed;left:0;right:0;(当时是为了让fixed定位的元素水平居中加的left:0;right:0;避免js动态计算定位的麻烦)时左右拖动会 ...

  5. JS Event 鼠标拖拽事件

    <!DOCTYPE html><html> <head>        <meta charset="UTF-8">         ...

  6. js - 面向对象 - 小案例:轮播图、随机点名、选项卡、鼠标拖拽

    面向对象 对象 : (黑盒子)不了解内部结构, 知道表面的各种操作. 面向对象 : 不了解原理的情况下 会使用功能 . 面向对象是一种通用思想,并非编程中能用,任何事情都能用. 编程语言的面向对象的特 ...

  7. JavaScript实现拖拽元素对齐到网格(每次移动固定距离)

    这几天在做一个拖拽元素的附加功能,就是对齐到网格,实际上就是确定好元素的初始位置,然后拖拽元素时,每次移动固定的距离.让元素都可以在网格内对齐.先上效果图,然后在详细说明一下细节问题 做了一个gif图 ...

  8. juqery 拖拽元素

    转自  http://www.cnblogs.com/holbrook/archive/2012/03/13/2394111.html 因为怕博主删除博客,所以复制过来! JQuery UI是JQue ...

  9. jQuery 鼠标拖拽排序

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

随机推荐

  1. spoj 371 Boxes【最小费用最大流】

    对于ai==0连接(i,t,1,0),对于ai>1(s,i,ai-1,0),然后对以相邻的两个点(i,j)连接(i,j,inf,1),注意这里是一个环的形式,所以1和n+1相连 #include ...

  2. 5位ID生成方案

    最近在某微信技术群,有人问到如何生成5位唯一数字+字母字符串的算法,要保证生成的字符串唯一,且字符串内部也要唯一. 怎么样,这个需求是不是很简单,也有点特殊呢?简单是指需求简单,特殊是指,字符串长度要 ...

  3. C# Web页面打印网页

    <style media=print type="text/css">      .noprint{display:none}  </style> 在打印时 ...

  4. 爬虫—Requests高级用法

    Requests高级用法 1.文件上传 我们知道requests可以模拟提交一些数据.假如有的网站需要上传文件,我们也可以用requests来实现. import requests files = { ...

  5. ES高级查询

    Query Content 在查询过程中,除了判断文档是否满足查询条件外,ES还会计算一个_score来标识匹配的程度,旨在判断目标文档和查询条件的匹配有多好 # POST 192.168.100.1 ...

  6. [Usaco2005 Jan]Muddy Fields泥泞的牧场

    Description 雨连续不断的击打了放牛的牧场,一个R行C列的格子(1<=R<=50,1<=C<=50).虽然这对草来说是件好事,但这却使得一些没有草遮盖的土地变得很泥泞 ...

  7. Monkey Banana Problem LightOJ - 1004

    Monkey Banana Problem LightOJ - 1004 错误记录: 1.数组开小2.每组数据数组没有清空 #include<cstdio> #include<cst ...

  8. 题解报告:hdu 1032 The 3n + 1 problem(克拉兹问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1032 Problem Description Problems in Computer Science ...

  9. 准确计算CoreText高度的方法:

    - (int)getAttributedStringHeightWithString:(NSAttributedString *) string WidthValue:(int) width { ; ...

  10. 原创 SqlParameter 事务 批量数据插入

    不错,很好,以后防注入批量事务提交虽然麻烦点研究了几个小时,但不会是问题了 SqlCommand cmd; HelpSqlServer helps = new HelpSqlServer(); //定 ...