jQuery自定义图片缩放拖拽插件imageQ实现方法(附demo源码下载)


Posted in Javascript onMay 27, 2016

本文实例讲述了jQuery自定义图片缩放拖拽插件imageQ实现方法。分享给大家供大家参考,具体如下:

综合网上一些代码 自己写的一个图片缩放拖拽的小插件

/**
 *
 * <a href="http://lib.csdn.net/base/22" class='replace_word' title="jQuery知识库" target='_blank' style='color:#df3434; font-weight:bold;'>jQuery</a> imageQ plugin
 * @name jquery-imageQ.js
 * @author Q
 * @date 2011-01-19
 * maxRatio 最大放大比例
 * minRatio 最小缩小比例
 * changeRadio 每次变化幅度
 * picUrl:图片地址,
 * picWidth:图片宽度,
 * picHeight:图片高度,
 * reset:是否重设图片
 *
 */
(function($){
  var status = false;
  $.fn.imageQ = function(options){
    var defaults = {
      maxRatio:4,
      minRatio:4,
      changeRadio:0.2,
      picUrl:'',
      picWidth:306,
      picHeight:372,
      reset:false
    }
    var options=jQuery.extend(defaults,options);
    return this.each(function(){
      status = true;
      $(this).attr('src','');
      $(this).attr('src',options.picUrl);
      var naturalwidth = options.picWidth;
      var naturalheight = options.picHeight;
      var minwidth = Math.round(naturalwidth/options.minRatio);
      var minheight = Math.round(naturalheight/options.minRatio);
      var maxwidth = Math.round(naturalwidth*options.maxRatio);
      var maxheight = Math.round(naturalheight*options.maxRatio);
      if(options.reset)
      {
        $("#wrapDiv").css("width",naturalwidth+"px");
        $("#wrapDiv").css("height",naturalheight+"px");
        $("#wrapDiv").css("top",'0px');
        $("#wrapDiv").css("left",'0px');
      }
      else
      {
        $(this).css("width","100%");
        $(this).css("height","100%");
        $(this).wrap("<div id='wrapDiv' style='-moz-user-select: none;width:"+naturalwidth+"px;height:"+naturalheight+"px;cursor:move;position:relative;top:0px;left:0px;visibility: visible;' ondragstart='return false;' onselectstart='return false;'></div>");
        $("#wrapDiv").before('<div style="visibility: visible; height: 26px; width: 78px; display: block; position: absolute; line-height: 1px; cursor: pointer; left: 0px; top: 0px;z-index:1;"><div id="plusTool"></div><div id="minusTool"></div><div id="moveTool"></div></div>');
        //$("#wrapDiv").append('<div style="display: block; position: relative; left: 0px; top: 0px; width: 100%; height: 100%; -moz-user-select: none;" id="uploaduserpng"></div>');
        $("#plusTool").attr('title','放大');
        $("#minusTool").attr('title','缩小');
        $("#moveTool").attr('title','拖动');
        $("#plusTool").bind("click",function(){
          _changeOperate('plus');
        });
        $("#minusTool").bind("click",function(){
          _changeOperate('minus');
        });
        $("#moveTool").bind("click",function(){
          _changeOperate('move');
        });
        function plusOperate()
        {
          $("#wrapDiv").unbind();
          $("#wrapDiv").unbind();
          $("#wrapDiv").bind("click",function(){
              var h = $("#wrapDiv").height();
              var w = $("#wrapDiv").width();
              if(Math.round(h*(1+options.changeRadio)) >= maxheight)
              {
                var newH = maxheight;
              }
              else
              {
                var newH = Math.round(h*(1+options.changeRadio));
              }
              if(Math.round(w*(1+options.changeRadio)) >= maxwidth)
              {
                var newW = maxwidth;
              }
              else
              {
                var newW = Math.round(w*(1+options.changeRadio));
              }
              $("#wrapDiv").css("width",newW+"px");
              $("#wrapDiv").css("height",newH+"px");
            });
        }
        function minusOperate()
        {
          $("#wrapDiv").unbind();
          $("#wrapDiv").unbind();
          $("#wrapDiv").bind("click",function(){
              var h = $("#wrapDiv").height();
              var w = $("#wrapDiv").width();
              if(Math.round(h*(1-options.changeRadio)) <= minheight)
              {
                var newH = minheight;
              }
              else
              {
                var newH = Math.round(h*(1-options.changeRadio));
              }
              if(Math.round(w*(1-options.changeRadio)) <= minwidth)
              {
                var newW = minwidth;
              }
              else
              {
                var newW = Math.round(w*(1-options.changeRadio));
              }
              $("#wrapDiv").css("width",newW+"px");
              $("#wrapDiv").css("height",newH+"px");
            });
        }
        function moveOperate()
        {
          $("#wrapDiv").unbind();
          var _move = false;
          var _x,_y;
          $("#wrapDiv").bind("mousedown",function(e){
            _setCursor('grabbing');
            _move = true;
            if(!document.all)
            {
              _x = e.pageX - parseInt($("#wrapDiv").css("left"));
              _y = e.pageY - parseInt($("#wrapDiv").css("top"));
            }
            else
            {
              var pagex = e.clientX+(document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft);
              var pagey = e.clientY+(document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop);
              _x = pagex - parseInt($("#wrapDiv").css("left"));
              _y = pagey - parseInt($("#wrapDiv").css("top"));
            }
          });
          $("#wrapDiv").bind("mouseup",function(e){
            _setCursor('grab');
            _move = false;
          });
          $("#wrapDiv").bind("mousemove",function(e){
            if(_move)
            {
              if(!document.all)
              {
                var pagex = e.pageX;
                var pagey = e.pageY;
              }
              else
              {
                var pagex = e.clientX+(document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft);
                var pagey = e.clientY+(document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop);
              }
              var x = pagex-_x;
              var y = pagey-_y;
              $("#wrapDiv").css("top",y);
              $("#wrapDiv").css("left",x);
            }
          });
        }
        function _setCursor(type){
          $("#wrapDiv").css("cursor","url('images/cursors/"+type+".cur'),default");
        }
        function _changeOperate(operate)
        {
          if(operate == 'plus')
          {
            _setCursor('zoom-in');
            plusOperate();
          }
          if(operate == 'minus')
          {
            _setCursor('zoom-out');
            minusOperate();
          }
          if(operate == 'move')
          {
            _setCursor('grab');
            moveOperate();
          }
        }
      }
    });
  };
  $.fn.imageQ.getStatus = function()
  {
    return status;
  };
})(jQuery);

