前端插件之拖拽
来源:互联网 发布:2年的java工程师的薪资 编辑:程序博客网 时间:2024/06/06 15:02
1.拖拽的插件很多,其实我们只要明白他的原理,就不难了:
之所以形成拖拽的效果,是在鼠标按下onmousedown的时候记录鼠标坐标与被拖拽div左边界,上边界的距离disX,disY,在拖动onmousemove的时候,讲拖动时鼠标的x坐标oEv.clientX-disX;得到的这个值应该是拖动情况下,div的left;同理,投票也是:
2.js代码:
var oBox=document.getElementById('box');var oPut=oBox.getElementsByTagName('input')[0]; oBox.onmousedown=function(ev){ oEv=ev || window.event; var disX=oEv.clientX-oBox.offsetLeft; var disY=oEv.clientY-oBox.offsetTop; document.onmousemove=function(ev){oEv=ev || window.event;//console.log(oEv.clientX,oEv.clientY)var w=oEv.clientX-disX;var h=oEv.clientY-disY;oBox.style.left=w+"px";oBox.style.top=h+"px";if(w>document.documentElement.clientWidth-oBox.offsetWidth){oBox.style.left=document.documentElement.clientWidth-oBox.offsetWidth+'px';};if(w<0){oBox.style.left="0";};if(h>document.documentElement.clientHeight-oBox.offsetHeight){oBox.style.top=document.documentElement.clientHeight-oBox.offsetHeight+'px';};if(h<0){oBox.style.top="0";};}; oBox.onmouseup=function(){ document.onmousemove=null;};};//---------------------------oPut.onmousedown=function(ev){oEv=ev || window.event;oEv.cancelBubble=true;};2.html代码:
<div id="box"><input type="text">3.css代码,当然很简单
*{margin:0;padding:0;}
#box{width:200px;height:200px;background:#ccc;position:absolute;}
阅读全文
0 0
- 前端插件之拖拽
- 前端之插件-echats
- 前端之插件-fileinput
- 前端之插件-dynatree
- 前端之插件-ztree
- 前端之插件-mCustomScrollbar
- 前端插件之-swiper
- 前端之插件-layer
- 国产前端插件之xgcalendar
- 前端开发之日期插件
- 前端插件之跑马灯
- 前端之js插件-fullPage
- 前端之js-插件篇
- 前端拖拽插件gridster.js
- Gulp前端自动化之 ejs插件使用
- 深度学习edusoho之前端插件
- 前端构建之gulp与常用插件
- 前端发开插件之toastr
- set集合的遍历
- 一切都可以变好
- Window下ffmpeg项目搭建
- Iterator 在迭代中删除元素抛异常
- 矩阵计算的个人小笔记
- 前端插件之拖拽
- centos7 安装SVN
- python 疯狂填词 mad libs
- Spring-Cloud-1
- C——数组总结
- log4j.properties配置文件放置位置以及读取
- 【Java学习】基于Socket的多用户聊天Demo
- 分布式爬虫调度策略
- java并发编程笔记day1