Two things:
First, putting the jQuery code in the Jsfiddle Javascript block is a crime. If you look at the top left corner of the Fiddle, it has an option there to include jQuery automatically ;)
Second: you can search for elements that are descended from another element. Once you have declared the event for all class elements content:
$('.content').mouseenter(function() {
$(this).animate({
width: '+=750px',
opacity: '+=0.65'
});
$('.contentParagraph').fadeTo('fast',1);
});
You can replace the instruction in the last row by:
$(this).find('.contentParagraph').fadeTo('fast',1);
The method find finds the descendants of the element in which it is called, who meet the research condition you pass. But this is not efficient. You will make a search every time you fire the event. In your case are few elements and may even be fast, but if someday your HTML swell, it may harm performance.
I can think of a few alternatives you can try:
Create a dictionary of class element ids content for the class elements contentParagraph. Something like:
var contentDictionary: {
content1: $("#paragraph1"),
content2: $("#paragraph2"),
content3: $("#paragraph3")
/**etc., etc./
}
Then your event would look something like:
$('.content').mouseenter(function() {
var that = $(this);
that.animate({
width: '+=750px',
opacity: '+=0.65'
});
contentDictionary[that.id].fadeTo('fast',1);
});
The second alternative is to give an extra class to the content within the event mouseenter, and take this extra class at the event mouseleave. Hence you apply a different visibility to contentParagraph according to the parent element class.
And there’s one that requires even less effort. Do the contentParagraph be the same size as content. Then use the event mouseenter and mouseleave in the contentParagraph, and not in the content ;) from the contentParagraph, you can get the content unique to it using the function parent jQuery. So:
var content = $(this).parent();
This is much more efficient than the find. Good luck there!
opa, thanks, I’ll try all these alternatives and I’ll see which one fits my needs. Now, I have another problem which I would like to solve in this code. Every time I mouse the div content it stores the mouseenter calls and keeps going back and forth several times, there would be some way to fix it?
– user8278