jQuery实现鼠标拖动图片功能


Posted in jQuery onMarch 04, 2021

本例利用jQuery实现一个鼠标托动图片的功能。

首先设一个wrapper,wrapper内的坐标即图片移动的坐标

#wrapper{
      width: 1000px;
      height:1000px;
      position:relative;
    }

设置图片div,这个div即要拖动的div

#div1{
      position: absolute;
      left:0px;
      top:0px;
      width: 300px;
      height: 200px;
      background: url("d:/Pictures/Earth.jpg");
      background-size:contain;
    }

上面设置了wrapper的定位为relative,div1的定位为absolute。

接下来设计拖动的算法:

思路如下:

1.鼠标点下时让div跟随鼠标移动

2.鼠标松开时停止跟随

首先需要一个函数,他会将该div的坐标改变为当前鼠标的位置:

首先需要定义几个变量,保存当前鼠标的坐标以及图片的坐标

var timer;
      var mouseX=0;
      var mouseY=0;
      var pic_width = parseInt($("#div1").css("width")); 
      var pic_height = parseInt($("#div1").css("height"));

那么现在就需要为wrapper添加一个事件监听器,鼠标在wrapper中移动时,修改变量mousex,mousey的值

$("#wrapper").mousemove(function(e){
        mouseX = e.clientX;
        mouseY = e.clientY;
      });

编写follow函数,并用计时器调用它

$("#div1").mousedown(function(){
        timer=setInterval(follow,10);
      });
      $("#div1").mouseup(function(){
        clearInterval(timer);
      });
      var follow = function(){

        $("#div1").css("left",mouseX-pic_width/2);
        $("#div1").css("top",mouseY-pic_height/2);
      };

完整代码如下所示:

<!doctype html>
<html>
  <head>
    <script type = "text/javascript" src = "jquery.js"></script>
    <style type = "text/css">
    #wrapper{
      width: 1000px;
      height:1000px;
      position: relative;
      background: linear-gradient(lightblue,white);
      font-size: 40px;
    }
    #div1{
      position: absolute;
      left:0px;
      top:0px;
      width: 300px;
      height: 200px;
      background: url("d:/Pictures/Earth.jpg");
      background-size:contain;
    }
    </style>
  </head>
  <body>
    <div id = "wrapper">
      Lorem, ipsum dolor sit amet consectetur adipisicing elit. Impedit numquam accusamus explicabo praesentium laudantium et accusantium, ab ipsum, excepturi necessitatibus quos iste ad qui deleniti sed debitis reiciendis quam nisi.
      <div id = "div1">

      </div>
    </div>
    
    
    <script>
      
      var timer;
      var mouseX=0;
      var mouseY=0;
      var pic_width = parseInt($("#div1").css("width")); 
      var pic_height = parseInt($("#div1").css("height")); 

      
      $("#wrapper").mousemove(function(e){
        mouseX = e.clientX;
        mouseY = e.clientY;
      });

      $("#div1").mousedown(function(){
        timer=setInterval(follow,10);
      });
      $("#div1").mouseup(function(){
        clearInterval(timer);
      });
      var follow = function(){

        $("#div1").css("left",mouseX-pic_width/2);
        $("#div1").css("top",mouseY-pic_height/2);
      };
    </script>
  </body>
</html>

最终效果:

jQuery实现鼠标拖动图片功能

到此这篇关于jQuery实现鼠标拖动图片功能的文章就介绍到这了,更多相关jQuery鼠标拖动图片内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

jQuery 相关文章推荐
jquery实现图片平滑滚动详解
Mar 22 jQuery
jQuery实现动态生成表格并为行绑定单击变色动作的方法
Apr 17 jQuery
jQuery实现frame之间互通的方法
Jun 26 jQuery
jQuery插件jsonview展示json数据
May 26 jQuery
使用jQuery动态设置单选框的选中效果
Dec 06 jQuery
jquery实现动态创建form并提交的方法示例
May 27 jQuery
jQuery实现input[type=file]多图预览上传删除等功能
Aug 02 jQuery
jQuery zTree插件使用简单教程
Aug 16 jQuery
jQuery设置下拉框显示与隐藏效果的方法分析
Sep 15 jQuery
jQuery实现提交表单时不提交隐藏div中input的方法
Oct 08 jQuery
jQuery实现聊天对话框
Feb 08 jQuery
jQuery实现影院选座订座效果
Apr 13 jQuery
ajax jquery实现页面某一个div的刷新效果
Mar 04 #jQuery
jquery实现广告上下滚动效果
Mar 04 #jQuery
html5以及jQuery实现本地图片上传前的预览代码实例讲解
Mar 01 #jQuery
jQuery是用来干什么的 jquery其实就是一个js框架
Feb 04 #jQuery
jQuery使用hide()、toggle()函数实现相机品牌展示隐藏功能
Jan 29 #jQuery
jquery实现点击左右按钮切换图片
Jan 27 #jQuery
jquery实现穿梭框功能
Jan 19 #jQuery
You might like
php array_flip() 删除数组重复元素
2009/01/14 PHP
Zend Framework框架实现类似Google搜索分页效果
2016/11/25 PHP
php apache开启跨域模式过程详解
2019/07/08 PHP
用jscript实现列出安装的软件列表
2007/06/18 Javascript
JavaScript面向对象编程入门教程
2014/04/16 Javascript
jQuery幻灯片特效代码分享--鼠标滑过按钮时切换(2)
2020/11/18 Javascript
jQuery垂直多级导航菜单代码分享
2015/08/18 Javascript
深入解析JavaScript的闭包机制
2015/10/20 Javascript
jquery ajax结合thinkphp的getjson实现跨域的方法
2016/06/06 Javascript
AngularJS在IE下取数据总是缓存问题的解决方法
2016/08/05 Javascript
windows下vue-cli导入bootstrap样式
2017/04/25 Javascript
jquery replace方法去空格
2017/05/08 jQuery
详解vue中的computed的this指向问题
2018/12/05 Javascript
小程序显示弹窗时禁止下层的内容滚动实现方法
2019/03/20 Javascript
Vue filter 过滤当前时间 实现实时更新效果
2019/12/20 Javascript
jquery选择器和属性对象的操作实例分析
2020/01/10 jQuery
JS sort方法基于数组对象属性值排序
2020/07/10 Javascript
python备份文件的脚本
2008/08/11 Python
Python编程使用NLTK进行自然语言处理详解
2017/11/16 Python
python实现键盘控制鼠标移动
2020/11/27 Python
python for 循环获取index索引的方法
2019/02/01 Python
Python实现的矩阵转置与矩阵相乘运算示例
2019/03/26 Python
python pygame实现滚动横版射击游戏城市之战
2019/11/25 Python
Python使用matplotlib绘制Logistic曲线操作示例
2019/11/28 Python
如何写python的配置文件
2020/06/07 Python
Python接口自动化测试框架运行原理及流程
2020/11/30 Python
css3的focus-within选择器的使用
2020/05/11 HTML / CSS
HTML5 Canvas自定义圆角矩形与虚线示例代码
2013/08/02 HTML / CSS
HTML5 Canvas玩转酷炫大波浪进度图效果实例(附demo)
2016/12/14 HTML / CSS
获奖的大学生创业计划书
2014/01/05 职场文书
担保书格式及范文
2014/04/01 职场文书
中考标语大全
2014/06/05 职场文书
债务追讨授权委托书范本
2014/10/16 职场文书
幽默导游词开场白
2015/05/29 职场文书
python实现高效的遗传算法
2021/04/07 Python
将Python代码打包成.exe可执行文件的完整步骤
2021/05/12 Python