- <html>
- <head>
- <title>图片拖动</title>
- <style type="text/css">
- /*overflow设为hidden,就不会有滚动条显示,但是滚动条仍然存在,
- 通过scrollTop和scrollLeft操作滚动条
- */
- #wrap{width:500px; height:500px; overflow:hidden; border:1px solid red; margin:50px 300px;}
- #wrap div.inside{background-p_w_picpath:url(p_w_picpaths/java.jpg);
- background-repeat:no-reapt;
- width:1000px;height:1000px;
- cursor:pointer;
- }
- </style>
- </head>
- <body>
- <div id = "wrap">
- <div class="inside"></div>
- </div>
- <script type="text/javascript">
- //获取外层div的对象
- var wrap = document.getElementById("wrap");
- //给外层div添加事件
- wrap.onmousedown = mouseDown;
- wrapwrap.onmouseout = wrap.onmouseup = mouseUp;
- wrap.onmousemove = mouseMove;
- //json法创建一个对象
- //x,y保存鼠标的坐标,buttonDown记录鼠标按下的状态(初始值为0和未按下)
- var point = {x:0, y:0, buttonDown:false};
- function mouseDown(e){
- //获得事件参数对象
- ee = e || window.event;
- //e.offsetX获取鼠标指针相对于事件源元素的的位置
- point.x = e.offsetX;
- point.y = e.offsetY;
- //一旦调用表示已经按下,变为true
- point.buttonDown = true;
- }
- function mouseUp(){
- point.buttonDown = false;
- }
- function mouseMove(e){
- //获得事件参数对象
- ee = e || window.event;
- //鼠标移动并且按下
- if(point.buttonDown){
- //外层div通过加有滚动条而查看内层较大的div内容,所以scrollLeft是外层div的属性
- wrap.scrollLeft += point.x - e.offsetX;
- wrap.scrollTop += point.y - e.offsetY;
- }
- }
- </script>
- </body>
- </html>