点击图片后图片随鼠标移动/图片拖拽
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title</title>
<style>
img{
position: absolute;
}
</style>
</head>
<body>
<img src="images/tianshi.gif" alt="" id="img">
<script>
document.onmousemove=function (e) {
document.getElementById("img").style.left=e.clientX+"px";
document.getElementById("img").style.top=e.clientY+"px";
};
</script>
</body>
</html>
导航例子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
* {
margin: 0;
padding: 0
}
ul {
list-style: none
}
body {
background-color: #333;
}
.nav {
width: 800px;
height: 42px;
margin: 100px auto;
background: url(images/rss.png) right center no-repeat;
background-color: #fff;
border-radius: 10px;
position: relative;
}
.nav li {
width: 83px;
height: 42px;
text-align: center;
line-height: 42px;
float: left;
cursor: pointer;
}
.nav span {
position: absolute;
top: 0;
left: 0;
width: 83px;
height: 42px;
background: url(images/cloud.gif) no-repeat;
}
ul {
position: relative;
}
</style>
</head>
<body>
<div class="nav">
<span id="cloud"></span>
<ul id="navBar">
<li>北京校区</li>
<li>上海校区</li>
<li>广州校区</li>
<li>深圳校区</li>
<li>武汉校区</li>
<li>关于我们</li>
<li>联系我们</li>
<li>招贤纳士</li>
</ul>
</div>
<script>
function animate(element, target) {
clearInterval(element.timeId);
element.timeId = setInterval(function () {
var current = element.offsetLeft;
var step = (target - current) / 10;
step = step > 0 ? Math.ceil(step) : Math.floor(step);
current += step;
element.style.left = current + "px";
if (current == target) {
clearInterval(element.timeId);
}
console.log("目标位置:" + target + ",当前位置:" + current + ",每次移动步数:" + step);
}, 20);
}
var cloud = document.getElementById("cloud");
var list = document.getElementById("navBar").children;
for (var i = 0; i < list.length; i++) {
list[i].onmouseover = mouseoverHandle;
list[i].onclick = clickHandle;
list[i].onmouseout = mouseoutHandle;
}
function mouseoverHandle() {
animate(cloud, this.offsetLeft);
}
var lastPosition = 0;
function clickHandle() {
lastPosition = this.offsetLeft;
}
function mouseoutHandle() {
animate(cloud, lastPosition);
}
</script>
</body>
</html>
鼠标摆尾
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title</title>
<style>
*{
margin: 0;
padding: 0;
}
body{
height: 2000px;
}
img{
position: absolute;
}
</style>
</head>
<body>
<img src="images/bird.png" alt="" id="img" />
<script src="common.js"></script>
<script>
document.onmousemove=function (e) {
function getScroll() {
return {
left:window.pageXOffset||document.body.scrollLeft||document.documentElement.scrollLeft||0,
top:window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop||0
}
}
document.getElementById("img").style.left=window.event.clientX+getScroll().left+"px";
document.getElementById("img").style.top=window.event.clientY+getScroll().top+"px";
};
</script>
</body>
</html>
最终版封装
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title</title>
<style>
*{
margin: 0;
padding: 0;
}
body{
height: 2000px;
}
img{
position: absolute;
}
</style>
</head>
<body>
<img src="images/bird.png" alt="" id="im" />
<script src="common.js"></script>
<script>
var evt={
getEvent:function (evt) {
return window.event||evt;
},
getClientX:function (evt) {
return this.getEvent(evt).clientX;
},
getClientY:function (evt) {
return this.getEvent(evt).clientY;
},
getScrollLeft:function () {
return window.pageXOffset||document.body.scrollLeft||document.documentElement.scrollLeft||0;
},
getScrollTop:function () {
return window.pageYOffset||document.body.scrollTop||document.documentElement.scrollTop||0;
},
getPageX:function (evt) {
return this.getEvent(evt).pageX? this.getEvent(evt).pageX:this.getClientX(evt)+this.getScrollLeft();
},
getPageY:function (evt) {
return this.getEvent(evt).pageY?this.getEvent(evt).pageY:this.getClientY(evt)+this.getScrollTop();
}
};
document.onmousemove=function (e) {
document.getElementById("im").style.left=evt.getPageX(e)+"px";
document.getElementById("im").style.top=evt.getPageY(e)+"px";
};
</script>
</body>
</html>