鼠标拖拽,阻止默认事件、事件冒泡。
来源:互联网 发布:java 递归删除子节点 编辑:程序博客网 时间:2024/05/20 20:18
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
*{ margin:0; top:0}
#box{ height:200px; width:200px; background:red; position:absolute; cursor:move}
</style>
<script>
window.onload=function(){
var oDiv=document.getElementById('box');
var oInput=document.getElementsByTagName('input')[0];
//阻止内部标签的事件冒泡;使oInput标签在onmousedown下会聚焦,能输入文本。
oInput.onmousedown=function(ev){
ev=ev||window.event; //IE8以下事件对象是window.event
ev.cancelBubble=true;
};
oDiv.onmousedown=function(ev){
ev=ev||window.event;
//计算偏移距离
var disX=ev.clientX-oDiv.offsetLeft; //distance 距离
var disY=ev.clientY-oDiv.offsetTop;
//开始拖动------------------------------------------
document.onmousemove=function(ev){
ev=ev||window.event;
var l=ev.clientX-disX; //鼠标x
var t=ev.clientY-disY;//鼠标y
console.log(l,t);
//先判断,后赋值
if(l<0){
l=0;
};
if(t<0){
t=0;
};
var screenW=document.documentElement.clientWidth; //浏览器窗口宽度
var screenH=document.documentElement.clientHeight; //浏览器窗口高度
if(l>screenW-oDiv.offsetWidth){
l=screenW-oDiv.offsetWidth;
};
if(t>screenH-oDiv.offsetHeight){
t=screenH-oDiv.offsetHeight;
};
oDiv.style.left=l+"px";
oDiv.style.top=t+"px";
};
//停止拖动 动作写在document上-----------------------------
document.onmouseup=function(){
document.onmousemove=null;
};
//阻止默认事件,这里指的具体默认事件是 “鼠标会选中oDiv里面的内容” 这一事件
return false;
};
};
</script>
</head>
<body>
<div id="box">dsf sdf sadf sadf sadf adsf sad fas df safd ad fa fd asd asdf
<input type="text" value="3234243234">
</div>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
*{ margin:0; top:0}
#box{ height:200px; width:200px; background:red; position:absolute; cursor:move}
</style>
<script>
window.onload=function(){
var oDiv=document.getElementById('box');
var oInput=document.getElementsByTagName('input')[0];
//阻止内部标签的事件冒泡;使oInput标签在onmousedown下会聚焦,能输入文本。
oInput.onmousedown=function(ev){
ev=ev||window.event; //IE8以下事件对象是window.event
ev.cancelBubble=true;
};
oDiv.onmousedown=function(ev){
ev=ev||window.event;
//计算偏移距离
var disX=ev.clientX-oDiv.offsetLeft; //distance 距离
var disY=ev.clientY-oDiv.offsetTop;
//开始拖动------------------------------------------
document.onmousemove=function(ev){
ev=ev||window.event;
var l=ev.clientX-disX; //鼠标x
var t=ev.clientY-disY;//鼠标y
console.log(l,t);
//先判断,后赋值
if(l<0){
l=0;
};
if(t<0){
t=0;
};
var screenW=document.documentElement.clientWidth; //浏览器窗口宽度
var screenH=document.documentElement.clientHeight; //浏览器窗口高度
if(l>screenW-oDiv.offsetWidth){
l=screenW-oDiv.offsetWidth;
};
if(t>screenH-oDiv.offsetHeight){
t=screenH-oDiv.offsetHeight;
};
oDiv.style.left=l+"px";
oDiv.style.top=t+"px";
};
//停止拖动 动作写在document上-----------------------------
document.onmouseup=function(){
document.onmousemove=null;
};
//阻止默认事件,这里指的具体默认事件是 “鼠标会选中oDiv里面的内容” 这一事件
return false;
};
};
</script>
</head>
<body>
<div id="box">dsf sdf sadf sadf sadf adsf sad fas df safd ad fa fd asd asdf
<input type="text" value="3234243234">
</div>
</body>
</html>
阅读全文
0 0
- 鼠标拖拽,阻止默认事件、事件冒泡。
- 阻止事件冒泡 默认事件
- 绑定事件、阻止默认事件、阻止冒泡
- 阻止冒泡和默认事件
- 阻止事件冒泡\阻止默认行为
- JQ阻止默认事件与阻止冒泡
- JS 阻止冒泡与阻止默认事件
- js 阻止默认事件 和事件冒泡
- 阻止事件冒泡、捕获、和默认事件
- 如何阻止事件冒泡和默认事件
- 如何阻止事件冒泡和默认事件
- JavaScript阻止事件冒泡和默认事件
- 如何阻止冒泡事件和默认事件
- 阻止事件冒泡、取消默认事件
- 阻止默认事件行为和事件冒泡
- 前端阻止事件冒泡和默认事件
- 事件冒泡。阻止事件冒泡 |阻止元素的默认行为
- javascript事件之: 事件冒泡, 事件捕获 ,阻止默认事件
- 当一个程序员写不出代码了,该怎么办?
- 19、结果集映射之多对一关联映射
- centos7.x 配置(php7.x) + mariadb + apache
- 线性探测法的查找函数
- Leetcode513. Find Bottom Left Tree Value
- 鼠标拖拽,阻止默认事件、事件冒泡。
- 如何连接远程服务器的MySQL
- Mac上安装MySQL
- js中的面向对象
- leetcode十二周解题报告
- Linux常用命令学习总结三(which,find,文件系统,df,du)
- 20、结果集映射之一对多关联映射
- CXF整合Spring通过tomcat发布webservice
- 前端学习框架