javascript--鼠标拖拽窗口案例(鼠标按下,在鼠标移动过程中,盒子跟着一起移动,鼠标松开,盒子停止移动)...

界面如图所示:

要求:在“信息注册”栏,按下鼠标,然后鼠标在页面移动,在鼠标移动过程中,该窗口跟着鼠标移动,当鼠标松开的时候,窗口停止移动。点击“关闭”,该窗口隐藏。

实现思路:

1.页面结构分析:一个大盒子d-box来确定位置,里面放一个小盒子drop(存放“注册信息(可以拖拽”文字和span标签,span存放“关闭”,id为box_close),鼠标在drop中按下,移动时候d-box跟着移动。

2.实现分析:

   2.1 获取鼠标在盒子中的位置。通过鼠标在页面中的位置-盒子在页面的位置实现

       ①鼠标在页面的位置实现代码:

       var x=e.pageX || e.clientX + (document.body.scrollLeft || document.documentElement.scrollLeft); 

       var y=e.pageY || e.clientY + (document.body.scrollTop || document.documentElement.scrollTop); 

  ②盒子在页面中位置实现代码:

    var box_x=box.offsetLeft; 

    var box_y=box.offsetTop; 

  ③鼠标在盒子中的位置实现代码:

    var mouse_in_box_x=x-box_x; 

    var mouse_in_box_y=y-box_y; 

  2.2 获取鼠标移动时盒子移动的位置。通过鼠标移动时,鼠标在页面的位置-鼠标在盒子中的位置实现。

    ①鼠标在页面的位置

      var x=e.pageX || e.clientX + (document.body.scrollLeft || document.documentElement.scrollLeft); 

      var y=e.pageY || e.clientY + (document.body.scrollTop || document.documentElement.scrollTop); 

  ②鼠标移动时盒子移动的位置代码实现:

   var boxX=x-mouse_in_box_x; 

      var boxY=y-mouse_in_box_y; 

   2.3 给盒子样式赋值

   box.style.left=boxX+'px';

      box.style.top=boxY+'px'; 

3.注意:

①要移动的盒子,其pisition必须赋值,absolute或者relative,必须使其脱离文档流才能移动

②给盒子样式赋值时,一定要注意不能丢掉px单位

③事件对象e,位置e.pageX,滚动条滚动距离的兼容性。以前的博文里面有记录,完整代码中也做了处理。

完整代码如下:

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8">
 5         <title>鼠标拖拽案例</title>
 6         
 7         <style type="text/css">
 8             *{
 9                 margin: 0px;
10                 padding: 0px;
11             }
12             
13             .d-box{
14                 width: 400px;
15                 height: 300px;
16                 border: 5px solid #eee;
17                 box-shadow: 2px 2px 2px 2px #666;
18                 position: absolute;
19                 top: 40%;
20                 left: 40%;
21                 background-color: white;
22             }
23             
24             .hd{
25                 height: 25px;
26                 background-color: #7c9299;
27                 color: white;
28                 line-height: 25px;
29                 cursor: move;
30                 border-bottom: 1px solid #369;
31             }
32             #box_close{
33                 float: right;
34                 cursor: pointer;
35             }
36         </style>
37     </head>
38     <body>
39         <div id="d-box" class="d-box">
40             <div id="drop" class="hd">注册信息(可以拖拽)
41                 <span id="box_close">【关闭】</span>
42             </div>
43         </div>
44         
45         <!-- 插入JS代码 -->
46         <script type="text/javascript">
47             /****************************盒子拖拽功能********************************/ 
48             //点击拖拽的“注册信息栏”,拖动整个盒子的移动。首先获得要拖拽的盒子
49             var box=document.getElementById('d-box');
50             var drop=document.getElementById('drop');
51             drop.onmousedown=function(e){
52                 e = e || window.event;
53                 //当鼠标按下的时候,获得鼠标在盒子中的位置
54                 //鼠标在盒子中的位置=鼠标在页面中的位置-盒子在页面的位置
55                 //鼠标在页面中的位置
56                 var x=e.pageX || e.clientX + (document.body.scrollLeft || document.documentElement.scrollLeft);
57                 var y=e.pageY || e.clientY + (document.body.scrollTop || document.documentElement.scrollTop);
58                 
59                 //盒子在页面中的位置
60                 var box_x=box.offsetLeft;
61                 var box_y=box.offsetTop;
62                 //鼠标在盒子中的位置
63                 var mouse_in_box_x=x-box_x;
64                 var mouse_in_box_y=y-box_y;
65                 
66                 //注册鼠标移动事件,因为鼠标按下后,在页面移动,盒子跟着移动
67                 document.onmousemove=function(e){
68                     e = e || window.event;
69                     //鼠标在页面中移动时,求盒子的坐标
70                     //鼠标移动时盒子移动位置=鼠标当前位置-鼠标在盒子中移动的距离
71                      x=e.pageX || e.clientX + (document.body.scrollLeft || document.documentElement.scrollLeft);
72                      y=e.pageY || e.clientY + (document.body.scrollTop || document.documentElement.scrollTop);
73                     
74                     var boxX=x-mouse_in_box_x;
75                     var boxY=y-mouse_in_box_y;
76                     //px一定不能忘
77                     box.style.left=boxX+'px';
78                     box.style.top=boxY+'px';
79                 }
80             }
81             
82             //当鼠标弹起时,移出鼠标移动事件
83             drop.onmouseup=function(){
84                 document.onmousemove=null;
85             }
86             
87             //点击关闭盒子,隐藏盒子
88             var box_close=document.getElementById('box_close');
89             box_close.onclick=function(){
90                 box.style.display='none';
91             }
92         </script>
93     </body>
94 </html>

 

