简体   繁体   English

JavaScript - 尝试使用鼠标滚轮调整图像大小

[英]JavaScript - Trying to resize image size with mouse wheel

I'm a beginner in JavaScript and I'm trying to resize the image size by scrolling the mouse wheel.我是 JavaScript 的初学者,我正在尝试通过滚动鼠标滚轮来调整图像大小。 I run this in Chrome.我在 Chrome 中运行它。

This code only resizes its height but not its width;此代码仅调整其高度而不是其宽度; I've tried working it out on paper by substituting numbers into img1.height and img1.width and it works but not when I run the code.我已经尝试通过将数字代入 img1.height 和 img1.width 来在纸上解决它,它可以工作,但在我运行代码时却不能。

HTML HTML

<img id="img1" src="https://upload.wikimedia.org/wikipedia/commons/7/73/HDRI_Sample_Scene_Balls_(Auto_Exposure).jpg" style="position:absolute; transition: 1s all">

JAVASCRIPT --- wrong code, not working, only height changes JAVASCRIPT ---错误的代码,不起作用,只有高度变化

function changeSize(e) {
    var img1 = document.getElementById("img1");
    var slope = img1.height / img1.width;

    img1.height = img1.height + e.deltaY;
    img1.width = img1.height / slope;
}

function init() {
    window.onwheel = changeSize;
}

window.onload = init;

With the same HTML code, the code below works but I don't understand why copying the new height to a variable and using that variable instead of the image height works.使用相同的 HTML 代码,下面的代码有效,但我不明白为什么将新高度复制到变量并使用该变量而不是图像高度有效。

JAVASCRIPT --- correct code, working, both height and width changes JAVASCRIPT ---正确的代码,工作,高度和宽度变化

function changeSize(e) {
    var img1 = document.getElementById("img1");
    var slope = img1.height / img1.width;
    var change = img1.height + e.deltaY;

    img1.height = change;
    img1.width = change / slope;
}

function init() {
  window.onwheel = changeSize;
}

window.onload = init;

Thanks for the answers.感谢您的回答。

img1.height = img1.height + e.deltaY;
img1.width = img1.height / slope;

In this code above there is an attempt to assign a value to image height and to read it immediately.在上面的这段代码中,尝试为图像高度分配一个值并立即读取它。 The problem is that image properties are set asynchronously.问题是图像属性是异步设置的。 In order to read the height value, that is previously assigned, you should wait for an onload event, like this:为了读取先前分配的高度值,您应该等待一个onload事件,如下所示:

img1.height = img1.height + e.deltaY;
img1.onload = function() {
    img1.width = img1.height / slope;
}

Specify only one dimension in the markup / css, and change that via the wheel event.在标记/css 中仅指定一个维度,并通过wheel事件更改它。 Image will resize keeping the aspect ratio intact.图像将调整大小,保持纵横比不变。

Try it in the snippet below:在下面的代码段中尝试一下:

 var image = document.getElementById('image'); image.addEventListener('wheel', function(e) { this.width += Math.floor(e.deltaY); e.preventDefault(); });
 img { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); transition: all 0.2s linear; }
 <img id="image" src="//placehold.it/240x120/33d/fff">

In this code Non working :在此代码中不工作

function changeSize(e) {
    var img1 = document.getElementById("img1");
    var slope = img1.height / img1.width;

    img1.height = img1.height + e.deltaY;
    img1.width = img1.height / slope;
}

function init() {
    window.onwheel = changeSize;
}

window.onload = init;

If we add alert like this:如果我们像这样添加alert

 function changeSize(e) { var img1 = document.getElementById("img1"); var slope = img1.height / img1.width; img1.height = img1.height + e.deltaY; alert(img1.height); img1.width = img1.height / slope; } function init() { window.onwheel = changeSize; } window.onload = init;
 <img id="img1" src="https://upload.wikimedia.org/wikipedia/commons/7/73/HDRI_Sample_Scene_Balls_(Auto_Exposure).jpg" style="position:absolute; transition: 1s all">

Now we see that the code starts to work as expected.But the point to notice is that the value of alert is the previous image height not the updated one even it is written after img1.height = img1.height + e.deltaY;现在我们看到代码开始按预期工作了。但要注意的是,即使是在img1.height = img1.height + e.deltaY;之后写入的,alert 的值也是前一个图像的高度,而不是更新后的img1.height = img1.height + e.deltaY; .Since the img1.height value does not update until the code ends this is happening.The alert box takes the focus away from the current window and when we close it again switches to current window thats why code works in case of alert .因为img1.height值在代码结束之前不会更新,这正在发生。警报框将焦点从当前窗口移开,当我们再次关闭它时切换到当前窗口,这就是代码在alert情况下起作用的原因

In this code Working :在此代码中工作

function changeSize(e) {
    var img1 = document.getElementById("img1");
    var slope = img1.height / img1.width;
    var change = img1.height + e.deltaY;

    img1.height = change;
    img1.width = change / slope;
}

function init() {
  window.onwheel = changeSize;
}

window.onload = init;

In this code img1.height as well as img1.width depends on a variable value change .Since the value of variable updates during the runtime of the function it changes the height and width of image.在这段代码中, img1.heightimg1.width取决于变量值的change 。由于变量的值在函数运行时更新,它改变了图像的高度和宽度。

Now the point to see is that:现在要注意的是:

 function changeSize(e) { var img1 = document.getElementById("img1"); var slope = img1.height / img1.width; var change = img1.height + e.deltaY; img1.height = change; alert(img1.height); img1.width = change / slope; } function init() { window.onwheel = changeSize; } window.onload = init;
 <img id="img1" src="https://upload.wikimedia.org/wikipedia/commons/7/73/HDRI_Sample_Scene_Balls_(Auto_Exposure).jpg" style="position:absolute; transition: 1s all">

If we alert in this code also it shows the previous height of the image.如果我们在这段代码中发出警报,它也会显示图像的先前高度。

Therefore I conclude that the height and width of the image changes after the end of the function and since in first code the width depends on updating the height so it does not changes因此我得出结论,图像的高度和宽度在函数结束后发生变化,并且由于在第一个代码中宽度取决于更新高度,因此它不会改变

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM