JS:跟随鼠标移动的图片,兼容IE与DOM浏览器(已在实际中用懂啊)
来源:互联网 发布:illuststudio mac 编辑:程序博客网 时间:2024/06/05 05:06
http://blog.sina.com.cn/s/blog_48e6792c0100z8ie.html
基本设计思路:
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>跟随鼠标移动的图片</title><script type="text/javascript">var mouse_x = 0;var mouse_y = 0; //鼠标坐标var img_x =0;var img_y = 0; //图片坐标var move_to_x = 0;var move_to_y = 0; //移动目标位置坐标var step = 5; //图片每次移动距离var interval = 10; //间隔时间移动var scroll_x;var scroll_y; //页面在水平、垂直方向上已经滚动的距离var image_width;var image_height;function getPos(e){ scroll_x = window.pageXOffset ? window.pageXOffset : document.documentElement.scrollLeft; scroll_y = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop; //获取窗口大小 win_width = windows.innerWidth ? windows.innerWidth : document.body.clientWidth; win_height = windows.innerHeight ? windows.innerHeight : document.body.clientHeight; //图片尺寸 image_width = document.getElementByIdx_x("image_id").width; image_height = document.getElementByIdx_x("image_id").height; //获取鼠标所在位置 e = e ? e : event mouse_x = e.clientX; mouse_y = e.clientY; //计算move_to_x,move_to_y值,为图片指定移动边界,避免图片移动到窗口外 if(mouse_x < 2) { move_to_x = scroll_x + 2; } else if(mouse_x > win_width - image_width - 20) { move_to_x = scroll_x + win_width - image_width - 20; } else { move_to_x = scroll_x + mouse_x; } if(mouse_y < 2) { move_to_y = scroll_y + 2; } else if(mouse_y > win_height - image_height - 20) { move_to_y = scroll_y + win_height - image_height - 20; } else { move_to_y = scroll_y + mouse_y; } return; //移动图片}function moveImage(){ var img = document.getElementByIdx_x("div_id"); //图片当前位置 img_x = parseInt(img.style.left); img_y = parseInt(img.style.top); //比较目标位置与图片当前位置,若不等,将img_x按 step距离计算,重新赋值给img.style.left; if(Math.abs(move_to_x - img_x) > 5) { img_x += step * ((move_to_x - img_x > 5) ? 1 : -1); img.style.left = img_x + "px"; } if(Math.abs(move_to_y - img_y) > 5) { img_y += step * ((move_to_y - img_y > 5) ? 1 : -1); img.style.top = img_y + "px"; } setTimeout("moveImage()",interval);}</script></head><body id="id_body" onload="moveImage()" onmousemove="getPos(event)"><div id="div_id" style="position:absolute; left:0px; top:0px; z-index:10;"><img id="image_id" src="images/gkbm.gif" /></div><table height="1000px" style="background:#ccc"><tr><td></td></tr></table></body></html>
0 0
- JS:跟随鼠标移动的图片,兼容IE与DOM浏览器(已在实际中用懂啊)
- Dom编程(图片 在 某一区域内 跟随鼠标运动、仅IE浏览器支持)
- IE浏览器下的JS鼠标特效(文本跟随鼠标环绕的特效)
- 一个跟随鼠标的图片放大效果,与FF兼容
- JS实现图片跟随鼠标移动
- 图片跟随鼠标移动
- js 鼠标放置图片展示大图并跟随鼠标移动
- 用js实现的图片在浏览器里面来回滚动效果 <兼容ie和ff>
- 图片跟随鼠标移动(jquery)
- js实现鼠标拖动图片 兼容IE,FF火狐,谷歌浏览器
- js在ie、火狐等浏览器的兼容
- 跟随鼠标移动放大图片
- [JS实例] 跟随鼠标移动而发生视差动画的图片
- js中关于IE与DOM对浏览器的兼容性
- 简单的鼠标经过图片时变大,IE低版本浏览器不兼容
- js兼容IE下载图片在本地
- js实现跟随鼠标移动的提示框
- JS特效--跟随鼠标移动的日历时钟
- 'org.springframework.orm.hibernate3.LocalSessionFactoryBean' not found解决方法
- C#笔记15:序列化
- 作为程序员,其实你并没真正努力(二)
- jquery validate 配置
- Qt QLineEdit 限制输入类型以及大小Int 和double
- JS:跟随鼠标移动的图片,兼容IE与DOM浏览器(已在实际中用懂啊)
- 【第四章】 资源 之 4.3 访问Resource ——跟我学spring3
- 编绎显示Unknown type name “CGFloat” 错误解决方法
- VxWorks中三元组mBlk的原理
- java多线程消息队列的实现
- 外键的作用
- Implement Stack using Queues
- linux系统相关命令
- Windows/Linux下引用jar包,并用javac/java编译运行