web前端|js教程
js拖动层
web前端-js教程
实例代码一见:
网站源码作用是什么,ubuntu双拼变全拼,反爬虫数据集,晋州 php,seo痛点lzw
#main div{position:absolute;width:220px;height:150px;border:1px solid #999;}
var a;
document.onmouseup = function() {
if (!a) return;
a = "";
};
document.onmousemove = function(d) {
if (!a) return;
d=d||event;
a.style.left = (d.clientX - b) + "px";
a.style.top = (d.clientY - c) + "px";
};
function $(o, e) {
a = o;
b = e.clientX - parseInt(a.style.left);
c = e.clientY - parseInt(a.style.top);
}
1
2
3
4
5
6
效果:
php c2c 网站源码,ubuntu 光驱读光盘,tomcat9新特征,任务队列+爬虫,php代码怎么换行,listing seolzw
小白 3d签到墙 源码,vscode怎跳转,ubuntu热,tomcat 慢速dos,sqlite设置连接密码,电脑版微营销工具插件,最新的前端框架是什么,客厅内爬虫如何清理干净,php 电台,阳曲seo优化效果,支付单页网站模板,table 网页模板网站,cmake 模板lzw
#main div{position:absolute;width:220px;height:150px;border:1px solid #999;}
var a; document.onmouseup = function() { if (!a) return; a = “”; }; document.onmousemove = function(d) { if (!a) return; d=d||event; a.style.left = (d.clientX – b) + “px”; a.style.top = (d.clientY – c) + “px”; }; function $(o, e) { a = o; b = e.clientX – parseInt(a.style.left); c = e.clientY – parseInt(a.style.top); }
1
2
3
4
5
6
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]实例代码二见:
#test{width:200px; height:200px; background:pink; cursor:move; position:absolute; left:100px; top:100px}
var obj=document.getElementById("test");
var b;
obj.onmousedown=function(e){
b=true;
var divLeft=parseFloat(window.getComputedStyle?window.getComputedStyle(obj,null).left:obj.currentStyle.left);
var divTop=parseFloat(window.getComputedStyle?window.getComputedStyle(obj,null).top:obj.currentStyle.top);
var e=e||event;
var divX=e.clientX-divLeft; //计算鼠标和div边框的距离
var divY=e.clientY-divTop;
document.onmousemove=function(e){
if(b){
var e=e||event; //兼容IE8及以下
obj.style.left=e.clientX-divX+"px";
obj.style.top=e.clientY-divY+"px";
}
}
}
document.onmouseup=function(){
b=false;
}
效果:
var obj=document.getElementById(“test”); var b; obj.onmousedown=function(e){ b=true; var divLeft=parseFloat(window.getComputedStyle?window.getComputedStyle(obj,null).left:obj.currentStyle.left); var divTop=parseFloat(window.getComputedStyle?window.getComputedStyle(obj,null).top:obj.currentStyle.top); var e=e||event; var divX=e.clientX-divLeft; //计算鼠标和div边框的距离 var divY=e.clientY-divTop; document.onmousemove=function(e){ if(b){ var e=e||event; //兼容IE8及以下 obj.style.left=e.clientX-divX+”px”; obj.style.top=e.clientY-divY+”px”; } } } document.onmouseup=function(){ b=false; }
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]实例代码三见(拖动把柄):
在这里才能移动
var obj=document.getElementById(“test”); var objBox=document.getElementById(“testBox”); var b; obj.onmousedown=function(e){ b=true; var divLeft=parseFloat(window.getComputedStyle?window.getComputedStyle(objBox,null).left:objBox.currentStyle.left); var divTop=parseFloat(window.getComputedStyle?window.getComputedStyle(objBox,null).top:objBox.currentStyle.top); var e=e||event; var divX=e.clientX-divLeft; //计算鼠标和div边框的距离 var divY=e.clientY-divTop; document.onmousemove=function(e){ if(b){ var e=e||event; //IE8及以下浏览器得写这句 objBox.style.left=e.clientX-divX+”px”; objBox.style.top=e.clientY-divY+”px”; } } } document.onmouseup=function(){ b=false; }
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]