不用left/top处理(用padding代替):
<html>
<head>
<script type="text/javascript">
function show_pic(event){
var x = event.clientX + document.body.scrollLeft;
var y = event.clientY + document.body.scrollTop;
var IMG = document.getElementById("img1");
var IMG_height = IMG.height;
var IMG_width = IMG.width;
IMG.style.paddingLeft = x - IMG_width/2;
IMG.style.paddingTop = y - IMG_height/2;
}
</script>
</head>
<body>
<img src="1.jpg" id="img1">
<script type="text/javascript">
document.addEventListener("mousemove",show_pic,'event');
</script>
</body>
</html>
用left/top处理:
<html>
<head>
<style type="text/css">
#img1{
position:absolute; //重点!
}
</style>
<script type="text/javascript">
function show_pic(event){
var x = event.clientX + document.body.scrollLeft;
var y = event.clientY + document.body.scrollTop;
var IMG = document.getElementById("img1");
var IMG_height = IMG.height;
var IMG_width = IMG.width;
IMG.style.left = x - IMG_width/2;
IMG.style.top = y - IMG_height/2;
}
</script>
</head>
<body>
<img src="1.jpg" id="img1">
<script type="text/javascript">
document.addEventListener("mousemove",show_pic,'event');
</script>
</body>
</html>
图片随鼠标伸缩效果:
<html>
<head>
<script type="text/javascript">
function show_pic(event){
var x = event.clientX + document.body.scrollLeft;
var y = event.clientY + document.body.scrollTop;
var IMG = document.getElementById("img1");
IMG.width = x;
IMG.height = y;
}
</script>
</head>
<body>
<img src="1.jpg" id="img1">
<script type="text/javascript">
document.addEventListener("mousemove",show_pic,'event');
</script>
</body>
</html>