javascript - 修改图像缩放逻辑

标签 javascript jquery image

我想在预览 Pane 上设置镜头并将完美图像显示到缩放器 div 中,但无法修复它。我只是从一些 javascript 示例转换我的代码,而且我对 javascript 很差,这就是为什么我需要一些专家的帮助!

我的期望如下图所示:

enter image description here

我目前的发展是这样的:

$(function(){
$('.previewPane, #zoomer').css('background-image','url('+$('.imgkey').first().attr('src')+')');
$('.imgkey').click(function(){
$('.previewPane').css('background-image','url('+$(this).attr('src')+')');
			});
$('.previewPane').mousemove(function(ev){
$('#zoomer').css('display','inline-block');
var img = $(this).css('background-image').replace(/^url\(['"](.+)['"]\)/, '$1');
var posX = ev.offsetX ? (ev.offsetX) : ev.pageX - $(this).offset().left;
var posY = ev.offsetY ? (ev.offsetY) : ev.pageY - $(this).offset().top;
$('#zoomer').css('background-position',((-posX * 3) + "px " + (-posY * 3) + "px"));
$('#zoomer').css('background-image','url('+img+')');
			});
$('.previewPane').mouseleave(function(){$('#zoomer').css('display','none');});
		});
.imgkey{width:50px;height:50px;border:1px solid #ddd;}
.previewPane{display:inline-block;border:1px solid #ddd;width:250px;height:250px;cursor:crosshair;background-repeat:no-repeat;background-position:center;background-size:100% 100%}
#zoomer{display:none;background-repeat:no-repeat;border:1px solid #ddd;width:250px;height:250px;z-index:1000;}
<!DOCTYPE html>
	<html>
	<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  </head>
  <body>
	<main>
		<div class="previewPane"></div><div id="zoomer"></div>
		<div class="imgline">
			<img class="imgkey" src="https://cdn.shopify.com/s/files/1/0622/2101/products/product-image-496137782_800x.jpg?v=1519622126">
			<img class="imgkey" src="https://images-na.ssl-images-amazon.com/images/I/61IPCXn13AL._SX385_.jpg">
			<img class="imgkey" src="https://http2.mlstatic.com/celular-smartphone-caterpillar-s60-negro-32-gb-D_NQ_NP_940281-MCO26572973595_122017-F.jpg">
		</div>
	</main>

我还尝试通过简单的更改来尝试您的代码,例如:只需在“$('.preview').each(function() {”之前添加这两行,但没有运气。

$('.img-zoom-container').html('<div class="preview"><img class="image" src="'+$('.Key').first().attr('src')+'"></div>');


$('.Key').click(function(){
    $('.img-zoom-container').html('<div class="preview"><img class="image" src="'+$(this).attr('src')+'"></div>');
});

以及“img-zoom-container”之外的图像,例如:

<img class="Key" src="https://www.w3schools.com/howto/img_girl.jpg" width="200" height="200">
<img class="Key" src="https://placeimg.com/640/480/animals" width="200" height="200">
<img class="Key" src="https://placeimg.com/640/480/arch" width="200" height="200">

最佳答案

您错过了 W3Schools 的 CSS,并且您的图像 src 无法正常工作,

查看包含多个图像的工作片段。

$('.preview').each(function() {
  var lens, cx, cy, img, result;
  img = $(this).find('img.image')[0];
  result = document.getElementById('result');
  /*create lens:*/
  lens = document.createElement("div");
  lens.setAttribute("class", "img-zoom-lens");
  /*insert lens:*/
  img.parentElement.insertBefore(lens, img);
  /*calculate the ratio between result DIV and lens:*/
  cx = result.offsetWidth / lens.offsetWidth;
  cy = result.offsetHeight / lens.offsetHeight;
  /*set background properties for the result DIV:*/
  
  /*execute a function when someone moves the cursor over the image, or the lens:*/
  $(lens).on('mousemove touchmove', moveLens);
  $(img).on('mousemove touchmove', moveLens);

  function getCursorPos(e) {
    var a, x = 0,
      y = 0;
    e = e || window.event;
    /*get the x and y positions of the image:*/
    a = img.getBoundingClientRect();
    /*calculate the cursor's x and y coordinates, relative to the image:*/
    x = e.pageX - a.left;
    y = e.pageY - a.top;
    /*consider any page scrolling:*/
    x = x - window.pageXOffset;
    y = y - window.pageYOffset;
    return {
      x: x,
      y: y
    };
  }

  function moveLens(e) {
    var pos, x, y;
    result.style.backgroundImage = "url('" + img.src + "')";
      result.style.backgroundSize = (img.width * cx) + "px " + (img.height * cy) + "px";

    /*prevent any other actions that may occur when moving over the image:*/
    e.preventDefault();
    /*get the cursor's x and y positions:*/
    pos = getCursorPos(e);
    /*calculate the position of the lens:*/
    x = pos.x - (lens.offsetWidth / 2);
    y = pos.y - (lens.offsetHeight / 2);
    /*prevent the lens from being positioned outside the image:*/
    if (x > img.width - lens.offsetWidth) {
      x = img.width - lens.offsetWidth;
    }
    if (x < 0) {
      x = 0;
    }
    if (y > img.height - lens.offsetHeight) {
      y = img.height - lens.offsetHeight;
    }
    if (y < 0) {
      y = 0;
    }
    /*set the position of the lens:*/
    lens.style.left = x + "px";
    lens.style.top = y + "px";
    /*display what the lens "sees":*/
    result.style.backgroundPosition = "-" + (x * cx) + "px -" + (y * cy) + "px";
    console.log(result);
  }

})
* {
  box-sizing: border-box;
}

.img-zoom-container {
  
  width: 100%
}

.preview {
  display: inline-block;
  margin: 0 10px;
  position: relative;
}

.img-zoom-lens {
  position: absolute;
  border: 1px solid #d4d4d4;
  /*set the size of the lens:*/
  width: 40px;
  height: 40px;
}

.img-zoom-result {
  border: 1px solid #d4d4d4;
  /*set the size of the result div:*/
  width: 300px;
  height: 300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
</script>

<h1>Image Zoom</h1>
<p>Mouse over the image:</p>
<div class="img-zoom-container">
  <div class="preview"><img class="image" src="https://www.w3schools.com/howto/img_girl.jpg" width="200" height="200"></div>
  <div class="preview"><img class="image" src="https://placeimg.com/640/480/animals" width="200" height="200"></div>
  <div class="preview"><img class="image" src="https://placeimg.com/640/480/arch" width="200" height="200"></div>

</div>

<div class="img-zoom-result" id="result"></div>

关于javascript - 修改图像缩放逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52074410/

相关文章:

javascript - react : How to render same component with same onClick and different drops

java - 如何在java中对图像进行阈值处理(用于黑白分色)?

python - 如何自动加载图像文件夹?

javascript - 在 Javascript 中使用 Math random 生成大数

javascript - 选择单选按钮值时显示警报

javascript - 淡入和淡出下拉菜单无序列表

javascript - 在 jquery 中创建新的垂直 json 到水平

image - 如何在 Matlab 中找到二值图像中的所有连通分量?

javascript - AJAX 成功重定向但给出 [对象 : object]

javascript - jQuery + 解析 : Functions from for loops