使用Javascript更改图像宽度/高度

使用Javascript更改图像宽度/高度,javascript,html,css,Javascript,Html,Css,这里是Javascript新手, 每当鼠标悬停时,我一直在尝试更改HTML页面中图像的大小(宽度/高度),但是如果在CSS页面中设置样式,则似乎不起作用,这对我来说是一个巨大的问题,因为我需要使用position属性来设置图像位置 这是密码 HTML: jS: 只要这样做: a:hover {transform:scale(1.5,1.5);} <here you can set the x and y scaling 您只能通过CSS来解决它。有一个-pseudo类,当您将鼠标悬停在

这里是Javascript新手, 每当鼠标悬停时,我一直在尝试更改HTML页面中图像的大小(宽度/高度),但是如果在CSS页面中设置样式,则似乎不起作用,这对我来说是一个巨大的问题,因为我需要使用position属性来设置图像位置

这是密码

HTML:

jS:

只要这样做:

a:hover {transform:scale(1.5,1.5);}  <here you can set the x and y scaling

您只能通过CSS来解决它。有一个-pseudo类,当您将鼠标悬停在特定元素上时,它就会被激活。对于您的请求,不需要使用JavaScript

#mw:hover {
    width: 183px;
    height: 121px;
}
上面的CSS片段所做的是:“如果将id为mw的元素悬停,则将宽度和高度分别更改为183px和121px”

下面是一个例子。点击“RunCodeSnippet”并尝试将rubic图像悬停

#兆瓦{
位置:绝对位置;
左:15%;
最高:35%;
宽度:146px;
高度:97px;
}
#mw:悬停{
宽度:183px;
高度:121px;
}

简单javascript版本,不需要样式

var-element=document.getElementsByName(“image1”)[0];
setAttribute('width',146);
元素。setAttribute('height',97);
函数大(){
元素。setAttribute('width',183);
setAttribute('height',121);
}
函数小(){
setAttribute('width',146);
元素。setAttribute('height',97);
}

您只能使用CSS来完成。顺便说一句,解决您的问题。
function big()
{
    document.getElementsByName("image1").style.width=183;
    document.getElementsByName("image1").style.height=121;
}
function small()
{
    document.getElementsByName("image1").style.width=146;
    document.getElementsByName("image1").style.height=97;
}
a:hover {transform:scale(1.5,1.5);}  <here you can set the x and y scaling
document.getElement etc.addEventListener("your event", function(event){
event.target.style.transform = "scale(1.5,1.5)";
});
#mw:hover {
    width: 183px;
    height: 121px;
}