我正在尝试创建应用程序,该应用程序将使用jQuery在鼠标悬停时缩放图像。问题是图像无法正确缩放到正确的位置,我无法找出问题所在。
zoomIn = function (event) {
var pre = document.getElementById("preview");
pre.style.visibility = "visible";
if ($('#zoom1').is(':hover')) {
var img = document.getElementById("zoom1");
pre.style.backgroundImage = "url('https://www.gettyimages.ca/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg')";
}
var posX = event.offsetX;
var posY = event.offsetY;
pre.style.backgroundPosition=(-posX*2.5)+"px "+(-posY*5.5)+"px";
}
zoomOut = function () {
var pre = document.getElementById("preview");
pre.style.visibility = "hidden";
}
#preview{
margin-top: 10px;
border:1px solid black;
width:350px;
height:500px;
background-repeat: no-repeat;
margin-left: auto;
margin-right: auto;
}
#samples{
text-align: center;
}
#samples img {
margin: 10px;
display: block;
border: 2px solid #6A6462;
}
#samples img:hover {
cursor: zoom-in;
border: 0;
-moz-box-shadow:2px 2px 7px 2px rgba(130,130,130,1),-1px -1px 7px 2px rgba(130,130,130,1);
-webkit-box-shadow: 2px 2px 7px 2px rgba(130,130,130,.7),-1px -1px 7px 2px rgba(130,130,130,1);
box-shadow: 2px 2px 7px 2px rgba(130,130,130,.7),-2px -2px 7px 2px rgba(130,130,130,1);
}
@media screen and (max-width: 767px){
#samples img {
display: inline-block;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<div class="container-fluid">
<div class="row">
<div class="col-lg-6" id="samples">
<img class="img-fluid" id="zoom1" onmousemove="zoomIn(event)" onmouseout="zoomOut()" src="https://www.gettyimages.ca/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg">
</div>
<div class="col-lg-6">
<div id="preview" onmousemove="zoomIn(event)"></div>
</div>
</div>
</div>
为进行完整的演示,我已将代码上传到jsfiddle。
答案 0 :(得分:0)
您有很多混合代码,有时可能会导致语法错误或编码问题。我会考虑将其全部移至jQuery。
例如,请参见以下内容:
$(function() {
function zoomIn(event) {
var img = $(event.target);
var pre = $("#" + img.data("preview"));
var high;
if ($(".highlight").length === 1) {
high = $(".highlight");
} else {
high = $("<div>", {
class: "highlight",
width: Math.round(pre.width() / 5.5) + "px",
height: Math.round(pre.height() / 2.5) + "px",
}).css({
top: 0,
left: 0
}).appendTo(img.parent());
}
pre.css("visibility", "visible");
if (img.is(':hover')) {
pre.css("background-image", "url('" + img.attr("src") + "')");
}
var posX = event.offsetX;
var posY = event.offsetY;
high.css({
left: posX + "px",
top: posY + "px"
});
pre.css("background-position", (-posX * 2.5) + "px " + (-posY * 5.5) + "px");
}
function zoomOut(event) {
$(".preview").css({
"background-image": "",
visibility: "hidden"
});
$(".highlight").remove();
}
$("img").mouseover(zoomIn).mousemove(zoomIn).mouseout(zoomOut);
});
.preview {
margin-top: 10px;
border: 1px solid black;
width: 350px;
height: 500px;
background-repeat: no-repeat;
margin-left: auto;
margin-right: auto;
}
.highlight {
border: 1px dotted #ccc;
background-color: rgba(255, 255, 255, .25);
position: absolute;
}
.samples {
text-align: center;
position: relative;
}
.samples img {
margin: 10px;
display: block;
border: 2px solid #6A6462;
}
.samples img:hover {
cursor: zoom-in;
border: 0;
-moz-box-shadow: 2px 2px 7px 2px rgba(130, 130, 130, 1), -1px -1px 7px 2px rgba(130, 130, 130, 1);
-webkit-box-shadow: 2px 2px 7px 2px rgba(130, 130, 130, .7), -1px -1px 7px 2px rgba(130, 130, 130, 1);
box-shadow: 2px 2px 7px 2px rgba(130, 130, 130, .7), -2px -2px 7px 2px rgba(130, 130, 130, 1);
}
@media screen and (max-width: 767px) {
.samples img {
display: inline-block;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<div class="container-fluid">
<div class="row">
<div class="col-lg-6 samples" id="sample-1">
<img class="img-fluid" id="zoom-1" src="https://www.gettyimages.ca/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg" data-preview="preview-1">
</div>
<div class="col-lg-6">
<div id="preview-1" class="preview"></div>
</div>
</div>
</div>
我也试图考虑更多的可移植脚本代码。我怀疑您将拥有1个以上的示例图像,并且可能会有多个预览区域。您将看到我调整了许多HTML元素的ID和类,以使其更易于解决。
为了帮助将img绑定到特定的预览窗口,我使用了data属性(data-preview='preview-1'
)。同样,这有助于使代码可移植,并且对于许多图像和预览或许多图像和1个预览有用。 image-1
,preview-1
等命名对jQuery很有帮助。您不会大量使用它,但需要考虑一些事情。
预览工作正常,您现在可以根据需要调整视图值。希望这会有所帮助。