0

David Thomas previously answered this question about super-imposed banners, which are viewed by default before the visitor views the main page content.

Superimpose a banner div over another div, with jQuery if possible

I have coded as far as I can and it works to a point, but there are a couple of issues, due entirely I'm sure to my low-level knowledge of such matters!

1) The X close does not work, but then it won't because I haven't put the JSQuery part of the coding anywhere yet - where does it go - is there a seperate file for JS (in which case how do you reference it in the html?), or does it go in the header with the CSS etc? Sorry for the stupid question

2) My banner is only 250px in height, but the content beneath is several paragraphs long - the content has been hidden by the banner (with a white background, that I chose since the banner itself is white) but the footer is right at the bottom of the page, below where the content ends, so there is a big gap which looks a bit crap. Can I get the page to only show the banner at it's actual height and then when it is closed, the page extends to what it looks like with the content displayed? Non-technical terms I know but don;t know how else to explain...

3) My banner does not cover the whole width of the content so can I centre it, rather than place it at 0,0?

Many thanks Claire

Community
  • 1
  • 1
clf1605
  • 1
  • 1

1 Answers1

0

Question 1: You can either copy the jquery code in a separate file, e.g. called main.js, and include it in the head part of you html page with

<script src="main.js"></script>

in case the file would be next to your html-page. Better practice is to use separate folders for css, scripts and images, so e.g. in case you have a directory called scripts the include would be

<script src="/scripts/main.js"></script>

for a relative path (assuming your html-pages are on root level with scripts as subdirectory) or, just in case you're testing locally without a local server

<script src="scripts/main.js"></script>

Be sure to include your version of jquery before including a javascript file using jquery functions.
You can also include the script part in the html page (also after the include of jquery) as inline script: <script> /* your code */ </script>. For completeness - as I don't know the html in question (if it's HTML5 or HTML4) - it could be necessary to add the type attribute to the script tag: <script type="text/javascript"> This applies to the the script tag including ths js-file as well as to the script tag for the online script. The type attribute is optional in HTML5, but mandatory in HTML4.

Also for completeness: You could also consider to include the scripts before the closing body tag instead of into the head section - detailed explanation why it could be better to include css in the header and js at the end can be found here: Where to place Javascript in a HTML file?

For further reference for the question inline script vs. external script files: When should I use Inline vs. External Javascript?

Question 2: No. There has been the javascript function resizeTo() - called like e.g. window.resizeTo(400,400) - that made it possible to resize the browser window, but for good reasons this has been disabled by many browsers on default. Reference e.g. here: The javascript "resizeTo" function not working in Chrome and Opera and here: http://kb.mozillazine.org/Resizing_oversize_window#JavaScript_no_longer_allowed_to_resize_windows__through_the_Location_Bar. You can use e.g. a mask instead - probably you've already seen this on many pages, e.g. when images/slideshows/videos etc. are displayed in a modal window and the page below the modal window is covered with a grey, transparent div. Just as example a very simplified Fiddle. When you click the banner, the banner as well as the mask will be hidden. The jquery code in the Fiddle is already executed when the Fiddle is loaded and could be used wrapped in a $( document ).ready(function() {} to be executed when jquery is loaded to set the height of the mask to the document height:

$(document).ready(function() {
  $("#banner").on("click", function() {
    $("#banner, #mask").hide();
  });
  $('#mask').height($(document).height());
});

For reference as this is only one possible approach to set the mask to cover the screen (with additional CSS in the Fiddle like setting the width of the mask to 100%) you can check several approaches here: How to make a div 100% of page (not screen) height?

Question 3: One of several solutions to display a div centered is used in above Fiddle. You can check a variety of solutions provided here: Using jQuery to center a DIV on the screen

Community
  • 1
  • 1
matthias_h
  • 11,356
  • 9
  • 22
  • 40