转载于:https://www.cnblogs.com/WangYujie1994/p/10254182.html

JavaScript之修复拖拽的bug 简易的拖拽  需要用到的对象和事件: Event  对象 Event  对象代表事件的状态,比如事件在其中发生的元素、键盘按键的状态、鼠标的位置、鼠标按钮的状态,事件通常与函数结合使用,函数不会在事件发生前被执行! Event  对象的产生当用户单击某个元素的时候,我们给这个元素注册的事件就会触发,该事件的本质就是一个函数,而该函数的形参接收一个event对象. onmousedow 阅读详情

相关推荐

JavaScript实现拖拽盒子效果

本文实例为大家分享了JavaScript实现拖拽盒子效果的具体代码,供大家参考,具体内容如下 实现效果: 1.单击关闭,关闭盒子 2.点击左上方‘点击注册’,打开盒子 3.鼠标放在盒子上方蓝色区域显示十字 4.点击鼠标不松开就可移动盒子 5.松开鼠标图片暂停 实现步骤: 1.在页面最上方设置一个盒子,里面有一个超链接,超链接里面设置href=”[removed]void(0);”,再点击时不打开网页。 2.再设置一个可以拖拽的盒子,盒子嵌套一个蓝色盒子为的最上方的蓝色区域,蓝色盒子里面两个span标签,里面写‘关闭’和‘可拖拽’。 3.给‘关闭’的盒子注册点击事件,点击后,拖拽盒子样式

Js实现div随鼠标移动的方法(点住鼠标移动,松开停止)

Title

weixin_49457021的博客 1386

Cadence DSP 算子开发实战:从入门到精通

本文详细介绍了Cadence DSP算子开发的实战技巧,从入门基础到高级优化。内容涵盖DSP架构特性、内存管理、分块计算策略、SIMD指令优化等核心知识,并提供了性能调优和调试的实用技巧,帮助开发者快速掌握Cadence DSP开发精髓,提升图像处理等计算密集型任务的开发效率。

weixin_29278805的博客 444

js实现鼠标拖拽和原轨迹返回

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>拖拽</title> <style type="text/css"> html, body { overflow: hidden; } body, div, ...

crazyFlex的博客 1051

SAP 货币类型和公司代码的货币设置

货币类型分为公司代码和集团货币,一般FI 10类型和集团货币 30,事务代码是8KEM; 设置货币类型的事务代码是OB22 在S/4 1809版本里编辑功能统合到事务代码FINSC_LEDGER 中了,这里集中了分类账和公司代码的设置; 设置多账簿的币种(会出现在BSEG和FAGLFLEXA表中) 汇率维护在TCURR表中,可通过BAPI_EXCHANGERATE_GETDETAIL函数获取。 CO货币的事务代码是成本控制范围货币:OKKP ,一般设置为货币码20和30; 另外, SCC4设置集团货币,

RichardYang2020的博客 1万+

​银行IT运维人必看!IT运维监控指标大全,一篇讲透

文章摘要:本文从七个维度系统阐述了IT运维指标体系,涵盖机房环境、基础设施、网络设施、数据存储、虚拟化平台、数据库中间件和应用层。作者结合20年金融行业经验,详细分析了各层级关键监控指标、正常范围及异常后果,并给出典型故障场景。重点强调现代运维需从被动响应转向主动预防,通过精细化指标管理实现系统稳定性保障。文章特别指出,在国产化与DevOps背景下,运维工作需兼顾传统架构与新兴技术,构建全方位监控体系,将运维从"故障报警"升级为"业务守护"。

