基于jquery編寫的放大鏡插件

字號:


    本文實例為大家分享了自己動手實現(xiàn)的jquery放大鏡插件,供大家參考,具體內(nèi)容如下
    /**
      ***********************************************
      **此插件使用須知---------------       **
      **濾鏡(inner)與其活動區(qū)(active)之比要等于 **
      **放大區(qū)(movequ)與其內(nèi)部圖片之比。如比值不相 **
      **等,請改變圖片大小.------------------------**
      **參數(shù)介紹                  **
      **active:濾鏡活動區(qū)             **
      **inner:濾鏡                 **
      **movequ:放大區(qū)域              **
      **需在Html頭部引入本js腳本及jquery-1.8.3腳本 **
      **如有疑問,請聯(lián)系QQ:64047399,為你解答   **
      ***********************************************
    **/
    $.fn.extend({
      yangbo:function(active,inner,movequ){
        $(active).hover(function(){
          $(inner).show();
          $(movequ).show();
          var proportionOne=$(active).width()/$(inner).width();
          var proportionTwo=$(movequ).find('img').width()/$(movequ).width();
          // console.log($(movequ).find('img').width());
          if(proportionOne==proportionTwo){
          $(this).mousemove(function(event){
            //以下為右側(cè)放大
            var proportionLeft=$(active).width()/$(inner).width();
            var proportionTop=$(active).height()/$(inner).height();
            $(movequ).scrollLeft($(inner).position().left*proportionLeft).scrollTop($(inner).position()
            .top*proportionTop);
            //以下為小濾鏡拖拽
            $(inner).offset({
                left:event.pageX-40,
                top:event.pageY-40
              });
            //以下為判斷臨界值
            if($(inner).position().left<=0){
                $(inner).css({
                  left:0
                })
              }
              if($(inner).position().top<=0){
                $(inner).css({
                  top:0
                })
              }
              if($(inner).position().left>=$(this).width()-$(inner).width()){
                $(inner).css({
                  left:$(this).width()-$(inner).width()
                })
              }
              if($(inner).position().top>=$(this).height()-$(inner).height()){
                $(inner).css({
                  top:$(this).height()-$(inner).height()
                })
              }
          })
        }else{
          $(active).text('圖片寬高不正確:請調(diào)整圖片寬高-->濾鏡與其父親之比應(yīng)該等于右側(cè)盒子與右側(cè)圖片之比')
          .css({
            background:'yellow',
            color:'red',
          });
        }
      },function(){
          $(inner).hide();
          $(movequ).hide();
        })
      }
    })
    以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。