ui.shadow.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. (function($) {
  2. //If the UI scope is not available, add it
  3. $.ui = $.ui || {};
  4. //Make nodes selectable by expression
  5. $.extend($.expr[':'], { shadowed: "(' '+a.className+' ').indexOf(' ui-shadowed ')" });
  6. $.fn.shadowEnable = function() { if($(this[0]).next().is(".ui-shadow")) $(this[0]).next().show(); }
  7. $.fn.shadowDisable = function() { if($(this[0]).next().is(".ui-shadow")) $(this[0]).next().hide(); }
  8. $.fn.shadow = function(options) {
  9. options = options || {};
  10. options.offset = options.offset ? options.offset : 0;
  11. options.opacity = options.opacity ? options.opacity : 0.2;
  12. return this.each(function() {
  13. var cur = $(this);
  14. //Create a shadow element
  15. var shadow = $("<div class='ui-shadow'></div>"); cur.after(shadow);
  16. //Figure the base height and width
  17. var baseWidth = cur.outerWidth();
  18. var baseHeight = cur.outerHeight();
  19. //get the offset
  20. var position = cur.position();
  21. //Append smooth corners
  22. $('<div class="ui-shadow-color ui-shadow-layer-1"></div>').css({ opacity: options.opacity-0.05, left: 5+options.offset, top: 5+options.offset, width: baseWidth+1, height: baseHeight+1 }).appendTo(shadow);
  23. $('<div class="ui-shadow-color ui-shadow-layer-2"></div>').css({ opacity: options.opacity-0.1, left: 7+options.offset, top: 7+options.offset, width: baseWidth, height: baseHeight-3 }).appendTo(shadow);
  24. $('<div class="ui-shadow-color ui-shadow-layer-3"></div>').css({ opacity: options.opacity-0.1, left: 7+options.offset, top: 7+options.offset, width: baseWidth-3, height: baseHeight }).appendTo(shadow);
  25. $('<div class="ui-shadow-color ui-shadow-layer-4"></div>').css({ opacity: options.opacity, left: 6+options.offset, top: 6+options.offset, width: baseWidth-1, height: baseHeight-1 }).appendTo(shadow);
  26. //If we have a color, use it
  27. if(options.color)
  28. $("div.ui-shadow-color", shadow).css("background-color", options.color);
  29. //Determine the stack order (attention: the zIndex will get one higher!)
  30. if(!cur.css("zIndex") || cur.css("zIndex") == "auto") {
  31. var stack = 0;
  32. cur.css("position", (cur.css("position") == "static" ? "relative" : cur.css("position"))).css("z-index", "1");
  33. } else {
  34. var stack = parseInt(cur.css("zIndex"));
  35. cur.css("zIndex", stack+1);
  36. }
  37. //Copy the original z-index and position to the clone
  38. //alert(shadow); If you insert this alert, opera will time correctly!!
  39. shadow.css({
  40. position: "absolute",
  41. zIndex: stack,
  42. left: position.left,
  43. top: position.top,
  44. width: baseWidth,
  45. height: baseHeight,
  46. marginLeft: cur.css("marginLeft"),
  47. marginRight: cur.css("marginRight"),
  48. marginBottom: cur.css("marginBottom"),
  49. marginTop: cur.css("marginTop")
  50. });
  51. function rearrangeShadow(el,sh) {
  52. var $el = $(el);
  53. $(sh).css($el.position());
  54. $(sh).children().css({ height: $el.outerHeight()+"px", width: $el.outerWidth()+"px" });
  55. }
  56. if($.browser.msie) {
  57. //Add dynamic css expressions
  58. shadow[0].style.setExpression("left","parseInt(jQuery(this.previousSibling).css('left'))+'px' || jQuery(this.previousSibling).position().left");
  59. shadow[0].style.setExpression("top","parseInt(jQuery(this.previousSibling).css('top'))+'px' || jQuery(this.previousSibling).position().top");
  60. } else {
  61. //Bind events for good browsers
  62. this.addEventListener("DOMAttrModified",function() { rearrangeShadow(this,shadow); },false);
  63. }
  64. });
  65. };
  66. })($);