js 如何js模拟鼠标滚轮移动

js实现鼠标左右移动,图片也跟着移动效果
转载 &发布时间:日 10:15:54 & 作者:happyzgm
本文主要介绍了js实现鼠标左右移动,图片也跟着移动效果的方法与思路。具有很好的参考价值,下面跟着小编一起来看下吧
效果:鼠标往左移,图片对应右移,鼠标往右移,图片就左移动。图片距离越远偏移距离越大。
思路:首先获取图片原先的距离。设置一个变化值,图片的最终距离等于原先的距离加上变化值
布局:大盒子里面是图片,大盒子position:relative;图片position:
&!doctype html&
&meta charset="utf-8"&
&title&无标题文档&/title&
body{margin:0;}
#wrap{width:800height:500margin:30 border:1px solid #000; position:}
#wrap img{ position:}
#wrap img:nth-of-type(1){ left:200top:200 z-index:0;}
#wrap img:nth-of-type(2){ left:300top:180 z-index:1;}
#wrap img:nth-of-type(3){ left:100top:100 z-index:2;}
#wrap img:nth-of-type(4){ left:400top:110 z-index:3;}
&div id="wrap"&
&img src="http://cdn.attach.qdfuns.com/notes/pics//094455cpacwz1yai2ap43p.jpg.editor.jpg" /&
&img src="http://cdn.attach.qdfuns.com/notes/pics//094455csz3xxx1x23um7e9.jpg.editor.jpg" /&
&img src="http://cdn.attach.qdfuns.com/notes/pics//rzfoov04owrof.jpg.editor.jpg" /&
&img src="http://cdn.attach.qdfuns.com/notes/pics//rzfoov04owrof.jpg.editor.jpg" /&
var oWrap=document.getElementById("wrap");
var aImg=oWrap.getElementsByTagName("img");
var iMax=4;
//获取图片的初始位置
for(var i=0;i&aImg.i++){
aImg[i].startX=parseInt(getStyle(aImg[i],'left'))
oWrap.onmousemove=function(ev){
ev=ev||window.
//获取鼠标的位置与大盒子中心点位置的距离
var iX=ev.clientX-(oWrap.offsetLeft+this.offsetWidth/2)
for(var i=0;i&aImg.i++){
//获取每个img的z-index
var iZindex=getStyle(aImg[i],'zIndex')
//Zindex越大移动的相对距离越小
var iDisL=-parseInt(iX/iMax*(iMax-iZindex)/5)
//图片的距离等于原先的距离加上计算的距离
aImg[i].style.left=aImg[i].startX+iDisL+'px'
function getStyle(obj,attr)
if( obj.currentStyle){
return obj.currentStyle[attr];
return getComputedStyle(obj)[attr];
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具拒绝访问 | www.yo28.cn | 百度云加速
请打开cookies.
此网站 (www.yo28.cn) 的管理员禁止了您的访问。原因是您的访问包含了非浏览器特征(420e7cd-ua98).
重新安装浏览器,或使用别的浏览器您还可以使用以下方式登录
当前位置:&>&&>&&>& > JS实现窗口加载时模拟鼠标移动的方法
JS实现窗口加载时模拟鼠标移动的方法
&  本文实例讲述了JS实现窗口加载时模拟鼠标移动的方法。分享给大家供大家参考。具体实现方法如下:  ?
function judge(){
alert(&mousemove&);
document.onmousemove =//撤销鼠标移动模拟
function simulateMouseMove(){//模拟鼠标移动
document.onmousemove =
window.onload=simulateMouseM//窗口加载
  希望本文所述对大家的javascript程序设计有所帮助。
欢迎转载:
推荐:    实现代码如下:
&!DOCTYPE html&
&html lang="en"&
&meta charset="UTF-8"&
&title&Title&/title&
position: relative;
padding:0;
width:100%;;
height: 1000px;
height: 400px;
width:400px;
position: absolute;
margin-left:-200px;
margin-top:-200px;
background: #ddd;
#small-box{
height: 50px;
width:50px;
position: absolute;
background: red;
cursor:move ;
&div id="box"&
&div id="small-box"&&/div&
window.onload=function(){
var box=document.querySelector('#small-box');
var body=document.querySelector('body');
var index=<span style="background-color: #f5f5f5; color: #;
var x1,y1;
box.onmousedown=function(e){
index=<span style="background-color: #f5f5f5; color: #;
//鼠标按下才能触发onmousemove方法
var x=e.clientX;
//鼠标点击的坐标值,x
var y=e.clientY;
var left= this.style.
left=left.substr(<span style="background-color: #f5f5f5; color: #,left.length-<span style="background-color: #f5f5f5; color: #);
var top=this.style.
top=top.substr(<span style="background-color: #f5f5f5; color: #,top.length-<span style="background-color: #f5f5f5; color: #);
x1=parseInt(x-left);
y1=parseInt(y-top);
box.onmousemove=function(e){
if(index===<span style="background-color: #f5f5f5; color: #){
this.style.left=e.clientX-x1+'px';
this.style.top=e.clientY-y1+'px';
if(this.style.left.substr(<span style="background-color: #f5f5f5; color: #,this.style.left.length-<span style="background-color: #f5f5f5; color: #)&<span style="background-color: #f5f5f5; color: #){
this.style.left=<span style="background-color: #f5f5f5; color: #;
if(this.style.top.substr(<span style="background-color: #f5f5f5; color: #,this.style.top.length-<span style="background-color: #f5f5f5; color: #)&<span style="background-color: #f5f5f5; color: #){
this.style.top=<span style="background-color: #f5f5f5; color: #;
if(this.style.top.substr(<span style="background-color: #f5f5f5; color: #,this.style.top.length-<span style="background-color: #f5f5f5; color: #)&<span style="background-color: #f5f5f5; color: #0){
this.style.top='<span style="background-color: #f5f5f5; color: #0px';
if(this.style.left.substr(<span style="background-color: #f5f5f5; color: #,this.style.left.length-<span style="background-color: #f5f5f5; color: #)&<span style="background-color: #f5f5f5; color: #0){
this.style.left='<span style="background-color: #f5f5f5; color: #0px';
box.onmouseup=function(e){
index=<span style="background-color: #f5f5f5; color: #;
body.onmouseup=function(e){
box.onmouseup();
//类似可以index=0;
阅读(...) 评论()AngularJS实现鼠标移入显示,鼠标移出隐藏 - 简书
AngularJS实现鼠标移入显示,鼠标移出隐藏
&span ng-mouseenter="show = true" ng-mouseleave="show = false"&
Mouse over me.
&div ng-show="show"&Hello!&/div&
在Plunker中预览效果:
Coding is 用智慧开创属于自己的事业!
联系邮箱:
用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金Cover 有什么料? 从这篇文章中你能获得这些料: 知道setContentView()之后发生了什么? ... Android 获取 View 宽高的常用正确方式,避免为零 - 掘金相信有很多朋友...
用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你能获得这些料: 知道setContentView()之后发生了什么? ... Android 获取 View 宽高的常用正确方式,避免为零 - 掘金 相信有很多...
【转】Android 开源项目分类汇总 旭川君已关注
16:49*字数 29527阅读 1795评论 1喜欢 35 来源:https://github.com/Trinea/android-open-project 更多:Android 开源库获取途径...
用到的组件1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SDWebImage多个缩略图缓存组件 UICKeyChainStore存放用户账号密码组件 Reachability监测网络状态 DateTools友好...
今天培训,我们做为销售督导的代表,只拿到了第11名的成绩,可谓异常失败,我们口口声声的喊着第一,第一,第一,却并没有为第一去冲刺,去拼命,没有为经销商做好榜样,没有实现自我突破,大大有愧于专门让我们参加培训的领导,我们必须为我们的失败负责,今天做300个俯卧撑只是小小的惩罚...
喜莱和春莱看着那瓢泼大雨,心里比吃了黄连还苦。地里所有的棉花都破败以后,这场大雨终于停下来了,可对于厂子里的人来说早已经于事无补。最苦的还不是员工们,而是厂长。下雨的那半个多月,厂长天天都去拜佛,可老天最终还是没给个好结果。当看见地里所有棉花都被冲走过后,厂长一阵咒骂“你个...
触动我的心弦 文丨闲人 通过安检,走进省博大厅,“福建古代文明”醒目的白色黑体大字映入眼帘。看到这个字眼,心中充满疑问,闽江大地到底有多少可以呈现的古老文明! 进入展馆,面前立有展览的形象牌。转眼往里望去,整个馆内灯光略显灰暗,不过展示台上方布有射灯,能够大致看到摆在台上的...
昨晚在家长群看到,一个大学少年班的学生,因为在大学期间玩游戏不想学习,因此在大三,挂科太多让其退学,今年考取中山大学,我对此很唏嘘,这个孩子的学习能力非同一般,也是非常聪明之人,大三挂科累积至退学,那么重新修读考上中山大学,还能否重振旗鼓,再次点燃学习的愿望,将大学学业顺利...}

我要回帖

更多关于 js 模拟鼠标移动 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信