jQuery自定義圖片縮放拖拽插件imageQ實(shí)現(xiàn)方法(附demo源碼下載)
來(lái)源:易賢網(wǎng) 閱讀:951 次 日期:2016-06-20 16:02:06
溫馨提示:易賢網(wǎng)小編為您整理了“jQuery自定義圖片縮放拖拽插件imageQ實(shí)現(xiàn)方法(附demo源碼下載)”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了jQuery自定義圖片縮放拖拽插件imageQ實(shí)現(xiàn)方法,涉及jQuery擴(kuò)展操作及頁(yè)面元素操作技巧,并附帶了完整的demo源碼供讀者下載參考,需要的朋友可以參考下

本文實(shí)例講述了jQuery自定義圖片縮放拖拽插件imageQ實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:

綜合網(wǎng)上一些代碼 自己寫(xiě)的一個(gè)圖片縮放拖拽的小插件

/**

 *

 * <a class='replace_word' title="jQuery知識(shí)庫(kù)" 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:是否重設(shè)圖片

 *

 */

(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','拖動(dòng)');

        $("#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);

希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)