Reputation: 131
The version can be shown here. I want the existing text to fade out and the new text to fade in. For some reason this fades in twice weirdly.
$(window).load(function(){
var originalTitle = $('.Pinctitle').text();
$('body').on('mouseenter', '.Pselectorbutton', function(){
var text = $(this).data('title');
$('.Pinctitle').text(text);
});
$('body').on('mouseleave', '.Pselectorbutton', function(){
$('.Pinctitle').text(originalTitle);
});
});
Upvotes: 1
Views: 58
Reputation: 146191
You may try this:
$('body').on('mouseenter', '.Pselectorbutton', function(){
var text = $(this).data('title');
$('.Pinctitle').stop(1,1).animate({opacity: 0}, 'slow', function() {
$(this).text(text);
}).animate({opacity: 1}, 'slow');
});
Upvotes: 2
Reputation: 386
The animate function has a call back on complete. Try that:
var originalTitle = $('.Pinctitle').text();
$('body').on('mouseenter', '.Pselectorbutton', function(){
var text = $(this).data('title');
var pinc = $('.Pinctitle');
pinc.text(originalTitle).animate({opacity: 0}, 'slow', pinc.text(text).animate({opacity: 1}, 'slow'));
});
$('body').on('mouseleave', '.Pselectorbutton', function(){
$('.Pinctitle').text(originalTitle);
});
Upvotes: 1
Reputation: 941
The problem in your solution is about asynchronity. You kick off the animation. This call returns immediately and continues with setting the new text. You could use a callback method to overcome this issue like so:
$('body').on('mouseenter', '.Pselectorbutton', function(){
var text = $(this).data('title');
$('.Pinctitle').text(originalTitle).animate({opacity: 0}, 'slow', function() {
$('.Pinctitle').text(text).animate({opacity: 1}, 'slow');
});
});
Upvotes: 1