前端页面实现鼠标滚动缩放图片,鼠标左键拖拽图片,div内部禁止页面滚动

1、写入html页面

 <section>
      <div class="container">
           <div class="test-box">
                <img id="wrap" class="test-img" src="img/cai.png"  alt="" width="100%"/>
            </div>
      </div>
 </section>

2、为html设置基本样式(container)以及相对定位样式

<style>
        .container{
            padding: 0px 15px 0px 15px;
            margin-left: auto;
            margin-right: auto;
        }
        .test-box {
            height: 1020px;
            border: 1px solid #F1F3F4;
            overflow: hidden;
            margin: 30px auto;
            position: relative;
        }
        .test-img {
            position: absolute;
            top: 0;
            left: 0;
        }
</style>

3、引入jquery文件,网上任意版本自行下载,放至动效代码最前面(需要有jquery文件才能驱使jquery代码生效)

<script src="dist/jquery.min.js"></script>

4、加入动效代码(js/jquery)

 <script>
    // 缩放
    var imgZoom = {
         init: function() { 
             this.zoomImage();
         },
         zoomImage: function() {
             var _this = this;
             $('.test-box').off('mousewheel').on('mousewheel', '.test-img', function(e) {
                 _this.mouseScroll($(this));
             });
             $('.test-box').off('DOMMouseScroll').on('DOMMouseScroll', '.test-img', function(e) {
                 _this.mouseScroll($(this), e);
             });
         },
         mouseScroll: function($img,e) {
             var e = e || window.event;
             var oper = Math.max(-1, Math.min(1,(e.wheelDelta || -e.originalEvent.detail)));
             var imgWidth = $img.width();
             var newWidth = Math.max(350, Math.min(12000,imgWidth + (30*oper)));
             var left = parseInt($img.css("left")) - (newWidth - imgWidth) / 2;
             $img.css({
                 "width": newWidth + "px",
                 "left": left + "px"
             })
         },
     };
 
 
 
    // 拖拽
    var imgDrag = function() {
         var isDrag = false;
         var dragTarget;
         var startX, startY;
         var imgPositionTop,imgPositionLeft;
         var boxWidthMin, boxWidthMax, boxHeightMin, boxHeightMax;
         function moveMouse(e) {
             if (isDrag) {
                 var e = window.event || e;
                 var clientY = e.clientY;
                 var clientX = e.clientX;
                 if(clientY >= boxHeightMin && clientY <= boxHeightMax) {
                     dragTarget.style.top = imgPositionTop + clientY - startY + "px";
                 }
                 if(clientX >= boxWidthMin && clientX <= boxWidthMax) {
                     dragTarget.style.left = imgPositionLeft + clientX - startX + "px";
                 }
                 return false;
             }
         }
         function initDrag(e) {
             var e = window.event || e;
             var dragHandle = e.srcElement;
             var topElement = "HTML";
             var eventBtn = e.button == 0 || e.button == 1; // 鼠标左键
 
             while (dragHandle.tagName != topElement && dragHandle.className != "test-img") {
                 dragHandle = dragHandle.parentElement;
             }
             if (dragHandle.className == "test-img" && eventBtn) {
                 isDrag = true;
                 dragTarget = dragHandle;
                 imgPositionTop = parseInt(dragTarget.style.top + 0);
                 startY = e.clientY;
                 imgPositionLeft = parseInt(dragTarget.style.left + 0);
                 startX = e.clientX;
 
                 var initVal = 50;  // 防止图片拖出框内的最小边界值
                 var $box =  $('.test-box');
                 boxWidthMin = $box.offset().left + initVal;
                 boxWidthMax = $box.offset().left + $box.width() - initVal;
                 boxHeightMin = $box.offset().top + initVal;
                 boxHeightMax = $box.offset().top + $box.height() - initVal;
 
                 $(document).unbind('mousemove').bind('mousemove', moveMouse);
                 return false;
             }
         }
 
         $(document).unbind("mousedown").bind("mousedown", initDrag);
         $(document).unbind("mouseup").bind("mouseup", function() {
             isDrag = false;
         });
     };
 
     imgZoom.init();
     imgDrag();
     </script>
     <script>
        window.onload = function () {
  for (i = 0; i < 50; i++) {
  var x = document.createElement('div');
  x.innerHTML = "test<br/>";
//   document.body.appendChild(x);
  }
  function $(x) {
  return document.getElementById(x);
  };
  $("wrap").onmousewheel = function scrollWheel(e) {
  var sl;
  e = e || window.event;
  if (navigator.userAgent.toLowerCase().indexOf('msie') >= 0) {
   event.returnValue = false;
  } else {
   e.preventDefault();
  };
  };
  if (navigator.userAgent.toLowerCase().indexOf('firefox') >= 0) {
    addEventListener('DOMMouseScroll',
   function (e) {
   var obj = e.target;
   var onmousewheel;
   while (obj) {
    onmousewheel = obj.getAttribute('onmousewheel') || obj.onmousewheel;
    if (onmousewheel) break;
    if (obj.tagName == 'BODY') break;
    obj = obj.parentNode;
   };
   if (onmousewheel) {
    if (e.preventDefault) e.preventDefault();
    e.returnValue = false; //禁⽌页⾯滚动
    if (typeof obj.onmousewheel != 'function') {
    //将onmousewheel转换成function
    eval('window._tmpFun = function(event){' + onmousewheel + '}');
    obj.onmousewheel = window._tmpFun;
    window._tmpFun = null;
    };
    // 不直接执⾏是因为若onmousewheel(e)运⾏时间较长的话,会导致锁定滚动失效,使⽤setTimeout可避免
    setTimeout(function () {
     obj.onmousewheel(e);
    },
    1);
   };
   },
   false);
  };
}
     </script>

5、完结

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值