我想出了如何在一次点击后改变图像的大小,但我试图让图像在每次点击后缩小几个像素,我找遍了这个网站和其他网站,但我没有发现任何关于如何在每次点击后改变大小的内容。我正在寻找一种严格的方式在这种格式,我正在寻找的代码,能够在HTML文件内工作。
感谢大家的意见,你的所有建议对我来说都很好。再次感谢所有评论和发送答案的人。
下面的代码就是我到目前为止所拥有的
<!doctype html>
<html>
<head>
<script>
"use script";
function resizeImage(img)
{
img.style.width = "150px";
img.style.height = "150px";
}
</script>
</head>
<body>
<figure id="image-box">
<img src="img/green.jpg" alt= "Whoops" style="width:200px;height:200px;" class="image-slide" onclick="resizeImage(this)">
</figure>
</body>
</html>发布于 2018-07-26 22:44:19
通过组合一些基本的JS思想,您可以轻松地做到这一点:
var scale = .9;
document.querySelector( 'img' ).addEventListener( 'click', function(){
// Get the width and height calculated in the viewport
var { width, height } = event.target.getBoundingClientRect();
// Apply by multiplying it by style,
// which will result in an update to the above
// boundingClientRect next time
event.target.style.width = width * scale + 'px';
event.target.style.height = height * scale + 'px';
});<img src="http://placehold.it/100x100" />
https://stackoverflow.com/questions/51541124
复制相似问题