Advertisement
Guest User

Untitled

a guest
May 27th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. // generic function for displaying a menu element when hovering on an other element
  2. function popinOnHover(hoverElement, menuElementOrFunction){
  3. var $menuElement = typeof menuElementOrFunction !== 'function' ? $(menuElementOrFunction) : null;
  4. var hoveredZones = 0;
  5. function hideIfNotHovered(){
  6. if (!hoveredZones){
  7. if (typeof menuElementOrFunction === 'function') {
  8. $menuElement = menuElementOrFunction(false);
  9. }
  10. else
  11. $menuElement.hide();
  12. }
  13. }
  14. function leave(){
  15. hoveredZones--;
  16. setTimeout(hideIfNotHovered, 100);
  17. }
  18. function hover(){
  19. hoveredZones++;
  20. }
  21. function showPopin(){
  22. if (typeof menuElementOrFunction === 'function')
  23. $menuElement = $(menuElementOrFunction(this));
  24. $menuElement
  25. .css($(this).position())
  26. .mouseenter(hover)
  27. .mouseleave(leave)
  28. .show();
  29. }
  30. $(hoverElement)
  31. .mouseenter(hover)
  32. .mouseenter(showPopin)
  33. .mouseleave(leave);
  34. }
  35.  
  36. // sample use:
  37.  
  38. function togglePopin(hoveredElement){
  39. var $popin = $('.notif-popin');
  40. if (!hoveredElement) {
  41. $popin.remove();
  42. return null;
  43. }
  44. if (!$popin.length) {
  45. $popin = $('<div class="notif-popin"></div>').appendTo($body);
  46. }
  47. populatePopin($popin, hoveredElement);
  48. return $popin;
  49. }
  50.  
  51. popinOnHover($('#hoverElement1, #hoverElement2'), togglePopin);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement