jquery.hoverdir.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. + function($) {
  2. "use strict";
  3. var a = function(element, options) {
  4. this.$el = $(element)
  5. this.options = $.extend(!0, {}, this.defaults, options)
  6. this.isVisible = !1
  7. this.$hoverElem = this.$el.find(this.options.hoverElem)
  8. this.transitionProp = "all " + this.options.speed + "ms " + this.options.easing
  9. this.support = this._supportsTransitions()
  10. this._loadEvents()
  11. }
  12. a.prototype = {
  13. defaults: {
  14. speed: 300,
  15. easing: "ease",
  16. hoverDelay: 0,
  17. inverse: !1,
  18. hoverElem: "div"
  19. },
  20. constructor: a,
  21. _supportsTransitions: function() {
  22. if ("undefined" != typeof Modernizr) return Modernizr.csstransitions;
  23. var h = document.body || document.documentElement,
  24. s = h.style,
  25. p = "transition";
  26. if ("string" == typeof s[p]) return !0;
  27. var a = ["Moz", "webkit", "Webkit", "Khtml", "O", "ms"];
  28. p = p.charAt(0).toUpperCase() + p.substr(1);
  29. for (var i = 0; i < a.length; i++)
  30. if ("string" == typeof s[a[i] + p]) return !0;
  31. return !1
  32. },
  33. _loadEvents: function() {
  34. this.$el.on("mouseenter.hoverdir mouseleave.hoverdir", $.proxy(function(h) {
  35. this.direction = this._getDir({
  36. x: h.pageX,
  37. y: h.pageY
  38. }), "mouseenter" === h.type ? this._showHover() : this._hideHover()
  39. }, this))
  40. },
  41. _showHover: function() {
  42. var a = this._getStyle(this.direction);
  43. this.support && this.$hoverElem.css("transition", ""), this.$hoverElem.hide().css(a.from), clearTimeout(this.tmhover), this.tmhover = setTimeout($.proxy(function() {
  44. this.$hoverElem.show(0, $.proxy(function() {
  45. this.support && this.$hoverElem.css("transition", this.transitionProp), this._applyAnimation(a.to)
  46. }, this))
  47. }, this), this.options.hoverDelay), this.isVisible = !0
  48. },
  49. _hideHover: function() {
  50. var h = this._getStyle(this.direction);
  51. this.support && this.$hoverElem.css("transition", this.transitionProp), clearTimeout(this.tmhover), this._applyAnimation(h.from), this.isVisible = !1
  52. },
  53. _getDir: function(h) {
  54. var a = this.$el.width(),
  55. c = this.$el.height(),
  56. x = (h.x - this.$el.offset().left - a / 2) * (a > c ? c / a : 1),
  57. v = (h.y - this.$el.offset().top - c / 2) * (c > a ? a / c : 1),
  58. y = Math.round((Math.atan2(v, x) * (180 / Math.PI) + 180) / 90 + 3) % 4;
  59. return y
  60. },
  61. _getStyle: function(h) {
  62. var a, c, v = {
  63. left: "0",
  64. top: "-100%"
  65. },
  66. y = {
  67. left: "0",
  68. top: "100%"
  69. },
  70. _ = {
  71. left: "-100%",
  72. top: "0"
  73. },
  74. b = {
  75. left: "100%",
  76. top: "0"
  77. },
  78. $ = {
  79. top: "0"
  80. },
  81. g = {
  82. left: "0"
  83. };
  84. switch (h) {
  85. case 0:
  86. case "top":
  87. a = this.options.inverse ? y : v, c = $;
  88. break;
  89. case 1:
  90. case "right":
  91. a = this.options.inverse ? _ : b, c = g;
  92. break;
  93. case 2:
  94. case "bottom":
  95. a = this.options.inverse ? v : y, c = $;
  96. break;
  97. case 3:
  98. case "left":
  99. a = this.options.inverse ? b : _, c = g
  100. }
  101. return {
  102. from: a,
  103. to: c
  104. }
  105. },
  106. _applyAnimation: function(a) {
  107. $.fn.applyStyle = this.support ? $.fn.css : $.fn.animate, this.$hoverElem.stop().applyStyle(a, $.extend(!0, [], {
  108. duration: this.options.speed
  109. }))
  110. },
  111. show: function(h) {
  112. this.$el.off("mouseenter.hoverdir mouseleave.hoverdir"), this.isVisible || (this.direction = h || "top", this._showHover())
  113. },
  114. hide: function(h) {
  115. this.rebuild(), this.isVisible && (this.direction = h || "bottom", this._hideHover())
  116. },
  117. setOptions: function(a) {
  118. this.options = h.extend(!0, {}, this.defaults, this.options, a)
  119. },
  120. destroy: function() {
  121. this.$el.off("mouseenter.hoverdir mouseleave.hoverdir"), this.$el.data("hoverdir", null)
  122. },
  123. rebuild: function(h) {
  124. "object" == typeof h && this.setOptions(h), this._loadEvents()
  125. }
  126. }, $.fn.hoverdir = function(c, v) {
  127. return this.each(function() {
  128. var y = $(this).data("hoverdir"),
  129. _ = "object" == typeof c && c;
  130. y || (y = new a(this, _), $(this).data("hoverdir", y)), "string" == typeof c && (y[c](v), "destroy" === c && $(this).data("hoverdir", !1))
  131. })
  132. }, $.fn.hoverdir.Constructor = a
  133. }(jQuery);