最近开始研究AIGC领域技术,欢迎交流 1108

html移动光标左右上下健,JS实现盒子跟着鼠标移动及键盘方向键控制盒子移动效果示例...

本文实例讲述了JS实现盒子跟着鼠标移动及键盘方向键控制盒子移动。分享给大家供大家参考,具体如下:1. 盒子跟着鼠标移动content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">Document*{margin:0;padding:0;}div{...

weixin_35947010的博客 879

PON架构(全光网络)

目前组网架构世界上有一种最快的速度又是光,以前传统以太网络规划满足不了现在的需求。有线网 无线网 全光网络方案场景 全光网络分类 以太全光网络 PON(Pas-sive-Optical Network 无源光网络) 再典型的中大型高校网络中 推荐万兆入室 千兆到桌面的带宽设计 一般三层 (终端-接入层-汇聚层-核心层-出口(安全设备)-互联网) 单校区核心设备 一般采用两台高性能的核心交换机 双机的方式采用的是 M-LAG 多校区之间核心交换机采用1

qq_56248592的博客 6281

鼠标跟随案例:用js实现盒子跟随鼠标移动

鼠标跟随案例:实现盒子跟随鼠标移动

YY的博客 2732

4D毫米波雷达天线设计

多种天线布局方案介绍、天线设计要点介绍

5746

别再被 SerialPort 坑了!C# 串口通信的 3 大隐形陷阱与工业级解决方案

NET的SerialPort类更适合实验室调试,而非工业现场——它的设计目标是“能用”,而非“稳定”。工业级串口通信的核心,是在SerialPort基础上构建一层“防护壳”,用主动轮询对抗事件失效,用硬件级操作解决缓冲区谎言,用原子性流程避免关闭死锁。本文提供的类已在30+工业项目中验证(包括汽车生产线、智能仓储、环境监测),可直接替换原生SerialPort,解决90%以上的“玄学问题”。记住:工业场景的“稳定”,从来都不是靠运气,而是靠对每个细节的偏执把控。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 755

java实现鼠标拖动挡板移动_javascript--鼠标拖拽窗口案例(鼠标按下,在鼠标移动过程中,盒子跟着一起移动,鼠标松开,盒子停止移动)...

