• js simple drag.


    // by zhangxinxu welcome to visit my personal website http://www.zhangxinxu.com/
    // zxx.drag v1.0 2010-03-23 å…ƒç´ çš„æ‹–æ‹½å®žçŽ°
    
    var params = {
    	left: 0,
    	top: 0,
    	currentX: 0,
    	currentY: 0,
    	flag: false
    };
    //获取相关CSS属性
    var getCss = function(o,key){
    	return o.currentStyle? o.currentStyle[key] : document.defaultView.getComputedStyle(o,false)[key]; 	
    };
    
    //拖拽的实现
    var startDrag = function(bar, target, callback){
    	if(getCss(target, "left") !== "auto"){
    		params.left = getCss(target, "left");
    	}
    	if(getCss(target, "top") !== "auto"){
    		params.top = getCss(target, "top");
    	}
    	//o是移动对象
    	bar.onmousedown = function(event){
    		params.flag = true;
    		if(!event){
    			event = window.event;
    			//防止IE文字选中
    			bar.onselectstart = function(){
    				return false;
    			}  
    		}
    		var e = event;
    		params.currentX = e.clientX;
    		params.currentY = e.clientY;
    	};
    	document.onmouseup = function(){
    		params.flag = false;	
    		if(getCss(target, "left") !== "auto"){
    			params.left = getCss(target, "left");
    		}
    		if(getCss(target, "top") !== "auto"){
    			params.top = getCss(target, "top");
    		}
    	};
    	document.onmousemove = function(event){
    		var e = event ? event: window.event;
    		if(params.flag){
    			var nowX = e.clientX, nowY = e.clientY;
    			var disX = nowX - params.currentX, disY = nowY - params.currentY;
    			target.style.left = parseInt(params.left) + disX + "px";
    			target.style.top = parseInt(params.top) + disY + "px";
    		}
    		
    		if (typeof callback == "function") {
    			callback(parseInt(params.left) + disX, parseInt(params.top) + disY);
    		}
    	}	
    };
    
  • 相关阅读:
    .NET——编写一个计算器
    利用Jieba对txt进行分词操作并保存在数据库中
    软件设计——代理模式之婚介所
    不单单要学程序,也要学穿衣服
    第一篇,就写今天看的东西
    Python中的参数传递问题
    LINUX基础内容
    python中时间相关问题,仅作为笔记
    appium自动化测试
    HTTP协议返回状态码
  • 原文地址:https://www.cnblogs.com/hualiu0/p/5091570.html
Copyright © 2020-2023  润新知