views:

279

answers:

2

I've built a webpage that's supposed to increase the size of images onmouseover. I'm not replacing the images with bigger ones but rather "stretch" the existing ones because of system limitations.

Here's the webpage: http://www.moviez.4pu.com/IMDBQueries.aspx

You can see that the movie images get bigger when you're on them.

Problem is when I move my mouse too quick that sometimes an image gets stuck open or it causes inifinite flickering.

attached is also the code I'm using for the resize:

function resizeImage(elem,width,height){
    var myEffect = new Fx.Morph(elem, {duration: 350});
    myEffect.start({'height': height,'width': width});
}
+2  A: 

First thing, set this variable outside your functions

var imagegrow

And then mouseover this

function () {
    imagegrow = setTimeout(function(){ resizeImage(elem,width,height); },1000);
}

And the mouseout this:

function () {
    clearTimeout(imagegrow);
}

Adjust the 1000 number to suit your preferred delay (it's in milliseconds). I'd write the whole code for you, but I haven't used MooTools for a while.

Comment if you have any questions

Gausie
Thanks for the answer. Please bear in mind I have dozens of images in the webpage. Will it not be a problem?
Faruz
Got a syntax error. Wrote <img ... onmouseover="function(){imagegrow...}" /> Got a syntax error on the first brackets (for the "function" keyword)
Faruz
Ah ok I was assuming you'd be setting the events on document load. In you're adding it as the onmouseover attribute, miss off the `function(){` at the beginning and the `}` at the end and it'll work. Also, there should be no problem with the amount of images you have. Make sure you declare the imagegrow variable outside of everything.In any case, I would suggest using addEvent rather than specifying the events inline, it makes for shorter, neater and more maintainable code in the future.
Gausie
+1  A: 

Faruz, Gaussie is right you need to use a timeout. However, consider using mootools' addEvent function as described in the mootools docs as well as the $$ function which will allow you to achieve something much more elegant, along the lines of:

window.addEvent('domready', function() {
  $$("tr td input").addEvent("mouseover", function() {
    //anonymous function like Gaussie's here
  });
});

Note that this isn't the exact code, it will take some modification but it is cleaner and should be more efficient then setting the onmouseover property of every image. Also, remember this goes in the head of your HTML document.

andykram