JavaScript拖拽 1.0

本文介绍了拖拽功能的基本实现及优化过程,包括在不同浏览器(IE8.0, Firefox)上的兼容性测试。通过引入事件监听和元素定位,实现了元素在页面上的拖拽与位置调整。后续版本通过增加标题元素的拖拽支持,提升了用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这两天没什么心情,进步不大,心虚。刚搞了个简单的拖拽,试了下,IE 8.0, Firefox都可以

var drag = {
    enable : function(domIdStr){
        var dom = document.getElementById(domIdStr);
        var diffX = 0, diffY = 0, draging = false;
        dom.style.position = 'absolute';
        dom.style.visibility = 'visible';
        dom.style.width = '200px';
        dom.style.height = '100px';
        dom.style.backgroundColor = 'teal';
        
        dom.onmousedown = function(e){
            e = e || window.event;
            draging = true;
            dom.style.cursor = 'move';
			diffX = e.clientX - dom.offsetLeft;
			diffY = e.clientY - dom.offsetTop;
			if(dom.setCapture){
			    dom.setCapture();
			}
			dom.onmousemove = function(e){
                e = e || window.event;
		        if(draging){
        	        dom.style.left = (e.clientX - diffX) + 'px';
        	        dom.style.top = (e.clientY - diffY) + 'px';
		        }
            };
	        dom.onmouseup = function(e){
		        e = e || window.event;
		        if(dom.releaseCapture){
		            dom.releaseCapture();
		        }
		        draging = false;
		        dom.style.cursor = 'default';
		        dom.onmousemove = null;
		        dom.onmouseup = null;
		        
	        };
        };
    }
};

 稍作修改,再出个1.1版本,就不发新的blog了

 

var drag = {
    enable : function(domIdStr, titleDomIdStr){
        var dom = document.getElementById(domIdStr);
        var title = document.getElementById(titleDomIdStr);
        var diffX = 0, diffY = 0, draging = false;
        dom.style.position = 'absolute';
        dom.style.visibility = 'visible';
        dom.style.width = '200px';
        dom.style.height = '100px';
        dom.style.backgroundColor = 'teal';
        
        title.onmousedown = function(e){
            e = e || window.event;
            draging = true;
            title.style.cursor = 'move';
            title.style.margin = 0;
            title.style.border = '1px solid yellow';
			diffX = e.clientX - dom.offsetLeft;
			diffY = e.clientY - dom.offsetTop;
			if(title.setCapture){
			    title.setCapture();
			}
			title.onmousemove = function(e){
                e = e || window.event;
		        if(draging){
        	        dom.style.left = (e.clientX - diffX) + 'px';
        	        dom.style.top = (e.clientY - diffY) + 'px';
		        }
            };
	        title.onmouseup = function(e){
		        e = e || window.event;
		        if(title.releaseCapture){
		            title.releaseCapture();
		        }
		        draging = false;
		        title.style.cursor = 'default';
		        title.onmousemove = null;
		        title.onmouseup = null;
		        
	        };
        };
    }
};

  

 

转载于:https://www.cnblogs.com/realwall/archive/2011/10/27/2227096.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值