jq元素拖拽

摘要:
js12$(function(){3$('#a1').mousedown(function(e){4varpositionDiv=$(this).offset();5vardistenceX=e.pageX-positionDiv.left;6vardistenceY=e.pageY-positionDiv.top;7$(document).mousemove(function(e){8varx=e.pageX-distenceX;9vary=e.pageY-distenceY;10if(x$(document).width()-$('#a1').outerWidth(true)){13x=$(document).width()-$('#a1').outerWidth(true);14}15if(y$(document).height()-$('#a1').outerHeight(true)){18y=$(document).height()-$('#a1').outerHeight(true);19}20$('#a1').css({21'left':x+'px',22'top':y+'px'23});24});25$(document).mouseup(function(){26$(document).off('mousemove');27});28});29});3031

<div id="a1"></div>

js

1 <script type="text/javascript">
2         $(function(){
3           $('#a1').mousedown(function(e){
4             var positionDiv = $(this).offset();
5             var distenceX = e.pageX -positionDiv.left;
6             var distenceY = e.pageY -positionDiv.top;
7             $(document).mousemove(function(e){
8               var x = e.pageX -distenceX;
9               var y = e.pageY -distenceY;
10               if(x<0){
11                 x=0;
12               }else if(x>$(document).width()-$('#a1').outerWidth(true)){
13                 x = $(document).width()-$('#a1').outerWidth(true);
14 }
15               if(y<0){
16                 y=0;
17               }else if(y>$(document).height()-$('#a1').outerHeight(true)){
18                 y = $(document).height()-$('#a1').outerHeight(true);
19 }
20               $('#a1').css({
21                 'left':x+'px',
22                 'top':y+'px'
23 });
24 });
25             $(document).mouseup(function(){
26               $(document).off('mousemove');
27 });
28 });
29 });
30 
31 </script>

免责声明:文章转载自《jq元素拖拽》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Java基础教程——内部类【高性能并行计算】——第四课 线性代数方程组的并行求解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

h5 时间控件问题,怎么设置type =datetime-local 的值

在js中设置自定义时间到date控件的方法: 1、在html5中定义时间控件 <input type="date" value=""/> 2、编写脚本实现自定义时间的赋值 //创建一个当前日期对象 var now = new Date(); //格式化日,如果小于9,前面补0var day = ("0" + now.getDa...

C# 获取 串口 设备名称 与 串口号 ManagementObjectSearcher类

1.效果图: 2.代码 classProgram { static void Main(string[] args) { GetComList(); } private static voidGetComList() {try{...

【一】、.net core 3.1 创建windows服务并集成Serilog的步骤记录

1、新建项目   选择“Worker Service”,如下图: 2、添加nuget引用   Microsoft.Extensions.Hosting.WindowsServices   Serilog.Extensions.Hosting   Serilog.Sinks.Console   Serilog.Sinks.File 3、修改Program的...

要过年啦,用canvas做了个烟火效果

  声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢!   要过年了,过年想到的就是放烟火啦。。。。于是就用canvas写了个放烟火的效果,鼠标点击也会产生烟火,不过不要产生太多烟火哦,一个烟火散出的粒子是30到200个之间,当页面上的粒子数量达到一定的时候,页面就会很卡咯,我也没特意去优化神马的。以后有空再说吧。   直接上DEMO吧:放烟火 ...

JS DOM元素

// 为element增加一个样式名为newClassName的新样式 functionaddClass(element, newClassName) { var value =element.className; element.className = value + " " +newClassName; } var bo...

三级联动下拉菜单1

一:HTML代码 1 <select id='c1'></select> 2 <select id='c2'></select> 3 <select id='c3'></select> 二: 三级菜单数组举例定义(实际从数据库中获取) var city1 = ['请选择','江苏'...