完整实例代码点击此处本站下载。

PS:在此小编为大家推荐几款javascript格式化美化工具,相信在以后的开发中可以派上用场:

C语言风格/HTML/CSS/json代码格式化美化工具:
http://tools.3water.com/code/ccode_html_css_json

在线JavaScript代码美化、格式化工具:
http://tools.3water.com/code/js

JavaScript代码美化/压缩/格式化/加密工具:
http://tools.3water.com/code/jscompress

在线JSON代码检验、检验、美化、格式化工具:
http://tools.3water.com/code/json

json代码在线格式化/美化/压缩/编辑/转换工具:
http://tools.3water.com/code/jsoncodeformat

希望本文所述对大家jQuery程序设计有所帮助。

Javascript 相关文章推荐
Mootools 1.2教程(2) DOM选择器
Sep 14 Javascript
js 小数取整的函数
May 10 Javascript
JavaScript中的运算符种类及其规则介绍
Sep 26 Javascript
jquery动态分页效果堪比时光网
Sep 25 Javascript
jquery简单图片切换显示效果实现方法
Jan 14 Javascript
json定义及jquery操作json的方法
Sep 29 Javascript
JavaScript cookie详解及简单实例应用
Dec 31 Javascript
JavaScript下拉菜单功能实例代码
Mar 01 Javascript
Angular CLI 安装和使用教程
Sep 13 Javascript
angular2 ng2-file-upload上传示例代码
Aug 23 Javascript
如何使用puppet替换文件中的string
Dec 06 Javascript
vue3使用vuedraggable实现拖拽功能
Apr 06 Vue.js
JS集成fckeditor及判断内容是否为空的方法
May 27 #Javascript
js实现div模拟模态对话框展现URL内容
May 27 #Javascript
详解jQuery中的deferred对象的使用(一)
May 27 #Javascript
基于JS实现密码框(password)中显示文字提示功能代码
May 27 #Javascript
使用jQuery制作Web页面遮罩层插件的实例教程
May 26 #Javascript
Node.js的npm包管理器基础使用教程
May 26 #Javascript
JavaScript中的各种操作符使用总结
May 26 #Javascript
You might like
php生成WAP页面
2006/10/09 PHP
基于xcache的配置与使用详解
2013/06/18 PHP
解析VS2010利用VS.PHP插件调试PHP的方法
2013/07/19 PHP
PHP面向对象之后期静态绑定功能介绍
2015/05/18 PHP
apache和PHP如何整合在一起
2015/10/12 PHP
form表单传递数组数据、php脚本接收的实例
2017/02/09 PHP
如何用PHP做到页面注册审核
2017/03/02 PHP
php设计模式之观察者模式实例详解【星际争霸游戏案例】
2020/03/30 PHP
Javascript中自动切换焦点实现代码
2012/12/15 Javascript
JavaScript的21条基本知识点
2014/03/04 Javascript
浅谈javascript回调函数
2014/12/07 Javascript
JavaScript设计模式之建造者模式介绍
2014/12/28 Javascript
JavaScript实现梯形乘法表的方法
2015/04/25 Javascript
onclick和onblur冲突问题的快速解决方法
2016/04/28 Javascript
vue项目中使用ueditor的实例讲解
2018/03/05 Javascript
node前端开发模板引擎Jade的入门
2018/05/11 Javascript
基于vue开发微信小程序mpvue-docs跳转页面功能
2019/04/10 Javascript
vue百度地图 + 定位的详解
2019/05/13 Javascript
layui动态表头的实现代码
2019/08/22 Javascript
javascript中innerHTML 获取或替换html内容的实现代码
2020/03/17 Javascript
前端如何实现动画过渡效果
2021/02/05 Javascript
[53:23]Secret vs Liquid 2018国际邀请赛淘汰赛BO3 第二场 8.25
2018/08/29 DOTA
python实现的一个火车票转让信息采集器
2014/07/09 Python
Python实现对百度云的文件上传(实例讲解)
2017/10/21 Python
纯python进行矩阵的相乘运算的方法示例
2019/07/17 Python
Python学习笔记之文件的读写操作实例分析
2019/08/07 Python
python 控制台单行刷新,多行刷新实例
2020/02/19 Python
Python读取二进制文件代码方法解析
2020/06/22 Python
CSS3 text-shadow实现文字阴影效果
2016/02/24 HTML / CSS
英国家居用品和床上用品零售商:P&B Home
2020/01/16 全球购物
美国精品地毯网站:Boutique Rugs
2020/03/04 全球购物
介绍一下Linux内核的排队自旋锁
2014/01/04 面试题
护士自我鉴定怎么写
2014/02/07 职场文书
餐厅总厨求职信
2014/03/04 职场文书
协议书格式模板
2016/03/24 职场文书
table设置超出部分隐藏,鼠标移上去显示全部内容的方法
2022/12/24 HTML / CSS