Interactive pop-up bubbles with jQuery & CSS
08 Set up the events
08 Set up the events
We need two events for each of our badges: one for mouseover and a second for mouseout. When the user mouses over the badge, we’re going to set some CSS properties to move the bubble down a little before animating it back into position, and to make sure it’s hidden by setting opacity to 0, and display to Block. Add the code below inside your second <script> tags:
$(document).ready(function(){
$(“.moreinfo”).each(function(){
var t;
var isanimated = false;
var isvisible = false;
var popup = $(“.floatingpanel”,this);
$(this).mouseover(function(){
/* Mouseover code in here */
}).mouseout(function(){
/* Mouseout code in here */
});
});
});
09 Animate the appearance
We’re going to use the animate function that’s built into jQuery to animate the opacity of the bubble from 0 (invisible) to 1 (visible). At the same time we’ll move the bubble back up into position so that it appears to puff up into existence. Before we can do that, we need to set the CSS of the bubble to start in the right place. Add the code below inside the mouseover area of the script then test in your browser to view the effect:
popup.css({opacity:0,display:”block”,marginTop:15}).animate({marginTop:0,opacity:1},500);
10 Animate the disappearance
We want our bubble to animate out of view in a similar way to how it appears, so we’re more or less doing the same animation, but with the opacity moving from 1 to 0 instead of the other way around. Insert the code below inside the mouseout code area:
popup.animate({marginTop:-10,opacity:0},500);
11 Test in your browser and adjust
The basic effect is now complete. Test in your browser and adjust the settings to suit your preference. The line of jQuery code popup.css({opacity:0,display:”block”,marginTop:15}) in the mouseover section sets the starting position and opacity of the bubble. Note that we’re using marginTop to animate the position. The line.animate({marginTop:0,opacity:1},500); sets the end position and opacity, and the time (500 milliseconds).
12 Add a delay
The bubble disappears too quickly so wrap the mouse-off function inside a setTimeout function. This will allow us to wait for the specified time (we’ve opted for 500 milliseconds here) before running the animation to hide the bubble. By adding a clearTimeout function call to the mouseover, we get the added benefit of allowing the user to mouse off the badge and move back over the badge within half a second without the bubble disappearing. Update your code to look like that below:
var t;
$(this).mouseover(function(){
if (t) clearTimeout(t);
popup.css({opacity:0,display:”block”,marginTop:15}).animate({marginTop:0,opaci
ty:1},500);
}).mouseout(function(){
if (t) clearTimeout(t);
t = setTimeout(function(){isanimated=true;popup.animate({marginTop:-
10,opacity:0},500}, 500);
});
13 Test for existing animation
There’s a problem if you mouse off and back on to the badge while the animation is running. To solve this we need to add a check to see if the animation is in progress and if it is, ignore the mouseover/mouseoff. Update your code to look like the below noting the new if () statements that check for animation, and the new call back function at the end of the animation that sets the isanimated property to false: (new code set in bold)
$(document).ready(function(){
$(“.moreinfo”).each(function(){
var t;
var isanimated = false;
var isvisible = false;
var popup = $(“.floatingpanel”,this);
$(this).mouseover(function(){
if (t) clearTimeout(t);
if (!isvisible && !isanimated){
isanimated = true;
popup.css({opacity:0,display:”block”,marginTop:15}).animate({marginTop:0,opacit
y:1},500, function(){isanimated=false;isvisible=true;});
} else {
return;
}
}).mouseout(function(){
if (t) clearTimeout(t);
if (!isanimated && isvisible){
t = setTimeout(function(){isanimated=true;popup.animate({marginTop:-
10,opacity:0},500, function(){popup.css({display:”none”});isvisible=false;isanimate
d=false;});}, 500);
} else {
return;
}
});
});
});
14 Tidy up and add more hotspots
Get your code all tidy and properly indented. Make sure you’ve removed any console.log() or alert() statements you added to test the events firing. Work your way around the document adding additional hotspots where required. Here we’ve got 18 individual hotspots, so you’ll see the benefit of splitting the position out from the rest of the visual characteristics of the pop-ups. Once you’ve got all your content in place, test thoroughly cross-browser to make sure everything works as it should.
END
(This article originally appeared in Web Designer issue 160, authored by Sam Hampton-Smith)
















Thanks for this excellent jquery tool, i will definitely start using it in my web designs.
Nice tutorial/effect. However, I would make one suggestion . . .
A demo would be nice, with some sort of call-to-action button linking to it . . .
I can only speak for myself but, I know that 90% of the time, if there is no demo, I do not bother reading the tutorial.
Its unfortunate that the shading doesn’t seem to work quite right in Internet Explorer (but what is new?) but otherwise the tutorial was a nice straightforward use of jQuery. Many thanks for this great tutorial. Just a notice, if anyone has any problems, redo your quotation marks ;)
If you download the files and run the index.html it should work just as well as a demo, just a few more steps to see it.
Agreed with Batfan. I like to see what tutorial will accomplished.
+1 demo
@Batfan and @Kathleen:
Did you guys even went to the Coda website link that is PROVIDED in the tutorial. If you had you 2 would have seen the DEMO you 2 wanted so badly. I don’t know about the 2 of you, but for me that example was more than enough to see what this tutorial will do.
I can only speak for myself but, I know that 90% of the time, I read the entire thing before writing a reply.
And by the way if you guys need more explanation as to where you will see a “demo” at the Coda website. Just hover over the Download button. You will see your demo.
@author of the tutorial:
Thanks for the tutorial. This is nice.
Great article on jquery. I was looking for such tool for my web design http://www.bestwebsitesdesigner.com. I will try to implement it tonight and if have any problems will put my comment here :)
Might be great but where’s the demo!?!
I agree with @Batfan, should be nice to see a demo page. Without that, just we can imagine.
Looks cool. Any demo yet?
There is a problem, when I mouse over it quickly the floating panel gets stuck and doesn’t animate back to invisible. All it takes is brushing the mouse over it and it’ll get stuck. How can this be fixed?
What @Batfan said … no demo?
Nice of you to include the GIF at the top, but … well …
demo?!
foundmostwillagreewith your blog.