An alternative to reach all tags img
similar to the modern API adventListener, would use the event captureEvents. Behold:
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 - See this example 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 do not need comments because it becomes simpler your understanding regarding the syntax used in them.
Another way is to insert Javascript events into the image element at runtime.
We can get it 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 the setAttribute()
basically toggle the element attribute img
:
for (var visualizar in zoom) {
foto.setAttribute(visualizar, zoom[visualizar]);
}
For a perfect optimized operation we create an object:
var zoom = {
onMouseOver : "aumenta(this)",
onMouseOut : "diminui(this)"
}
To keep these rules - onMouseOver="aumenta(this)"
and onMouseOut="diminui(this)"
.
And soon it will be applied on the tags img
when loading the elements is carried out.
About several means here presented by me, the simplest would be to use CSS1 becoming "Cross-Browser" I believe. We do a lot of writing little. Observe:
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 added a sample of this example here!
Note - In the IE, must be declared a <! doctype>
so that the selector hover
function on items other than the element <a>
.
Do all your images in your HTML document fall below any other element? In any specific location? Or the images are scattered among various elements?
– Gabriel Katakura
@Gabrielkatakura They were defined inside an element whose ha a
id
especially for them.– Diego Henrique
Can you put this HTML snippet in the example? I think you can do
Event Delegation
is the best solution, since the elements are all grouped into one element.– Gabriel Katakura
@Gabrielkatakura Well, now I’m complicated! Because I put here what I think is necessary. Add an example so that it becomes practical for understanding the real problem you were facing. The whole creation gets all dynamic, tag
img
and other elements, I think that if I were to post this would all come out of the scope of the question and would probably win some downVote. rsrs– Diego Henrique
Okay, so I can’t formulate an answer with the idea of
Event Delegation
, but here is a link for you to see if this is your case. https://davidwalsh.name/event-delegate– Gabriel Katakura