js实现层的移动与上下层的切换,模仿他人代码 --firefox无法实现
2011-06-10 10:43
671 查看
<!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> <mce:script language="javascript" type="text/javascript"><!-- var currentMoveObj = null; //当前拖动对象 var indexnum=0; var relLeft; //鼠标按下位置相对对象位置 var relTop; function f_mdown(obj) { //当对象被按下时,记录该对象 indexnum++; currentMoveObj = obj; //setCapture()可以让对象捕捉到鼠标事件,跟随着鼠标做出响应 currentMoveObj.setCapture(); //设置对象的定位方式为absolute,便于计算拖动位置 currentMoveObj.style.position = "absolute"; currentMoveObj.style.zIndex=indexnum; //记录鼠标按下时距离被移动物体的左上角的偏移量 //以便在移动鼠标的时候正确计算位移 relLeft = event.x - currentMoveObj.style.pixelLeft; relTop = event.y - currentMoveObj.style.pixelTop; } window.document.attachEvent('onmouseup',function(){ //releaseCapture()执行和setCapture()相反的操作 currentMoveObj.releaseCapture(); currentMoveObj = null; //当鼠标释放时同时释放拖动对象 }); function f_move(obj) { if(currentMoveObj != null) { //真正移动鼠标的时候,计算被移动物体的实际位置 currentMoveObj.style.pixelLeft=event.x-relLeft; currentMoveObj.style.pixelTop=event.y-relTop; } } // --></mce:script> </head> <body> <div id="1234" onMouseDown="f_mdown(this)" onMouseMove="f_move(this)"> <img style="background-color:#cccccc;width:60px;height:60px;"></img>1 </div> <div id="12345" onMouseDown="f_mdown(this)" onMouseMove="f_move(this)"> <img style="background-color:#ccccc;width:60px;height:60px;"></img>2 </div> <div id="12346" onMouseDown="f_mdown(this)" onMouseMove="f_move(this)"> <img style="background-color:#ccccc;width:60px;height:60px;"></img>3 </div> </body> </html>
相关文章推荐
- JS动态的把左边列表添加到右边的实现代码(可上下移动)
- JS动态的把左边列表添加到右边的实现代码(可上下移动)
- js实现方块上下左右移动效果
- js实现搜索框智能提示上下移动效果
- 一个实现图片切换的js代码
- js实现简单的动画(4个按钮控制图片上下左右移动)
- jquery实现tr元素的上下移动示例代码
- jquery实现tr元素的上下移动示例代码
- JS实现的文字与图片定时切换效果代码
- 键盘上下键移动选择table表格行的js代码
- js实现兼容IE、Firefox的图片缩放代码
- JavaScript获取移动设备型号的实现代码(JS获取手机型号和系统)
- 左右两个Select列表框交换数据的JS代码,上下左右都可以移动数据,还可以置顶、沉底
- 七行jquery代码实现图片渐变切换【兼容ie6+、 Chrome、 Firefox】
- JS模仿手机端九宫格登录功能实现代码
- 用js模仿word格式刷功能实现代码 [推荐]
- IE,firefox(火狐)浏览器无提示关闭窗口js实现代码小结
- js鼠标点击图片切换效果实现代码
- 图片的左右移动,js动画效果实现代码