您的位置:首页 > Web前端 > JavaScript

jsp中使用js随意移动图片位置效果

2009-06-26 16:00 691 查看
带边框的,只能在边框内移动

<%@ 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>
</head>
<body>
<script>
var xx = 0, yy =0
</script>
<div id=woyingjie
style="position: absolute; top: 0px; left: 0px; width: 1000px; height: 600px; background: green;"></div>
<img src="ys.gif" style='position: absolute;'
ondragstart="setCapture();xx=event.x-this.offsetLeft;yy=event.y-this.offsetTop;"
ondrag='woyingjie(this)' ondragend="releaseCapture();">
<img src="1ys.gif" style='position: absolute;'
ondragstart="setCapture();xx=event.x-this.offsetLeft;yy=event.y-this.offsetTop;"
ondrag='woyingjie(this)' ondragend="releaseCapture();">

<script>
function woyingjie(o) {
if (event.x - xx > 1000 - 90) {//此处90代表图片宽度
o.style.left = 1000 - 90
} else {
if (event.x - xx < 0) {
o.style.left = 0
} else {
o.style.left = event.x - xx
}
}
if (event.y - yy > 600 - 30) {此处30代表图片高度

o.style.top = 600 - 30
} else {
if (event.y - yy < 0) {
o.style.top = 0
} else {
o.style.top = event.y - yy
}
}
}
</script>
</body>
</html>

不带边框,可以移动带外面!

<%@ 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>
</head>

<body>
<img src="ys.gif" style="position: absolute; width: 200; height: 200;"
onmousedown=
MouseDown(this);;
onmousemove=
MouseMove();;
onmouseup=
MouseUp()
>

<script type="text/javascript">
var Obj
function MouseDown(obj) {
Obj = obj;
Obj.setCapture();
Obj.l = event.x - Obj.style.pixelLeft;
Obj.t = event.y - Obj.style.pixelTop;
}
function MouseMove() {
if (Obj != null) {
Obj.style.left = event.x - Obj.l;
Obj.style.top = event.y - Obj.t;
}
}
function MouseUp() {
if (Obj != null) {
Obj.releaseCapture();
Obj = null;
}
}
</script>
<marquee direction="left">让他从右向左慢慢滚动出来</marquee>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: