admin管理员组文章数量:1430621
New to Javascript here, I've been trying to change the size of an image (width/height) in an HTML page whenever a mouseover occurs, however it doesn't seem to work if the styles are set in the CSS page, which is a huge problem for me since I need to use the position property to set the image location.
Here's the code.
HTML:
<a href="#" onMouseOver="big()" onMouseOut="small()">
<img src="img1.png" name="image1" id="mw">
</a>
CSS:
#mw
{
position:absolute;
left:15%;
top:35%;
width:146px;
height:97px;
}
jS:
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;
}
New to Javascript here, I've been trying to change the size of an image (width/height) in an HTML page whenever a mouseover occurs, however it doesn't seem to work if the styles are set in the CSS page, which is a huge problem for me since I need to use the position property to set the image location.
Here's the code.
HTML:
<a href="#" onMouseOver="big()" onMouseOut="small()">
<img src="img1.png" name="image1" id="mw">
</a>
CSS:
#mw
{
position:absolute;
left:15%;
top:35%;
width:146px;
height:97px;
}
jS:
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;
}
Share
Improve this question
asked Jan 17, 2016 at 13:39
user5626322user5626322
2
- You can do it with CSS only btw. – KarelG Commented Jan 17, 2016 at 13:42
- Fix your problem. jsbin./qicugikeza/edit?html,css,js,console,output – Vuong Commented Jan 17, 2016 at 13:47
3 Answers
Reset to default 2Simple javascript version, style not required
var element = document.getElementsByName("image1")[0];
element.setAttribute('width', 146);
element.setAttribute('height', 97);
function big() {
element.setAttribute('width', 183);
element.setAttribute('height', 121);
}
function small() {
element.setAttribute('width', 146);
element.setAttribute('height', 97);
}
<a href="#" onMouseOver="big()" onMouseOut="small()">
<img src="http://zoarchurch.co.uk/content/pages/uploaded_images/91.png" name="image1" id="mw">
</a>
You can solve it by CSS only. There is a :hover
-pseudo class which gets activated once you hover a specific element. For your request, there is no need to use JavaScript.
#mw:hover {
width: 183px;
height: 121px;
}
What the above CSS snippet does is: "change width and height to respectively 183px and 121px if you hover an element with id mw".
Here below is an example of it. click on "Run code snippet" and try to hover the rubic image.
#mw {
position: absolute;
left: 15%;
top: 35%;
width: 146px;
height: 97px;
}
#mw:hover {
width: 183px;
height: 121px;
}
<a href="#">
<img src="http://zoarchurch.co.uk/content/pages/uploaded_images/91.png" name="image1" id="mw">
</a>
Just do this:
a:hover {transform:scale(1.5,1.5);} <here you can set the x and y scaling
with JS you can:
document.getElement etc.addEventListener("your event", function(event){
event.target.style.transform = "scale(1.5,1.5)";
});
本文标签: htmlChange image widthheight with JavascriptStack Overflow
版权声明:本文标题:html - Change image widthheight with Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745429639a2658268.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论