4

I am currently trying to make it so that if i hover over the li, the image changes its size. I managed to pull out a code from a website that changes the image size if i hover over the img which looks like the following:

<div class="profiles">
    <ul>
        <li class="portraitsLeft" id="one" align="left">
            <a href="../project/profile1.html">
                <img src="../project/images/portraits/img1.jpg" width="100" onmouseover="this.width=150;" onmouseout="this.width=100" align="middle" alt="Img1"/>
            </a> 
            Character 1
        </li>
    </ul>
</div>

What i don't understand is what this is pointing at. I assumed it points at img but it doesn't seem to work. How should I change the code so that i can change the image size when i hover over the li or a instead of this? Thanks in advance.

user3748353
  • 65
  • 1
  • 1
  • 5
  • "this" is referring to the element currently being referred to by the event....in this case the onmouseover event is referring to "this" image that is being moused over – The One and Only ChemistryBlob Jul 27 '14 at 17:39
  • if you prefer a zoom effect -- http://jsfiddle.net/y4yAP/ -- http://stackoverflow.com/questions/20078250/simple-jquery-hover-enlarge – Tasos Jul 27 '14 at 17:43
  • Are you sure the above isn't working? Take a look at this [JSFiddle](http://jsfiddle.net/jVeju/2/). It validates what you did and works. – G-- Jul 27 '14 at 18:00

6 Answers6

7

There's no need for CSS(although it is cleaner) in this case. You can do this using a name attribute.

<img src="https://upload.wikimedia.org/wikipedia/commons/6/6f/OrteliusWorldMap.jpeg" width="150" height="100" name="OrteliusWorldMap"
onmouseover="OrteliusWorldMap.width='300';OrteliusWorldMap.height='200';"
onmouseout="OrteliusWorldMap.width='150';OrteliusWorldMap.height='100';" />

JSFiddle Link.

EDIT:

You can now change it anywhere you like thanks to the name attribute.

<div class="profiles">
    <ul>
        <li class="portraitsLeft" id="one" align="left">
            <a href="google.com" onmouseover="OrteliusWorldMap.width=150;" onmouseout="OrteliusWorldMap.width=100">
                <img src="https://upload.wikimedia.org/wikipedia/commons/6/6f/OrteliusWorldMap.jpeg" width="100" name="OrteliusWorldMap">
            </a> 
            Character 1
        </li>
    </ul>
</div>

JSFiddle Link.

The issue with trying to use this elsewhere is that this will try to reference the current HTML element. See the following for more information on this and how the onMouseOver event works.

G--
  • 924
  • 7
  • 8
4

Why not use CSS?

<img id="image" src="../project/images/portraits/img1.jpg" align="middle" alt="Img1"/>

#image{
    width:50px;
    height:50px;
}

#image:hover{
    width:100px;
    height:100px;
}

JSFiddle Demo

Or you can change your code to this:

<img id="image" src="https://www.google.com/images/srpr/logo11w.png" onMouseOver="this.style.width='200px'" onMouseOut="this.style.width='100px'" alt="Img1"/>

Use this.style.width='200px'

JSFiddle Demo

imbondbaby
  • 6,351
  • 3
  • 21
  • 54
2

Alternatively to CSS, you could go the jQuery route:

$("img").mouseover(function() {
    $("img").css({ width: '150px', height: '150px' });
});

This would also allow you to add some animations on hover if you wanted that.

This would require that you include the jQuery package in your HTML:

<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
2

You may try like this:

<img src="https://upload.wikimedia.org/wikipedia/commons/6/6f/OrteliusWorldMap.jpeg" alt="test" />

img {
    width: 100px;
}
img:hover {
    width: 300px;
}

Working DEMO

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • first of all, thank you for your help. however, i think that this answer isnt really what i needed help on (sorry if the question wasnt specific enough). from your code, i think that it only changes the image size when i hover over the img if im not mistaken. I wanted to know how to change it so that a specific image thats inside the li changes its size if i hover over any part of the or
  • . i edited the question so that its more specific. thank you
  • – user3748353 Jul 27 '14 at 17:52