gpt4 book ai didi

javascript - 如何在移动设备上使用纯 JavaScript 放大点击的图像

转载 作者:太空狗 更新时间:2023-10-29 16:41:56 26 4
gpt4 key购买 nike

我正在尝试创建点击时放大的缩略图。目标是让选定的缩略图自行放大到设备的最大宽度。如果单击另一个缩略图,其图像将替换当前所选缩略图的图像。一次只能放大一个缩略图。

图像应跨越设备的最大宽度。此外,我正在尝试使用纯 JavaScript(无 jQuery)、CSS 和 HTML 来完成此任务。

JavaScript

function showImage(imgName) {
document.getElementById('largeImg').src = imgName;
showLargeImagePanel();
unselectAll();
}
function showLargeImagePanel() {
document.getElementById('largeImgPanel').style.visibility = 'visible';
}
function unselectAll() {
if(document.selection) document.selection.empty();
if(window.getSelection) window.getSelection().removeAllRanges();
}
function hideMe(obj) {
obj.style.visibility = 'hidden';
}

HTML

<img  src="./Images/image.jpg" alt="1.jpeg" style="cursor:pointer" 
onclick="showImage('./Images/image.jpg');">
<div id="largeImgPanel" onclick="hideMe(this);">
<img id="largeImg" style="height: 100%; margin: 0; padding: 0;">

CSS

#largeImgPanel {
text-align: center;
visibility: hidden;
position: fixed;
z-index: 100;
top: 0; left: 0;
width:100%;
height:100%;
background-color: rgba(100,100,100, 0.5);
}

图像确实弹出,但它太大(比设备宽),因此不适合移动屏幕。如何使其尺寸合适?

最佳答案

您需要将图像宽度限制为父容器的宽度。

#largeImg  {
height: auto;
max-width: 100%;
}

这将强制图像不大于其所在的容器,并在较小的屏幕上自动缩小。

关于javascript - 如何在移动设备上使用纯 JavaScript 放大点击的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23641576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com