{"id":3726,"date":"2007-06-15T16:20:50","date_gmt":"2007-06-15T21:20:50","guid":{"rendered":"http:\/\/www.drunkenfist.com\/304\/2007\/06\/15\/mad-science-flash-for-a-page-background-using-javascript-and-css\/"},"modified":"2019-12-31T11:22:48","modified_gmt":"2019-12-31T16:22:48","slug":"mad-science-flash-for-a-page-background-using-javascript-and-css","status":"publish","type":"post","link":"https:\/\/htmlcssjavascript.com\/javascript\/mad-science-flash-for-a-page-background-using-javascript-and-css\/","title":{"rendered":"Mad Science&#8230; Flash for a page background using JavaScript and CSS"},"content":{"rendered":"<p>For the current  <a href=\"http:\/\/www.eforallexpo.com\/\">EforAll Expo<\/a>*1 web site, the designer suggested that we use a flash piece for the  background. It&#8217;s a kind of hip event, for gamers and other technophiles so we  made some assumptions about our user base and decided it was worth a go. The  onus fell on me, as these things do, to set up this effect.<\/p>\n<p>I&#8217;m not sure whether or not I&#8217;m proud or completely ashamed of this one. On the positive side, it was interesting to script and it looks  kind of cool. On the negative it adds <strong>nothing <\/strong> to the site other than some pizzazz and  therefore feels like a goofy script from back in the dHTML days.<\/p>\n<p>I&#8217;m sure you  all have your own opinions on the matter \ud83d\ude42<br \/>\n<!--more--><br \/>\nWhatever side of the fence you fall on, I figured it might  be interesting to explain how I got this nasty beast out the door.<\/p>\n<p>My original attempt was to do it with <strong>just CSS.<\/strong> I envisioned some perfect voodoo combination of <code>&quot;overflow:hidden;&quot;<\/code>,  <code>wmode=transparent<\/code> and the like combining to create some magic. While <a href=\"http:\/\/www.drunkenfist.com\/web\/samples\/idg\/\">I had some  success<\/a>, the amount of time I had to get it to work coupled with the inevitable  cross browser problems, made me  reconsider that approach. After a glance at the schedule and some thought I  decided that if I went with a JavaScript based solution, I could be certain of implementation  success and I would be able to do it in a lot less time than I had already  spent on the CSS only solution.<\/p>\n<p>I planned to create two DIVs, populate them with the two  wings of the flash background and then set the widths of the DIVs dynamically  so as to suppress any potential scroll bars. This would create the appearance  of flash in the background without having to have the content sit on top of a  SWF.<\/p>\n<p>To start off I sketched out a skeleton of the script:<\/p>\n<p><img src='https:\/\/javaplusplusplus.com\/wp-content\/uploads\/2007\/06\/sketching-out-the-script.jpg' alt='sketching-out-the-script.jpg' \/><\/p>\n<p>I then ported those steps over to Homesite as comments forming the shell of my  script. I like doing that sort of preparatory work for any script that would be a new challenge for me, to make sure I have all the logic worked out before I type a single line. It doesn&#8217;t always work, fo course, but it saves a lot of headaches later to have a good roadmap in place even for small scripts such as these.<\/p>\n<p>In the end, I created two functions which run onload;<br \/>\n<code><br \/>\n     insert_flash() ;<br \/>\n     set_dimensions();<\/code><br \/>\n<code><br \/>\nset_dimensions()<\/code> also runs onresize.<\/p>\n<p><code>insert_flash()<\/code> looks like this. Comments explain the code:<\/p>\n<div style=\"width:90%;height:auto;overflow:scroll; border:1px solid #666;padding-left:3px;\">\n<code><\/p>\n<pre>\r\nfunction insert_flash() {<br \/>\r\n  <em>\/\/create a left div<\/em><br \/>\r\n           leftdiv=document.createElement(&quot;DIV&quot;)<br \/>\r\n  <em>\/\/give it an ID to hook JS and some styles defined in the  CSS<\/em><br \/>\r\n           leftdiv.setAttribute(&quot;id&quot;,&quot;left-bg&quot;);<br \/>\r\n <em> \/\/create a right div<\/em><br \/>\r\n           rightdiv=document.createElement(&quot;DIV&quot;)<br \/>\r\n <em> \/\/give it an ID to hook JS and some styles defined in the  CSS<\/em><br \/>\r\n           rightdiv.setAttribute(&quot;id&quot;,&quot;right-bg&quot;);<br \/>\r\n <em> \/\/dump them into the doc at the bottom of the DOM<\/em><br \/>\r\n           document.getElementsByTagName(&quot;body&quot;)[0].appendChild(leftdiv)<br \/>\r\n           document.getElementsByTagName(&quot;body&quot;)[0].appendChild(rightdiv)                   <br \/>\r\n  <em>\/\/I use this totally ugly method to get the flash into the  documents. <\/em><br \/>\r\n  <em>\/\/I know there are better methods. This one is so  straightforward I don't care about elegance.<\/em><br \/>\r\n           document.getElementById(&quot;left-bg&quot;).innerHTML='         &lt;div&gt;&lt;object  classid=&quot;clsid:D27CDB6E-AE6D-11cf-96B8-444553540000&quot;  codebase=&quot;http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=7,0,19,0&quot;  width=&quot;460&quot; height=&quot;578&quot;&gt;&lt;param name=&quot;movie&quot;  value=&quot;\/themes\/idg_assets\/flash\/bgglow_left.swf&quot; \/&gt;&lt;param name=&quot;quality&quot;  value=&quot;high&quot; \/&gt;; &lt;param name=&quot;wmode&quot;  value=&quot;transparent&quot;&gt;&lt;embed src=&quot;\/themes\/idg_assets\/flash\/bgglow_left.swf&quot;  quality=&quot;high&quot  ;  wmode=&quot;transparent&quot; pluginspage=&quot;http:\/\/www.macromedia.com\/go\/getflashplayer&quot;  type=&quot;application\/x-shockwave-flash&quot; width=&quot;460&quot;  height=&quot;578&quot;&gt;;&lt;\/embed&gt;; &lt;\/object&gt;;&lt;\/div&gt;;';<br \/>\r\n           document.getElementById  (&quot;right-bg&quot;).innerHTML='     &lt;div&gt;&lt;object  classid=&quot;clsid:D27CDB6E-AE6D-11cf-96B8-444553540000&quot;  codebase=&quot;http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=7,0,19,0&quot;  width=&quot;460&quot; height=&quot;578&quot;&gt;&lt;param name=&quot;movie&quot;  value=&quot;\/themes\/idg_assets\/flash\/bgglow_right.swf&quot; \/&gt;&lt;param name=&quot;wmode&quot;  value=&quot;transparent&quot;&gt;;  &lt;param name=&quot;quality&quot; value=&quot;high&quot; \/&gt;;           &lt;embed  src=&quot;\/themes\/idg_assets\/flash\/bgglow_right.swf&quot;  quality=&quot;high&quot;  pluginspage=&quot;http:\/\/www.macromedia.com\/go\/getflashplayer&quot  wmode=&quot;transparent&quot;  type=&quot;application\/x-shockwave-flash&quot; width=&quot;460&quot;  height=&quot;578&quot;&gt;;&lt;\/embed&gt;; &lt;\/object&gt;;&lt;\/div&gt;;';<br \/>\r\n }\r\n<\/pre>\n<p><\/code>\n<\/div>\n<p>set_dimensions looks like this. Comments again explain the  code:<\/p>\n<div style=\"width:90%;height:auto;overflow:scroll; border:1px solid #666;padding-left:3px;\">\n<code><\/p>\n<pre>\r\nfunction set_dimensions(){<br \/>\r\n  <em>\/\/cross browser doc width. From ppk? I don't remember where I got this.<\/em><br \/>\r\n                 docWidth  = (typeof( window.innerWidth ) == 'number') ? docWidth = window.innerWidth :  docWidth = document.documentElement.clientWidth;<br \/>\r\n <em> \/\/cross browser doc height.<\/em><br \/>\r\n                 docHeight = (typeof(  window.innerHeight ) == 'number') ? docHeight = window.innerHeight : docHeight  = document.documentElement.clientHeight;<br \/>\r\n  <em>\/\/if you were going to repurpose this, you would want to<br \/>\r\n  \/\/change the following 1000 to something like<br \/>\r\n  \/\/document.getElementById(&quot;wrapper&quot;).style.width<br \/>\r\n  \/\/or<br \/>\r\n  \/\/document.getElementById(&quot;wrapper&quot;).offsetWidth;<br \/>\r\n  \/\/I just knew that the wrapper div had a width of 1000 and  just worked <br \/>\r\n  \/\/with that knowledge.<br \/>\r\n  \/\/anyway, I'm just making sure we don't pass any nonsensical values  (in this case, a negative width) to the next part of the script<br \/><\/em>\r\n                if (docWidth&gt;;1000){<br \/>\r\n  <em>\/\/again. We just want to make sure we pass a good value so  we parse an integer out of the diff<br \/>\r\n  \/\/.5px doesn't really make much sense.<br \/><\/em>\r\n                     diff=parseInt((docWidth-1000)\/2)<br \/>\r\n                }<br \/>\r\n                else {<br \/>\r\n                      diff=0;<br \/>\r\n                }<br \/>\r\n<em>  \/\/then we just set some styles<\/em><br \/>\r\n                document.getElementById(&quot;left-bg&quot;).style.width=diff+&quot;px&quot;;<br \/>\r\n                document.getElementById(&quot;right-bg&quot;).style.width=diff+&quot;px&quot;;<br \/>\r\n                document.getElementById(&quot;left-bg&quot;).style.height=docHeight+&quot;px&quot;;<br \/>\r\n                document.getElementById(&quot;right-bg&quot;).style.height=docHeight+&quot;px&quot;;<br \/>\r\n  }<\/pre>\n<p><\/code>\n<\/div>\n<p>The CSS looks like this:<br \/>\n<code><br \/>\n#left-bg,<br \/>\n  #right-bg {<br \/>\n  width:100px;<br \/>\n  overflow:hidden;<br \/>\n  position:absolute;<br \/>\n  top:0;<br \/>\n  z-index:0;<br \/>\n  }<br \/>\n  \/*pin the left to the left edge*\/<br \/>\n  #left-bg {<br \/>\n  left:0;<br \/>\n  }<br \/>\n  \/*pin the right to the right edge*\/<br \/>\n  #right-bg {<br \/>\n  right:0;<br \/>\n  }<br \/>\n  #left-bg div,<br \/>\n  #right-bg div {<br \/>\n  position:absolute;<br \/>\n  top:0;<br \/>\n  z-index:0;<br \/>\n  }<br \/>\n  \/*pin the div containing the SWF to the right edge of the  left div.<br \/>\n  This ensures the div expands the same way a centered  background image would<br \/>\n  *\/<br \/>\n  #left-bg div{<br \/>\n  right:0;<br \/>\n  }<br \/>\n\/*pin the div containing the SWF to the left edge of the right  div.<br \/>\n  This ensures the div expands the same way a centered  background image would<br \/>\n  *\/<br \/>\n  #right-bg div{<br \/>\n  left:0;<br \/>\n  }<br \/>\n<\/code><\/p>\n<p>  And that&#8217;s that. It went really smoothly and it looks pretty  cool across browsers (Safari, Firefox 2, IE6 an IE7). My biggest gripe about the way it looks is the way that Firefox waits  until the resize event is completely over to redraw the screen. Because of  that, things can look less than optimal in firefox with a dramatic browser  resize. Other than that, I&#8217;m pretty happy with it (even if I&#8217;m kind of embarrassed  to have written such a gratuitously useless script.)<\/p>\n<p>*The site, since this article was written, has been rewritten. I&#8217;m no longer responsible for anything you see there in the way that I normally am. I tried my best to guide the site&#8217;s production, but we&#8217;re working with Drupal in a big way so the front end is simply out of my hands.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For the current EforAll Expo*1 web site, the designer suggested that we use a flash piece for the background. It&#8217;s a kind of hip event, for gamers and other technophiles so we made some assumptions about our user base and decided it was worth a go. The onus fell on me, as these things do, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-3726","post","type-post","status-publish","format-standard","hentry","category-javascript"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3uLY6-Y6","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/posts\/3726","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/comments?post=3726"}],"version-history":[{"count":0,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/posts\/3726\/revisions"}],"wp:attachment":[{"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/media?parent=3726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/categories?post=3726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/tags?post=3726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}