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

本文介绍了一种在网页中实现图片拖拽的方法,并通过JavaScript控制拖拽范围,确保图片只在指定区域内移动,同时提供了另一种不限制边界的拖拽方式。

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

带边框的,只能在边框内移动

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值