1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 鼠标点击某处时可以让小精灵移动到鼠标点击的地方 让精灵跟随鼠标移动

鼠标点击某处时可以让小精灵移动到鼠标点击的地方 让精灵跟随鼠标移动

时间:2023-12-04 06:20:59

相关推荐

鼠标点击某处时可以让小精灵移动到鼠标点击的地方  让精灵跟随鼠标移动

鼠标点击页面某处的时候可以让小精灵移动到该处

css代码:

<style>html,body {height: 100%;width: 100%;background-color: black;/* 根据图片的背景色调整页面背景色 */overflow: hidden;}img {width: 100px;/* border: 1px solid red; *//* 定位 */position: absolute;}</style>

html代码:

<img src="./images/girl.gif" alt="" />

script代码

var oImg = document.querySelector("img");//在整个文档里移动//获取元素的自身宽度// oImg.offsetWidth//oImg.offsetHeightdocument.onmousemove = function (e) {oImg.style.left = e.pageX - oImg.offsetWidth / 2 + "px";oImg.style.top = e.pageY - oImg.offsetHeight / 2 + "px";};//鼠标点击document.onclick = function (e) {oImg.style.left = e.pageX - oImg.offsetWidth / 2 + "px";oImg.style.top = e.pageY - oImg.offsetHeight / 2 + "px";};

小精灵图片:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。