Posted By

noah on 04/07/07


Tagged

css javascript template html DOM example standards layout slidingdoors alistapart nonsemantic


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

jonhenshaw
pagetoscreen


Reusable Box with Images and CSS (implements a non-semantic variant of Sliding Doors)


 / Published in: HTML
 

URL: http://onemorebug.com/meme.washer/code_examples/reusableBox/index.html

Reusable balloon, built with JavaScript and css. Expands horizontally (set in the CSS) and vertically (automatically) up the size of its component images. This solution is not semantic, but it is flexible.

The goal was to implement a variant of Sliding Doors that could be boiled down to two snippets of code (one before the content container, and one after) and some CSS.

Expects 3 images, examples of which can be found here: http://onemorebug.com/meme.washer/code_examples/reusableBox/images

Also includes an example JavaScript implementation.

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <!--** Time-stamp: <[box.html] modified by Noah Sussman on Saturday, 2007.04.07 at 12 : 41 : 08 on GRIMOIRE> **-->
  3. <title>Reusable Box (sliding doors)</title>
  4. <style type="text/css">
  5. /****************************************************************
  6. ** Reusable Box
  7. ** This is an image-based box design, which allows arbitrary
  8. ** expansion up to the width and height of its component images.
  9. ** See the "Sliding Doors of CSS" series of articles at A List Apart
  10. ** for in-depth explanation.
  11. ** Note that I have not actually implemented "Slding Doors," as the
  12. ** markup here is anything but semantic.
  13. ****************************************************************/
  14. body { background: url(images/mars.png) }
  15. .reusableBox {
  16. background: url(images/left.png) no-repeat;
  17. margin-left: -10px; /* Using a negative margin to avoid overlap of transparent images. I learned to do this by reading css-discuss. NS 2007.04.06 */
  18. position: absolute;
  19. }
  20. .reusableBox .presentationTop {
  21. background: url(images/top.png) no-repeat top right;
  22. margin-left: 10px;
  23. /* padding-top: 10px; */
  24. }
  25. .reusableBox .rbContent {
  26. padding-top: 20px;
  27. padding-left: 10px; /* another 10px is added by the margin of presentationTop */
  28. padding-right: 20px;
  29. position: relative;
  30. }
  31. .reusableBox .presentationBottom {
  32. height: 10px;
  33. background: url(images/bottom.png) no-repeat bottom right;
  34. position: relative;
  35. top: 10px;
  36. }
  37. .reusableBox .presentationLeftBottom {
  38. height: 10px;
  39. width: 10px;
  40. margin-left: -10px;
  41. margin-bottom: -10px;
  42. background: url(images/bottom.png) no-repeat top left;
  43. position: relative;
  44. }
  45. .reusableBox .rbNipple {
  46. background: url(images/nipple.png) no-repeat;
  47. width: 15px;
  48. height: 11px;
  49. position: absolute;
  50. top: 4px;
  51. left: 22px;
  52. }
  53. .rbSource { display: none; } /* this line is optional */
  54. /* styles below here apply to the example implementations */
  55. div#foo {
  56. width: 320px; /* set the width of each box by its id */
  57. top: 40px;
  58. left: 500px;
  59. position: relative;
  60. }
  61. div#downBalloon {
  62. width: 200px;
  63. top: 110px;
  64. left: 740px;
  65. position: relative;
  66. }
  67. div#downBalloon .rbNipple {
  68. left: 134px;
  69. }
  70. p { display: inline; } /* optional */
  71. code { display: block; padding: .3em; } /* optional */
  72. <script type="text/javascript">
  73. function makeWeb2Box(elId, width, top, left, hasNipple, nippleOffset) {
  74. var el = document.getElementById(elId);
  75. var elContainer = document.createElement('div');
  76. elContainer.className="reusableBox";
  77. elContainer.style.width = width;
  78. elContainer.style.top = top;
  79. elContainer.style.left = left;
  80.  
  81. var offset = (nippleOffset ? 'style="left: '+ nippleOffset +'px"' : '');
  82. var nipple = (hasNipple ? '<div class="rbNipple"' + offset + '></div>' : '');
  83.  
  84. elContainer.innerHTML = '<div class="presentationTop"><div class="rbContent">' +
  85. el.innerHTML +
  86. '</div><div class="presentationBottom"><div class="presentationLeftBottom">' +
  87. nipple +
  88. '</div></div></div>';
  89. el.parentNode.insertBefore(elContainer, el);
  90. el.parentNode.removeChild(el);
  91. }
  92.  
  93. window.onload = function() {
  94. makeWeb2Box('bar', 400, 10, 40, true, 102);
  95. makeWeb2Box('jsBox', 200, 300, 400);
  96. }
  97. </head>
  98.  
  99. <!-- snippet #1 -->
  100. <div class="reusableBox" id="foo">
  101. <div class="presentationTop">
  102. <div class="rbContent">
  103. <!-- snippet #1 ends -->
  104.  
  105. <p>Reusable box built with css </p>
  106.  
  107. <!-- snippet #2 -->
  108. </div><!-- end of rbContent -->
  109. <div class="presentationBottom">
  110. <div class="presentationLeftBottom">
  111. </div><!-- end of presentationLeftBottom -->
  112. </div><!-- end of presentationBottom -->
  113. </div><!-- end of presentationTop -->
  114. </div><!-- end of reusableBox -->
  115. <!-- snippet #2 ends -->
  116.  
  117.  
  118. <div class="reusableBox" id="downBalloon">
  119. <div class="presentationTop">
  120. <div class="rbContent">
  121. <h3>mars</h3>
  122. <p>this is a popup built with css</p>
  123. </div><!-- end of rbContent -->
  124. <div class="presentationBottom">
  125. <div class="presentationLeftBottom">
  126. </div><!-- end of presentationLeftBottom -->
  127. <div class="rbNipple"></div>
  128. </div><!-- end of presentationBottom -->
  129. </div><!-- end of presentationTop -->
  130. </div><!-- end of reusableBox -->
  131.  
  132.  
  133. <div id="bar" class="rbSource">
  134. <p>Reusable balloon, built with JavaScript.
  135. makeWeb2Box(someId (string), width (integer), top (integer), left (integer), [hasPointer (boolean), pointerPosition (integer)])
  136. </code>
  137. Turns the hidden div named 'someId' into a popup,
  138. with or without a pointer (the default is no pointer).
  139. If a pointer is specified, optionally set the left position
  140. of the pointer image (the default is set in the CSS).
  141. </p>
  142. </div>
  143.  
  144. <div id="jsBox" class="rbSource">
  145. <p>This is a box (not a balloon, because the pointer is omitted). This box has been built with JavaScript.</p>
  146. </div>
  147.  
  148. </body>
  149. </html>

Report this snippet  

You need to login to post a comment.