An alternative to reach all img
tags similar to the modern API adventListener , it would use the event captureEvents a>. See:
var figura = document.getElementsByTagName("img");
for (var i in figura) {
var fotos = figura[i];
if (document.layers);
document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
fotos.onmouseover = aumenta;
fotos.onmouseout = diminui;
}
function aumenta() {
this.height = this.height * 2;
this.width = this.width * 2;
}
function diminui() {
this.height = this.height / 2;
this.width = this.width / 2;
}
<img src="https://sites.google.com/site/mplayerplugin/louvor/1.jpg"width="120px" height="90px"/>
<img src="https://sites.google.com/site/mplayerplugin/louvor/2.jpg"width="120px" height="90px"/>
<img src="https://sites.google.com/site/mplayerplugin/louvor/3.jpg"width="120px" height="90px"/>
w3schools - View this example by running on an external platform
Already for this other example the syntax used is cleaner and widely compatible with other browsers. Check out:
var figura = document.getElementsByTagName("img");
for ( var i in figura ) {
var fotos = figura[i];
fotos.onmouseover = function() {
this.width = this.width * 2;
this.height = this.height * 2;
}
fotos.onmouseout = function() {
this.width = this.width / 2;
this.height = this.height / 2;
}
}
<img src="https://sites.google.com/site/mplayerplugin/louvor/1.jpg"width="120px" height="90px"/>
<img src="https://sites.google.com/site/mplayerplugin/louvor/2.jpg"width="120px" height="90px"/>
<img src="https://sites.google.com/site/mplayerplugin/louvor/3.jpg"width="120px" height="90px"/>
w3schools - This example is also working online
For the two examples given above I dispense comments by making their understanding of the syntax used in them simpler.
Another way is to insert JavaScript events in the image element at runtime.
We can achieve this like this:
var figura = document.getElementsByTagName("img");
for (var i in figura) {
var foto = figura[i];
var zoom = {
onMouseOver : "aumenta(this)",
onMouseOut : "diminui(this)"
}
for (var visualizar in zoom) {
foto.setAttribute(visualizar, zoom[visualizar]);
}
}
function aumenta(obj) {
obj.height = obj.height * 2;
obj.width = obj.width * 2;
}
function diminui(obj) {
obj.height = obj.height / 2;
obj.width = obj.width / 2;
}
<img src="https://sites.google.com/site/mplayerplugin/louvor/1.jpg"width="120px" height="90px"/>
<img src="https://sites.google.com/site/mplayerplugin/louvor/2.jpg"width="120px" height="90px"/>
<img src="https://sites.google.com/site/mplayerplugin/louvor/3.jpg"width="120px" height="90px"/>
w3schools - An external demonstration of this example
This is an example of how to include - onMouseOver
and onMouseOut
in real time.
Explanation
The main part is setAttribute()
basically toggle the attribute of the img
element:
for (var visualizar in zoom) {
foto.setAttribute(visualizar, zoom[visualizar]);
}
For optimum optimization we create an object:
var zoom = {
onMouseOver : "aumenta(this)",
onMouseOut : "diminui(this)"
}
To save these rules - onMouseOver="aumenta(this)"
and onMouseOut="diminui(this)"
.
And soon it will be applied to the img
tags when the elements are loaded.
About several medium here presented by me, the simpler would be to use CSS1 becoming "Cross-Browser" I believe. We do a lot of writing little. Note:
img:hover {
width: 240px;
height: 180px;
}
<img src="https://sites.google.com/site/mplayerplugin/louvor/1.jpg"width="120px" height="90px"/>
<img src="https://sites.google.com/site/mplayerplugin/louvor/2.jpg"width="120px" height="90px"/>
<img src="https://sites.google.com/site/mplayerplugin/louvor/3.jpg"width="120px" height="90px"/>
w3schools - I've added a sample of this example here!
Note - In IE , a <! doctype>
must be declared so that the hover
selector works on elements other than the <a>
element.