[javascript]一种兼容性比较好的简单拖拽
作为一个马上要找工作、非计算机专业、热爱前端的大四狗,最近开始疯狂写demo、看书,准备九、十月份的校招。
晚上用js实现了一个比较简单(low)的拖拽效果,初步测试兼容性还是不错的,于是写一段小博文记录下~大神求轻喷
面向过程的写法
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style>
- #box {
- width: 100px;
- height: 100px;
- background: red;
- position: absolute;
- }
- </style>
- </head>
- <body>
- <div id="box"></div>
- <script>
- window.onload = function (){
- var disX = 0;
- var disY = 0;
- var oBox = document.getElementById("box");
- oBox.onmousedown = function (ev){
- var oEvent = ev || event;
- oBox.style.cursor = "move";
- disX = oEvent.clientX - oBox.offsetLeft;
- disY = oEvent.clientY - oBox.offsetTop;
- document.onmousemove = function (ev){
- var oEvent = ev || event;
- var theLeft = oEvent.clientX - disX;
- var theTop = oEvent.clientY - disY;
- // 禁止用户从浏览器左框拖出
- if (theLeft < 0) {
- theLeft = 0;
- } else if (theLeft > document.documentElement.clientWidth-
- oBox.offsetWidth) {
- theLeft = document.documentElement.clientWidth-
- oBox.offsetWidth;
- } else if (theTop < 0) {
- theTop = 0;
- } else if (theTop > document.documentElement.clientHeight-
- oBox.offsetHeight) {
- theTop = document.documentElement.clientHeight-
- oBox.offsetHeight;
- }
- oBox.style.left = theLeft + 'px';
- oBox.style.top = theTop + 'px';
- }
- }
- document.onmouseup = function (){
- document.onmousemove =null;
- }
- // 窗口重设大小时的处理方法
- window.onresize = function (){
- oBox.style.left = document.documentElement.clientWidth/2-oBox.offsetWidth/2 + 'px';
- oBox.style.top = document.documentElement.clientHeight/2-oBox.offsetHeight/2 + 'px';
- }
- // 兼容firefox 3.6.19
- return false;
- }
- </script>
- </body>
- </html>
创建一个拖拽对象
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style>
- #box,#box2 {
- width: 100px;
- height: 100px;
- background: red;
- position: absolute;
- }
- </style>
- </head>
- <body>
- <div id="box"></div>
- <div id="box2"></div>
- <script>
- window.onload = function (){
- new Drag("box");
- new Drag("box2");
- }
- function Drag(id){
- var _this = this;
- this.disX = 0;
- this.disY = 0;
- this.oBox = document.getElementById(id);
- this.oBox.onmousedown = function (ev){
- _this.fnDown(ev);
- // 兼容firefox 3.6.19
- return false;
- };
- document.onmouseup = function (){
- _this.fnUp();
- }
- window.onresize = function (){
- _this.fnResize();
- };
- // 兼容firefox 3.6.19
- return false;
- }
- Drag.prototype.fnDown = function(ev){
- var oEvent = ev || event;
- var _this = this;
- this.oBox.style.cursor = "move";
- this.disX = oEvent.clientX - this.oBox.offsetLeft;
- this.disY = oEvent.clientY - this.oBox.offsetTop;
- document.onmousemove = function (ev){
- _this.fnMove(ev);
- };
- }
- Drag.prototype.fnMove = function(ev){
- var oEvent = ev || event;
- var theLeft = oEvent.clientX - this.disX;
- var theTop = oEvent.clientY - this.disY;
- // 禁止用户从浏览器左框拖出
- if (theLeft < 0) {
- theLeft = 0;
- } else if (theLeft > document.documentElement.clientWidth-
- this.oBox.offsetWidth) {
- theLeft = document.documentElement.clientWidth-
- this.oBox.offsetWidth;
- }
- if (theTop < 0) {
- theTop = 0;
- } else if (theTop > document.documentElement.clientHeight-
- this.oBox.offsetHeight) {
- theTop = document.documentElement.clientHeight-
- this.oBox.offsetHeight;
- }
- this.oBox.style.left = theLeft + 'px';
- this.oBox.style.top = theTop + 'px';
- }
- Drag.prototype.fnUp = function (){
- document.onmousemove =null;
- }
- Drag.prototype.fnResize = function(){
- this.oBox.style.left = document.documentElement.clientWidth/2-this.oBox.offsetWidth/2 + 'px';
- this.oBox.style.top = document.documentElement.clientHeight/2-this.oBox.offsetHeight/2 + 'px';
- }
- </script>
- </body>
- </html>
[javascript]一种兼容性比较好的简单拖拽的更多相关文章
- 学习笔记---Javascript事件Event、IE浏览器下的拖拽效果
学习笔记---Javascript事件Event.IE浏览器下的拖拽效果 1. 关于event常用属性有returnValue(是否允许事件处理继续进行, false为停止继续操作).srcE ...
- 练习:javascript弹出框及地址选择功能,可拖拽
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 移动端多个DIV简单拖拽功能
移动端多个DIV简单拖拽功能. 这个demo与之前写的一个例子差不了多少,只是这个多了一层遍历而已. <!DOCTYPE html> <html lang="en" ...
- javascript小实例,PC网页里的拖拽
几年前,我参与设计开发一个房产网的项目,我负责前端工作,由于项目经理要求比较高,参考了很多房产类网站比较优秀的功能,想把别人比较优秀的设计和想法集合到一起,那时的设计稿和功能实现,简直就是改了又改,今 ...
- javascript小实例,PC网页里的拖拽(转)
这是现在的效果,可能改了一些,原来的效果是,里面的这张图是可以上下左右拖动的,然后房子上面的显示的楼栋号,也跟着图片一起移动,当时js能力还不行,未能实现项目经理的要求,不过后来项目经理又把这个效果推 ...
- Unity UGUI 实现简单拖拽功能
说到拖拽,那必然离不开坐标,UGUI 的坐标有点不一样,它有两种坐标,一种是屏幕坐标,还有一种就是 UI 在Canvas内的坐标(暂时叫做ugui坐标),这两个坐标是不一样的,所以拖拽就需要转换. 因 ...
- javascript简单拖拽(鼠标事件 mousedown mousemove mouseup)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- Javascript:简单拖拽效果的实现
核心代码: /* *完成一个拖拽事件由三大事件组成: *1:onmousedown:选择元素 *2:onmousemove:移动元素 *3:onmouseup:释放元素 */ function dra ...
- 原生js通过prottype写的一个简单拖拽
<!DOCTYPE html> <head> <meta charset="utf-8"/> <title></title&g ...
随机推荐
- bzoj 1857: [Scoi2010]传送带 三分
题目链接 1857: [Scoi2010]传送带 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 934 Solved: 501[Submit][Stat ...
- 高质量程序设计指南C/C++语言——C++/C常量(2)
- RFID电子标签的二次注塑封装
生活当中,RFID电子标签具有明显的优势,随着RFID电子标签成本的降低.读写距离的提高.标签存储容量增大及处理时间缩短的发展趋势,R F I D电子标签的应用将会越来越广泛. RFID电子标签的应用 ...
- HDU 1498 50 years, 50 colors
题目大意:给你一个 n*n 的矩阵,每个格子上对应着相应颜色的气球,每次你可以选择一行或一列的同种颜色的气球进行踩破,问你在K次这样的操作后,哪些颜色的气球是不可能被踩破完的. 题解:对于每一种颜色建 ...
- 【Leetcode】Triangle
给定一个由数字组成的三角形,从顶至底找出路径最小和. Given a triangle, find the minimum path sum from top to bottom. Each step ...
- java代码发送JSON格式的httpPOST请求
package com.test; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOE ...
- [置顶] js操作iframe兼容各种浏览器
在做项目时,遇到了操作iframe的相关问题.业务很简单,其实就是在操作iframe内部某个窗体时,调用父窗体的一个函数.于是就写了两个很简单的htm页面用来测试,使用网上流行的方法在谷歌浏览器中始终 ...
- 大一C语言结课设计之《简单计算器》
/*===============================================*\ ** 设计目的:简单计算器,计算形如10*(20.2-30.6)+5.0/2的表达式值 ** 简 ...
- c#关于EXCEL导出数据库的做法
using System;using System.Diagnostics;using System.Collections;using System.Data;using System.Web;us ...
- C#中静态方法的运用和字符串的常用方法(seventh day)
又来到了今天的总结时间,由于昨天在云和学院学的知识没有弄懂,今天老师又专门给我们非常详细地讲了一遍,在这里非常谢谢老师.O(∩_∩)O 话不多说,下面就开始为大家总结一下静态方法的运用和字符串的常用方 ...