jquery.ishadow.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Usage: $(selector).iShadow({position: "fixed", referPoint: "topleft"}
  3. * position: can be "fixed", or "absolute"
  4. * referPoint: can be "topleft", "topright", "bottomleft", or "bottomright"
  5. */
  6. ;(function($) {
  7. if ($.fn.removeShadow) {
  8. return;
  9. }
  10. var ieBug = $.browser.msie && parseFloat($.browser.version) < 7;
  11. $.fn.removeShadow = function() {
  12. return this.each(function() {
  13. var shadowId = $(this).data("iShadow");
  14. if (shadowId) {
  15. $("#" + shadowId).remove();
  16. }
  17. });
  18. };
  19. $.fn.iShadow = function(settings) {
  20. if ((ieBug && !document.getElementsByTagName("select").length && !document.getElementsByTagName("object").length) ||
  21. (!ieBug && !document.getElementsByTagName("embed").length && !document.getElementsByTagName("object").length)) {
  22. return this;
  23. }
  24. var defaults = {
  25. position: "fixed",
  26. referPoint: "topleft"
  27. };
  28. // merge current settings with defaults
  29. var opts = $.extend(defaults, settings);
  30. return this.each(function() {
  31. var $this = $(this), shadowId = $this.data("iShadow"), $iframe, position, iWidth, iHeight;
  32. // Create a shadow iframe in the first time. Next time, just get it directly.
  33. if (shadowId) {
  34. $iframe = $("#" + shadowId);
  35. }
  36. else {
  37. shadowId = "ishadow-" + new Date().getTime();
  38. $this.data("iShadow", shadowId);
  39. $iframe = $('<iframe id="' + shadowId + '" frameborder="0" tabindex="-1" src="about:blank" style="position:' + opts.position +
  40. ';z-index:' + parseFloat($this.css("zIndex")).toString() + ';display:block;cursor:default;opacity:0;filter:alpha(opacity=0);"></iframe>')
  41. .insertBefore($this);
  42. if (opts.position == "fixed") {
  43. switch (opts.referPoint) {
  44. case "topleft":
  45. $iframe.css({"top": $this.css("top"), "left": $this.css("left")});
  46. break;
  47. case "topright":
  48. $iframe.css({"top": $this.css("top"), "right": $this.css("right")});
  49. break;
  50. case "bottomleft":
  51. $iframe.css({"bottom": $this.css("bottom"), "left": $this.css("left")});
  52. break;
  53. case "bottomright":
  54. $iframe.css({"bottom": $this.css("bottom"), "right": $this.css("right")});
  55. break;
  56. default:
  57. alert("iShadow: incorrect reference point!");
  58. return;
  59. }
  60. }
  61. }
  62. // adjust the shadow iframe's position
  63. if (opts.position == "absolute") {
  64. position = $this.position();
  65. $iframe.css({"left": position.left + "px", "top": position.top + "px"});
  66. }
  67. // caculate width and height for the shadow iframe element
  68. iWidth = $this.width() +
  69. (parseInt($this.css("padding-left")) || 0) +
  70. (parseInt($this.css("padding-right")) || 0) +
  71. (parseInt($this.css("border-left-width")) || 0) +
  72. (parseInt($this.css("border-right-width")) || 0);
  73. iHeight = $this.height() +
  74. (parseInt($this.css("padding-top")) || 0) +
  75. (parseInt($this.css("padding-bottom")) || 0) +
  76. (parseInt($this.css("border-top-width")) || 0) +
  77. (parseInt($this.css("border-bottom-width")) || 0);
  78. $iframe.css({"width": iWidth + "px", "height": iHeight + "px",
  79. "margin-top": $this.css("margin-top"), "margin-right": $this.css("margin-right"),
  80. "margin-bottom": $this.css("margin-bottom"), "margin-left": $this.css("margin-left")});
  81. });
  82. };})(jQuery);