1. 程式人生 > >js實現文字拖動

js實現文字拖動

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
.move_out{position: relative;cursor: hand;font-family: "華文行楷"}
</style>
<script type="text/javascript">
var move_out=false;
var z,x,y;
function move(){
	if(event.button==move_out){		
		z.style.pixelLeft=temporarily1+event.clientX-x;
		z.style.pixelTop=temporarily2+event.clientY-y;
		return false;
	}
}
function down(){
	if(event.srcElement.className=="move_out"){
		move_out=true;
		z=event.srcElement;
		temporarily1=z.style.pixelLeft;
		temporarily2=z.style.pixelTop;
		x=event.clientX;
		y=event.clientY;
		document.onmousemove=move;
	}else{
		move_out=false;
	}
}
document.onmousedown=down;
</script>
</head>
<body>
<font color="0000ff" size=10 class="move_out">萬里山河,誰主沉浮</font>
</body>
</html>