界面如图所示: 要求:在“信息注册”栏,按下鼠标,然后鼠标在页面移动,在鼠标移动过程中,该窗口跟着鼠标移动,当鼠标松开的时候,窗口停止移动。点击“关闭”,该窗口隐藏。实现思路:1.页面结构分析:一个大盒子d-box来确定位置,里面放一个小盒子drop(存放“注册信息(可以拖拽”文字和span标签,span存放“关闭”,id为box_close),鼠标在drop中按下,移动时候d-box跟着移动。2...

weixin_33739387的博客 513

使用curl命令出现fatal: unable to access ‘https://github.com/nvm-sh/nvm.git/‘: Encountered end of file的解决方案

本文主要介绍了使用curl命令出现fatal: unable to access ‘https://github.com/nvm-sh/nvm.git/’: Encountered end of file的解决方案,希望能对使用curl命令的同学们有所帮助。 文章目录 1. 问题描述 2. 解决方案

weixin_43178406的博客 1万+

使用js拖拽盒子移动()

使用js拖拽盒子移动 所用到的方法有onmousemove , onmouseup , onmousedown 按下鼠标开始拖拽,松下及停止 此方法用函数实现 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>鼠标事件</title> &...

qq_44983621的博客 1458

Qcom(基础篇)ROM烧录

文章摘要:该文档介绍了设备烧录操作流程。首先连接设备进入烧录模式,然后选择编译生成的配置文件,路径包括target和workspace目录下的qfil-files文件夹。重点操作包括加载prog_firehose_ddr_SM6225.elf文件,通过LoadXML选择不带efuse的配置文件,最后完成烧录。整个过程涉及文件路径选择和关键烧录步骤的执行。

weixin_49303682的博客 127

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

vue2精简方式实现鼠标在方框内拖拽效果源码

vue实现鼠标拖拽效果,盒子跟随鼠标移动,在方框范围内实现鼠标拖动盒子移动效果。

qq_44994453的博客 2784

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

Html模拟鼠标移动,原生js及H5模拟鼠标点击拖拽

一.原生js1、拖拽的流程动作鼠标按下触发onmousedown事件鼠标移动触发onmousemove事件鼠标松开触发onmouseup事件2、注意事项:要防止div移出可视框,要限制div移动的横纵坐标;防止火狐的bug, 要在最后写上return false,阻止默认事件;防止鼠标运动时移出div,所以要用document.onmousemove和document.onmouseup,不...

weixin_30420027的博客 1899

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

利用JS实现页面元素拖拽

利用JS实现卡片拖拽 需求分析: 拖拽的流程: 当鼠标在被拖拽元素上按下时,开始拖拽(onmousedown) 当鼠标移动时被拖拽的元素跟随鼠标移动 ????️(onmousemove) 当鼠标松开时,被拖拽的元素固定在当前位置(onmouseup) 前置知识: 「1」通过JS修改或读取元素的CSS样式 「2」什么是事件对象?什么是事件冒泡?如何阻止事件冒泡?中的什么是事件对象。 编码详情: 一、创建一个盒子,????值为“box1” 二、编写JS代码 为什么需要计算盒子的偏移量呢❓ 是因为倘若我

ChuanyangChen的博客 2728

js拖拽事件

<!doctype html> <html> <head> <metacharset="utf-8"> <title>index</title> <style> *{ margin:0px; padding: 0px; } #box1{ width:100...

yanjin_xiaoxiao的博客 5544

鼠标按下盒子跟随鼠标移动,鼠标松开,盒子停止移动

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document&l

y18774585870的博客 598

JavaScript经典案例之鼠标拖拽

实现原理:鼠标按下时根据onmousemove事件来动态获取鼠标坐标位置以此来更新div的位置,实现的前提是div要有一个定位效果,不然的话是移动不了它的。 首先先分析一下需求,这个需求就是点击时鼠标按下你才能移动并改变div在页面中的位置,鼠标松开你就不能在移动了。所以这里鼠标的状态有三个,分别是点击时鼠标按下(mousedown事件)、移动时鼠标移动(mousemove事件)、松开时鼠标抬起(mouseup事件) 1. 鼠标拖拽时,记录鼠标按下的位置,具体分析如下图 其中e.pageX,e..

weixin_53278262的博客 704

JS实现拖拽效果

拖拽效果也叫模态框,都是表述得同一个效果。 问题描述: 点击链接会弹出一个框框(我这里是登录框),鼠标点击不松手可以实现拖拽框框移动。点击框上得关闭按钮,可以关闭框。 先来看看效果 这个案例的主要分三部分来完成 1. 点击文字,登陆框的显示和隐藏 2. 鼠标按下,拖拽登陆框移动 3. 鼠标松开,登陆框位置不变了 具体得步骤代码里会有详细得解释得。 这里值得注意得点:鼠标...

爱前端的程序媛的博客 555

从零开始学JavaScript——JS拖拽三大事件

说到拖拽,我们要知道的三大事件 onmousedown(鼠标按下选择元素) onmousemove(按住鼠标移动元素) onmousedown(鼠标松开释放元素) 接下来我们来看一个小案例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge">

m0_46186102的博客 1813

案例:模态框拖拽

案例:模态框拖拽 业务需求: 弹出框,我们也称为模态框。 点击弹出层, 会弹出模态框, 并且显示灰色半透明的遮挡层。 点击关闭按钮,可以关闭模态框,并且同时关闭灰色半透明遮挡层。 鼠标放到模态框最上面一行,可以按住鼠标拖拽模态框在页面中移动。 鼠标松开,可以停止拖动模态框移动。 案例分析: ① 点击弹出层, 模态框和遮挡层就会显示出来 display:block; ② 点击关闭按钮,模态框和遮挡层就会隐藏起来 display:none; ③ 在页面中拖拽的原理: 鼠标按下并且移动, 之后松开鼠标。 ④

icemwj的博客 441

js拖拽效果

拖拽过程中,鼠标移动过程中,获得最新的值赋值给div的left和top值,这样div就可以跟着鼠标走了。鼠标点击div并且不松开鼠标就可以实现拖拽的效果,松开鼠标后鼠标再移动,div不会再跟着移动。鼠标的坐标减去鼠标在div内的坐标就是div的真正位置。一下代码理解了思路后也可进行更改,大致步骤就是这样。...

SqlloveSyn的博客 809
上一篇: javascript--setTimeout定时器
下一篇: javascript--自定义弹出登陆窗口(弹出窗)
de7439
博客等级 码龄12年 10粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值