使用javascript基于光标缩放

时间:2018-05-16 11:22:31

标签: javascript html css

我们都试图解决这个问题以及搜索多个解决方案的StackOverflow,似乎没有一个正常工作。我有一个似乎很接近但不完全是我正在寻找的最终结果。我试图这样做,当用户使用鼠标滚轮进行缩放时,缩放基于光标的位置。

有人可以解释我在这里做错了什么。在我计算图像偏移的某个地方我做错了,你会在测试时看到它。

 // offset image based on cursor position
  var width = img_ele.getBoundingClientRect().width;
  var height = img_ele.getBoundingClientRect().height;
  var x_cursor = window.event.clientX;
  var y_cursor = window.event.clientY;
  var x = img_ele.offsetLeft;
  var y = img_ele.offsetTop;

  // Calculate displacement of zooming position.
  var dx = x - ((x_cursor - x) * (factor - 1.0));
  var dy = y - ((y_cursor - y) * (factor - 1.0));

  img_ele.style.left = dx + 'px';
  img_ele.style.top = dy + 'px';

以下是完整代码。只需将src图像更改为您喜欢的图像。

<!DOCTYPE html>
<html>
<body>
<div id="container">
  <img ondragstart="return false" id="drag-img" src="map.jpg" />
</div>
<input type="button" id="zoomout" class="button" value="Zoom out">
<input type="button" id="zoomin" class="button" value="Zoom in">
<input type="button" id="zoomfit" class="button" value="Zoom fit">
</body>
</html>
<script>


var img_ele = null,
  x_cursor = 0,
  y_cursor = 0,
  x_img_ele = 0,
  y_img_ele = 0;

function zoom(factor) {
  img_ele = document.getElementById('drag-img');

  // Zoom into the image.
  var width = img_ele.getBoundingClientRect().width;
  var height = img_ele.getBoundingClientRect().height;
  img_ele.style.width = (width * factor) + 'px';
  img_ele.style.height = (height * factor) + 'px';

  // offset image based on cursor position
  var width = img_ele.getBoundingClientRect().width;
  var height = img_ele.getBoundingClientRect().height;
  var x_cursor = window.event.clientX;
  var y_cursor = window.event.clientY;
  var x = img_ele.offsetLeft;
  var y = img_ele.offsetTop;

  // Calculate displacement of zooming position.
  var dx = x - ((x_cursor - x) * (factor - 1.0));
  var dy = y - ((y_cursor - y) * (factor - 1.0));

  img_ele.style.left = dx + 'px';
  img_ele.style.top = dy + 'px';

  console.log('MAP SIZE : ' + width, height);
  console.log('MAP TOP/LEFT : ' + x, y, 'NEW:', dx, dy);
  console.log('CURSOR : ' + x_cursor, y_cursor);
  img_ele = null;

}

function zoom_fit() {
  bb_el = document.getElementById('container')
  img_el = document.getElementById('drag-img');

  var width = bb_el.getBoundingClientRect().width;
  var height = bb_el.getBoundingClientRect().height;

  img_el.style.width = width + 'px';
  img_el.style.height = height + 'px';
  img_el.style.left = '0px';
  img_el.style.top = '0px';
  img_el = null;
}

document.getElementById('zoomout').addEventListener('click', function() {
  zoom(0.9);
});
document.getElementById('zoomin').addEventListener('click', function() {
  zoom(1.1);
});
document.getElementById('zoomfit').addEventListener('click', function() {
  zoom_fit();
});
document.addEventListener('mousewheel', function(event) {
  event.preventDefault();
  x_cursor = window.event.clientX;
  y_cursor = window.event.clientY;
  // console.log('ZOOM : ' + 'X:', x_cursor, 'Y:', y_cursor);

  if (event.deltaY < 0) {
    // console.log('scrolling up');
    zoom(1.1);
  }
  if (event.deltaY > 0) {
    // console.log('scrolling down');
    zoom(0.9);
  }
});

function start_drag() {
  img_ele = this;
  // console.log('inside start_drag var img_ele set to : ' + this);
  x_img_ele = window.event.clientX - document.getElementById('drag-img').offsetLeft;
  y_img_ele = window.event.clientY - document.getElementById('drag-img').offsetTop;
  // console.log('start_drag : ' + 'x_img_ele:', x_img_ele, 'y_img_ele:', y_img_ele);
}

function stop_drag() {
// console.log('stop drag');
  img_ele = null;
}

function while_drag() {
  // console.log('while_drag: ', window.event);
  var x_cursor = window.event.clientX;
  var y_cursor = window.event.clientY;
  if (img_ele !== null) {
    img_ele.style.left = (x_cursor - x_img_ele) + 'px';
    img_ele.style.top = ( window.event.clientY - y_img_ele) + 'px';

    // console.log('while_drag: ','x_cursor', x_cursor, 'x_img_ele', x_img_ele, 'y_cursor', y_cursor, 'y_img_ele', y_img_ele,'Image left and top', img_ele.style.left+' - '+img_ele.style.top);
  }
}

document.getElementById('drag-img').addEventListener('mousedown', start_drag);
document.getElementById('container').addEventListener('mousemove', while_drag);
document.getElementById('container').addEventListener('mouseup', stop_drag);
function while_drag() {
  var x_cursor = window.event.clientX;
  var y_cursor = window.event.clientY;
  if (img_ele !== null) {
    img_ele.style.left = (x_cursor - x_img_ele) + 'px';
    img_ele.style.top = ( window.event.clientY - y_img_ele) + 'px';
    // console.log(img_ele.style.left+' - '+img_ele.style.top);
  }
}

document.getElementById('drag-img').addEventListener('mousedown', start_drag);
document.getElementById('container').addEventListener('mousemove', while_drag);
document.getElementById('container').addEventListener('mouseup', stop_drag);
</script>

<style>
#drag-img {
  cursor: move;
  position: relative;
  width: 400px;
  height: 400px;
}

#container {
  overflow: hidden;
  background: red;
  width: 400px;
  height: 400px;
}

.button {
  width: 80px;
  height: 25px;
}
</style>

0 个答案:

没有答案