Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery: retain a responsive div size for an image container before fadeIn
    primarykey
    data
    text
    <p>I have a bit of jQuery that selects a random image from a folder and fades it in within a div on each page load. The problem is the menu div just below the image jumps up into the blank space of the image before the image fades in.</p> <p>What's the best way to have the image div retain height before the image fades in? CSS to "fill" the div before the image fades in? Or load a blank .jpg before the random image fades in? (All random images are the same size 1000px width x 250px height).</p> <p><em>I need to retain the responsiveness of the design</em>, so it's possible the best solution is to preload a blank jpg and then fade in the random image.</p> <p>I haven't included the image and caption arrays for simplicity, but this function outputs the image with the .randomheader class:</p> <p><strong>jQuery:</strong></p> <pre><code> &lt;script&gt; $(document).ready(function(){ $('.randomheader').randomImage(); }); &lt;/script&gt; &lt;script&gt; (function($){ $.randomImage = { defaults: { path: '/fullpathhere/headerimages/', myImages: ['image1.jpg' , 'image2.jpg' , 'image3.jpg'], myCaptions: ['Caption 1' , 'Caption 2' , 'Caption 3'] } } $.fn.extend({ randomImage:function(config) { var config = $.extend({}, $.randomImage.defaults, config); return this.each(function() { var imageNames = config.myImages; var imageCaptions = config.myCaptions; var imageNamesSize = imageNames.length; var randomNumber = Math.floor(Math.random()*imageNamesSize); var displayImage = imageNames[randomNumber]; var displayCaption = imageCaptions[randomNumber]; var fullPath = config.path + displayImage; // Load the random image $(this).attr( { src: fullPath, alt: displayImage }).fadeIn('slow'); // Load the caption $('#caption').html(displayCaption).fadeIn('slow'); }); } }); &lt;/script&gt; </code></pre> <p><strong>HTML:</strong> </p> <pre><code> &lt;header id="masthead" class="site-header" role="banner"&gt; &lt;!-- random image loads in the div below --&gt; &lt;img src="" class="randomheader" width="1000" height="250" alt="header image"&gt; &lt;div id="caption"&gt;&lt;/div&gt; &lt;!-- The nav div below jumps up into the div above --&gt; &lt;nav id="site-navigation" class="main-navigation" role="navigation"&gt; // nav here &lt;/nav&gt; </code></pre> <p><strong>CSS:</strong></p> <pre><code>.randomheader { margin-top: 24px; margin-top: 1.714285714rem; } img.randomheader { max-width: 100%; height: auto; display: none; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload