<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
img{
position: fixed;
}
img:nth-of-type(1){
transition: all 0.8s;
}
</style>
</head>
<body>
<img src="01.jpg" class="ran" width="100px" alt="">
<img src="02.jpg" class="move" width="100px" alt="">
<script src="js/jquery.min.js"></script>
<script>
// document是一个原生的对象
// $(document).mousemove(function(e){
// // 窗口的宽度
// console.log($(window).width());
// // 窗口的高度
// console.log($(window).height());
// // e是一个事件对象
// // client距离窗口原点的坐标
// console.log('client'+e.clientY);
// // screen距离显示屏的坐标
// // console.log('screen'+e.screenX);
// // pageX距离页面的坐标
// console.log('page'+e.pageY);
// })
setInterval(function(){
// 获取标签的宽度 获取标签.width()
var x=Math.random()*($(window).width()-$('.ran').width());
var y=Math.random()*($(window).height()-$('.ran').height());
$('.ran').animate({left:x+'px',top:y+'px'})
},1000)
$(document).mousemove(function(e){
// e是一个形参,事件对象,在事件中内置的
var x=e.clientX - $('img').eq(0).width()/2;
var y=e.clientY - $('img').eq(0).height()/2;
$('.move').css('left',x+'px');
$('.move').css('top',y+'px');
})
</script>
</body>
</html>
$(window).width() | 窗口的宽度 |
$(window).height() | 窗口的高度 |
clientX | client距离窗口原点的X坐标 |
clientY | client距离窗口原点的Y坐标 |
pageX | 距离页面的X坐标 |
pageY | 距离页面的Y坐标 |