jquery.dropshadow.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. VERSION: Drop Shadow jQuery Plugin 1.6 12-13-2007
  3. REQUIRES: jquery.js (1.2.6 or later)
  4. SYNTAX: $(selector).dropShadow(options); // Creates new drop shadows
  5. $(selector).redrawShadow(); // Redraws shadows on elements
  6. $(selector).removeShadow(); // Removes shadows from elements
  7. $(selector).shadowId(); // Returns an existing shadow's ID
  8. OPTIONS:
  9. left : integer (default = 4)
  10. top : integer (default = 4)
  11. blur : integer (default = 2)
  12. opacity : decimal (default = 0.5)
  13. color : string (default = "black")
  14. swap : boolean (default = false)
  15. The left and top parameters specify the distance and direction, in pixels, to
  16. offset the shadow. Zero values position the shadow directly behind the element.
  17. Positive values shift the shadow to the right and down, while negative values
  18. shift the shadow to the left and up.
  19. The blur parameter specifies the spread, or dispersion, of the shadow. Zero
  20. produces a sharp shadow, one or two produces a normal shadow, and three or four
  21. produces a softer shadow. Higher values increase the processing load.
  22. The opacity parameter should be a decimal value, usually less than one. You can
  23. use a value higher than one in special situations, e.g. with extreme blurring.
  24. Color is specified in the usual manner, with a color name or hex value. The
  25. color parameter does not apply with transparent images.
  26. The swap parameter reverses the stacking order of the original and the shadow.
  27. This can be used for special effects, like an embossed or engraved look.
  28. EXPLANATION:
  29. This jQuery plug-in adds soft drop shadows behind page elements. It is only
  30. intended for adding a few drop shadows to mostly stationary objects, like a
  31. page heading, a photo, or content containers.
  32. The shadows it creates are not bound to the original elements, so they won't
  33. move or change size automatically if the original elements change. A window
  34. resize event listener is assigned, which should re-align the shadows in many
  35. cases, but if the elements otherwise move or resize you will have to handle
  36. those events manually. Shadows can be redrawn with the redrawShadow() method
  37. or removed with the removeShadow() method. The redrawShadow() method uses the
  38. same options used to create the original shadow. If you want to change the
  39. options, you should remove the shadow first and then create a new shadow.
  40. The dropShadow method returns a jQuery collection of the new shadow(s). If
  41. further manipulation is required, you can store it in a variable like this:
  42. var myShadow = $("#myElement").dropShadow();
  43. You can also read the ID of the shadow from the original element at a later
  44. time. To get a shadow's ID, either read the shadowId attribute of the
  45. original element or call the shadowId() method. For example:
  46. var myShadowId = $("#myElement").attr("shadowId"); or
  47. var myShadowId = $("#myElement").shadowId();
  48. If the original element does not already have an ID assigned, a random ID will
  49. be generated for the shadow. However, if the original does have an ID, the
  50. shadow's ID will be the original ID and "_dropShadow". For example, if the
  51. element's ID is "myElement", the shadow's ID would be "myElement_dropShadow".
  52. If you have a long piece of text and the user resizes the window so that the
  53. text wraps or unwraps, the shape of the text changes and the words are no
  54. longer in the same positions. In that case, you can either preset the height
  55. and width, so that it becomes a fixed box, or you can shadow each word
  56. separately, like this:
  57. <h1><span>Your</span> <span>Page</span> <span>Title</span></h1>
  58. $("h1 span").dropShadow();
  59. The dropShadow method attempts to determine whether the selected elements have
  60. transparent backgrounds. If you want to shadow the content inside an element,
  61. like text or a transparent image, it must not have a background-color or
  62. background-image style. If the element has a solid background it will create a
  63. rectangular shadow around the outside box.
  64. The shadow elements are positioned absolutely one layer below the original
  65. element, which is positioned relatively (unless it's already absolute).
  66. *** All shadows have the "dropShadow" class, for selecting with CSS or jQuery.
  67. ISSUES:
  68. 1) Limited styling of shadowed elements by ID. Because IDs must be unique,
  69. and the shadows have their own ID, styles applied by ID won't transfer
  70. to the shadows. Instead, style elements by class or use inline styles.
  71. 2) Sometimes shadows don't align properly. Elements may need to be wrapped
  72. in container elements, margins or floats changed, etc. or you may just
  73. have to tweak the left and top offsets to get them to align. For example,
  74. with draggable objects, you have to wrap them inside two divs. Make the
  75. outer div draggable and set the inner div's position to relative. Then
  76. you can create a shadow on the element inside the inner div.
  77. 3) If the user changes font sizes it will throw the shadows off. Browsers
  78. do not expose an event for font size changes. The only known way to
  79. detect a user font size change is to embed an invisible text element and
  80. then continuously poll for changes in size.
  81. 4) Safari support is shaky, and may require even more tweaks/wrappers, etc.
  82. The bottom line is that this is a gimick effect, not PFM, and if you push it
  83. too hard or expect it to work in every possible situation on every browser,
  84. you will be disappointed. Use it sparingly, and don't use it for anything
  85. critical. Otherwise, have fun with it!
  86. AUTHOR: Larry Stevens (McLars@eyebulb.com) This work is in the public domain,
  87. and it is not supported in any way. Use it at your own risk.
  88. */
  89. (function($){
  90. var dropShadowZindex = 1; //z-index counter
  91. $.fn.dropShadow = function(options)
  92. {
  93. // Default options
  94. var opt = $.extend({
  95. left: 4,
  96. top: 4,
  97. blur: 2,
  98. opacity: .5,
  99. color: "black",
  100. swap: false
  101. }, options);
  102. var jShadows = $([]); //empty jQuery collection
  103. // Loop through original elements
  104. this.not(".dropShadow").each(function()
  105. {
  106. var jthis = $(this);
  107. var shadows = [];
  108. var blur = (opt.blur <= 0) ? 0 : opt.blur;
  109. var opacity = (blur == 0) ? opt.opacity : opt.opacity / (blur * 8);
  110. var zOriginal = (opt.swap) ? dropShadowZindex : dropShadowZindex + 1;
  111. var zShadow = (opt.swap) ? dropShadowZindex + 1 : dropShadowZindex;
  112. // Create ID for shadow
  113. var shadowId;
  114. if (this.id) {
  115. shadowId = this.id + "_dropShadow";
  116. }
  117. else {
  118. shadowId = "ds" + (1 + Math.floor(9999 * Math.random()));
  119. }
  120. // Modify original element
  121. $.data(this, "shadowId", shadowId); //store id in expando
  122. $.data(this, "shadowOptions", options); //store options in expando
  123. jthis
  124. .attr("shadowId", shadowId)
  125. .css("zIndex", zOriginal);
  126. if (jthis.css("position") != "absolute") {
  127. jthis.css({
  128. position: "relative",
  129. zoom: 1 //for IE layout
  130. });
  131. }
  132. // Create first shadow layer
  133. bgColor = jthis.css("backgroundColor");
  134. if (bgColor == "rgba(0, 0, 0, 0)") bgColor = "transparent"; //Safari
  135. if (bgColor != "transparent" || jthis.css("backgroundImage") != "none"
  136. || this.nodeName == "SELECT"
  137. || this.nodeName == "INPUT"
  138. || this.nodeName == "TEXTAREA") {
  139. shadows[0] = $("<div></div>")
  140. .css("background", opt.color);
  141. }
  142. else {
  143. shadows[0] = jthis
  144. .clone()
  145. .removeAttr("id")
  146. .removeAttr("name")
  147. .removeAttr("shadowId")
  148. .css("color", opt.color);
  149. }
  150. shadows[0]
  151. .addClass("dropShadow")
  152. .css({
  153. height: jthis.outerHeight(),
  154. left: blur,
  155. opacity: opacity,
  156. position: "absolute",
  157. top: blur,
  158. width: jthis.outerWidth(),
  159. zIndex: zShadow
  160. });
  161. // Create other shadow layers
  162. var layers = (8 * blur) + 1;
  163. for (i = 1; i < layers; i++) {
  164. shadows[i] = shadows[0].clone();
  165. }
  166. // Position layers
  167. var i = 1;
  168. var j = blur;
  169. while (j > 0) {
  170. shadows[i].css({left: j * 2, top: 0}); //top
  171. shadows[i + 1].css({left: j * 4, top: j * 2}); //right
  172. shadows[i + 2].css({left: j * 2, top: j * 4}); //bottom
  173. shadows[i + 3].css({left: 0, top: j * 2}); //left
  174. shadows[i + 4].css({left: j * 3, top: j}); //top-right
  175. shadows[i + 5].css({left: j * 3, top: j * 3}); //bottom-right
  176. shadows[i + 6].css({left: j, top: j * 3}); //bottom-left
  177. shadows[i + 7].css({left: j, top: j}); //top-left
  178. i += 8;
  179. j--;
  180. }
  181. // Create container
  182. var divShadow = $("<div></div>")
  183. .attr("id", shadowId)
  184. .addClass("dropShadow")
  185. .css({
  186. left: jthis.position().left + opt.left - blur,
  187. marginTop: jthis.css("marginTop"),
  188. marginRight: jthis.css("marginRight"),
  189. marginBottom: jthis.css("marginBottom"),
  190. marginLeft: jthis.css("marginLeft"),
  191. position: "absolute",
  192. top: jthis.position().top + opt.top - blur,
  193. zIndex: zShadow
  194. });
  195. // Add layers to container
  196. for (i = 0; i < layers; i++) {
  197. divShadow.append(shadows[i]);
  198. }
  199. // Add container to DOM
  200. jthis.after(divShadow);
  201. // Add shadow to return set
  202. jShadows = jShadows.add(divShadow);
  203. // Re-align shadow on window resize
  204. $(window).resize(function()
  205. {
  206. try {
  207. divShadow.css({
  208. left: jthis.position().left + opt.left - blur,
  209. top: jthis.position().top + opt.top - blur
  210. });
  211. }
  212. catch(e){}
  213. });
  214. // Increment z-index counter
  215. dropShadowZindex += 2;
  216. }); //end each
  217. return this.pushStack(jShadows);
  218. };
  219. $.fn.redrawShadow = function()
  220. {
  221. // Remove existing shadows
  222. this.removeShadow();
  223. // Draw new shadows
  224. return this.each(function()
  225. {
  226. var shadowOptions = $.data(this, "shadowOptions");
  227. $(this).dropShadow(shadowOptions);
  228. });
  229. };
  230. $.fn.removeShadow = function()
  231. {
  232. return this.each(function()
  233. {
  234. var shadowId = $(this).shadowId();
  235. $("div#" + shadowId).remove();
  236. });
  237. };
  238. $.fn.shadowId = function()
  239. {
  240. return $.data(this[0], "shadowId");
  241. };
  242. $(function()
  243. {
  244. // Suppress printing of shadows
  245. var noPrint = "<style type='text/css' media='print'>";
  246. noPrint += ".dropShadow{visibility:hidden;}</style>";
  247. $("head").append(noPrint);
  248. });
  249. })(jQuery);