Reputation: 1101
I'm working on a wordpress site which display articles using AJAX. I added some jquery code in the success callback. One of my needs is to display an image on mouse over. All my code works except the mouse over effect.
So this is my ajax function :
function loadArticle(pageNumber, loop, term){
$.ajax({
url: ajax_var.url,
type:'POST',
data: someData,
success: function(html){
// This will be the div where our content will be loaded
$("#content").append(html);
// Zoom box
$('img.static').hide();
$(".ad-preview").live({
mouseover: function() {
$(this).find('img.static').stop().fadeIn();
},
mouseout: function() {
$(this).find('img.static').stop().fadeOut();
}
});
// Other stuff...
});
return false;
}
});
and the HTML :
<div class="ad-preview">
<a target="_blank" href="">
<img src="img/zoom.png" /></a>
</a>
</div>
What is the good way to implement this effect ?
Upvotes: 0
Views: 1043
Reputation: 532575
First, you should probably be using on rather than live
. Second, delegated handlers don't have to be applied in the callback. Because they are delegated to a higher level element that remains on the page you can apply them on page load.
$("body").on('mouseover', '.ad-preview', function() {
$(this).find('img.static').stop().fadeIn();
})
.on('mouseout', '.ad-preview', function() {
$(this).find('img.static').stop().fadeOut();
});
Upvotes: 2