javascript - 鼠标移动时放大图像 : reaching all corners

标签 javascript jquery css aspect-ratio zooming

我正在研究缩放功能。此缩放是一个具有 100% 窗口大小的固定框和一个具有固定框宽度 200% 的图像内部。

这个缩放需要像这样工作:

  • 当光标位于窗口中心时,图像应位于中心。
  • 当光标在右上角时,图像应该停留在窗口的右上角(所以用 Angular 到达图像)
  • 当光标在中间下 Angular 时,图片应该水平居中并到达底部,这样我们就可以看到图片的中间底部。
  • 等等。

我接近了,但无法完美到达 Angular 落。这是我的代码片段(请参阅 onmousemove 函数中的注释):

var Zoom = function(imageZoom) {
  this.urlImage = imageZoom;
  this.img = undefined;
  this.$img = undefined;

  this.init = function() {
    this.loaders("on");
    this.calcs();
  };
  this.calcs = function() {
    var self = this;
    this.img = new Image();
    this.img.onload = function() {
      self.build();
    };
    this.img.src = this.urlImage;
  };
  this.loaders = function(status) {
    switch(status) {
      case "on":
        $('#loader').fadeIn(200);
        break;
      case "off":
        $('#loader').fadeOut(200);
        break;
    }
  };
  this.build = function() {
    var self = this;
    this.$img = $(self.img);
    
    $('#zoom').fadeIn(200).append(this.$img);
    
    this.$img.on('mousedown', function(e) {
      e.preventDefault();
    });
    
    // this is the problematic function
    $('body').on('mousemove', function(e) {
      e.preventDefault();
      // calc the percents of the window where
      var px = 100 * e.pageX / $(window).width(); 
      var py = 100 * e.pageY / $(window).height();

      // calc of the percent pixel of the image
      var fx = self.$img.width() * px / 100;
      var fy = self.$img.height() * py / 100;

      // render it left / 2 and top / 1.5 (the 1.5 value is imaginary!!)
      self.$img.css({'transform': 'translate('+ -(fx/2) +'px, '+ -(fy/1.5)+'px)'});
    });
    self.loaders("off");
  };
};

var zoom = new Zoom("http://dummyimage.com/2000x1230/000/fff");
zoom.init();
#zoom {
	position: fixed;;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	z-index: 1000000;
	display: none;
}
#zoom img {
	width: 200%;
	height: auto;
	position: absolute;
	cursor: crosshair;
	-webkit-touch-callout: none;
	-webkit-user-select: none;
	-khtml-user-select: none;
	-moz-user-select: none;
	-ms-user-select: none;
	user-select: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="loader">Loading</div>
<div id="zoom"></div>

问题是我放置了 fx/1.5 因为 fx/2 不起作用。但是水平值非常有效。

我可以配置什么值来到达所有 Angular 落?为什么左边的值(像素除以 2)在顶部值不工作时工作得很好?

最佳答案

你们真的很亲密。获取垂直百分比时,只需计算image.height - window.height即可。

这也适用于不同的图像纵横比,因为它在计算时会考虑图像高度。宽度不是问题,因为它始终是窗口宽度的两倍。

var Zoom = function(imageZoom) {
  this.urlImage = imageZoom;
  this.img = undefined;
  this.$img = undefined;

  this.init = function() {
    this.loaders("on");
    this.calcs();
  };
  this.calcs = function() {
    var self = this;
    this.img = new Image();
    this.img.onload = function() {
      self.build();
    };
    this.img.src = this.urlImage;
  };
  this.loaders = function(status) {
    switch (status) {
      case "on":
        $('#loader').fadeIn(200);
        break;
      case "off":
        $('#loader').fadeOut(200);
        break;
    }
  };
  this.build = function() {
    var self = this;
    this.$img = $(self.img);

    $('#zoom').fadeIn(200).append(this.$img);

    this.$img.on('mousedown', function(e) {
      e.preventDefault();
    });

    // this is the problematic function
    $('body').on('mousemove', function(e) {
      e.preventDefault();

      var wx = $(window).width();
      var wy = $(window).height();
      var iy = self.$img.height();

      var px = e.pageX;
      var py = e.pageY;

      var tx = -1 * (px / wx) * wx;
      var ty = -1 * (py / wy) * (iy - wy);

      self.$img.css({
        'transform': 'translate(' + tx + 'px, ' + ty + 'px)'
      });
    });
    self.loaders("off");
  };
};

var zoom = new Zoom("http://dummyimage.com/2000x1200/000/fff");
zoom.init();
#zoom {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1000000;
  display: none;
}
#zoom img {
  width: 200%;
  height: 100%;
  height: auto;
  position: absolute;
  cursor: crosshair;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
body {
  margin: 0;
}
* {
  box-sizing: border-box;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="loader">Loading</div>
<div id="zoom"></div>

作为奖励:

如果你想去除图片高宽比(jsfiddle)时留下的空白区域,你可以确保图片高度至少等于窗口高度(jsfiddle)。但是,在这种情况下,您需要设置 width: auto,因此图像不会始终是窗口宽度的两倍。

关于javascript - 鼠标移动时放大图像 : reaching all corners,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38703966/

相关文章:

javascript - prevAll() 在更复杂的 DOM 中不起作用

html - 考试 70-480 : CSS order of anchor elements

css - 在 Eclipse 4 中使用 CSS 设置背景颜色和背景图像

javascript - 如何通过网络套接字连接到 Raspberry Pi 上的 Mosquitto 代理?

javascript - 如何让一个对象绕着html5 Canvas 中的一个点转动

javascript - 交叉请求错误: "Origin is not allowed by Access-Control-Allow-Origin"?

javascript - 导航栏中的水平图标

jquery - 了解 AJAX 响应中图像何时完成加载

jquery - 如何正确滑动导航栏

javascript - window.location重新加载页面后如何激活onbeforeunload?