The main solution out there is:
\"Just throw a loading screen up until the page is loaded\".
But my goal is to build pages that pres
I think I have a solution! (sorry, just very exited I got this working:)
//library (minified)
this.BgImgFader=function(){var styleRules;function getArray(str){if(str.indexOf(',')==0){str.substring(1);}if(str.lastIndexOf(',')==str.length-1){str.substring(0,str.length-1);}if(str.indexOf(',')==-1){var selectors=[str];}else{var selectors=str.split(',');}for(var i=0;i
#exampleId {
width: 300px;
height: 200px;
margin: 10px 0px 0px 10px;
background-color: #AAAAAA;
}
#exampleId .exampleClass {
width: 200px;
height: 130px;
padding: 5px;
}
#exampleId .exampleClass::after {background:url(https://placeimg.com/640/480/any) center/cover no-repeat;}
Some other text to illustrate how this can be implemented.
I assume you want transparent background images because you have text in the element that you do want to show from the start?
I basically ended up building a tiny library. Pure JavaScript, no jQuery required.
All you have to do is add these three BgImgFader
lines in JS:
//instantiate BgImgFader in global domain
var BgImgFader = new BgImgFader();
window.onload = function(){
//prepare specified elements
BgImgFader.prepare(0, '.exampleClass'); //stylesheet, selectors
//fade specified elements
BgImgFader.fade(0, '.exampleClass', true, 0, 0.5, 0.002); //stylesheet, selectors, global, startOpacity, endOpacity, delta
};
And add the following to all your background-image-elements in CSS:
#example {...}
#example::after {background:url(path/to/image.png) center/cover no-repeat;}
So for every element with a background-image, you have to add the #example::after {
rule and place your background-image in there. (You do NOT have to do this in HTML, only in CSS.)
I've added the library's source code as a code snippet below. Comments are in the code.
You can either paste the code at the top of your own script, or put the code in a file and in your HTML, link to that file like you would to any other library (before your own code):
/**
* BgImgFader - Library:
* This library makes it possible to fade the background-image of an element.
* The image can be faded in or out.
*
* Compatibility:
* - IE9 and higher should be fine, lower could give trouble.
* - Older versions of FF/Chrome will probably give some problems too, but I think we can safely assume
* that those who chose either one of these browsers, did so because they choose NOT to live in the past..
* - Opera and others... I have absolutely no idea.
*
* #################################################################################
* INSTRUCTIONS---------------------------------------------------------------------
* 1. In CSS:
* a. For every element with a background-image, create an '::after' rule, and put the image in there:
* (You don't have to create these '::after'-elements in the HTML)
*
* #element {...}
* #element::after {background:url(path/to/image.png) center/cover no-repeat;}
*
* The following declarations will be added by the BgImgFader, keep that in mind:
* #element {position:relative;}
* #element::after {position:absolute; top:0;right:0;bottom:0;left:0; opacity:0.0; content:"";}
*
* (The important one is 'position:relative;', the ones on the '::after'-element have no consequences)
*
* ---------------------------------------------------------------------------------
* 2. In JavaScript:
* a. Instantiate the BgImgFader in the global domain: var BgImgFader = new BgImgFader();
*
*
* b. Prepare the elements with a background-image: BgImgFader.prepare(0, 'elements'); //style, selectors
*
* - style: Reference to the style sheet with the rules for the specified elements.
* This can be either an INTEGER for internal style sheets (0),
* or a STRING of a filename for external style sheets ('style.css').
* - selectors: STRING reference to the selectors in the style rules.
* This works the same as in the CSS, below a few examples.
* Individual tags: ('div') ('#id') ('.class')
* Multiple tags: ('div.class') ('#id .class') ('.class.subclass')
* Multiple selectors: ('div, #id, div.class, #id .class, .class.subclass')
*
*
* c. Initiate the fade: BgImgFader.fade('style.css', 'elements', true, 0, 0.5, 0.005); //style, selectors, global, startOpacity, endOpacity, delta
*
* - style: See 2b for the details.
* - selectors: See 2b for the details.
* - global: BOOLEAN that deternimes whether only complete matches for the selectors are allowed,
* or partial matches as well, increasing the range of the BgImgFader.
* TRUE allowes partial matches: feed the BgImgFader '.class' and it will also try to fade 'div .class'.
* FALSE allowes only complete matches.
* - startOpacity: FLOAT that indicates the start opacity (0.0 - 1.0).
* - endOpacity: FLOAT that indicates the end opacity (0.0 - 1.0).
* - delta: FLOAT that indicates the delta of every fade-iteration (1.0 - 0.00000000...1).
* The effective range is approximately (0.1 - 0.0001).
* A smaller delta means a slower fade.
* A positive delta in combination with startend fades the image out.
*
* #################################################################################
*/
this.BgImgFader = function() {
var styleRules;
//GET/SET-FUNCTIONS=================================================================
//GET SELECTORS---------------------------------------------------------------------
function getArray(str) {
/* This function is invoked by this.prepare() and this.fade().
* This function converts the specified string of selectors to an array, and returns that.
*/
//strip trailing comma's
if (str.indexOf(',')==0) {str.substring(1);} //strip first comma
if (str.lastIndexOf(',')==str.length-1) {str.substring(0,str.length-1);} //strip last comma
//store selectors in array
if (str.indexOf(',')==-1) {var selectors = [str];}
else {var selectors = str.split(',');}
//trim trailing spaces
for (var i=0; i').
* A filename is for external sheets (e.g. 'style.css').
* See the instructions in the header of this file for details.
*/
if (typeof style === 'number') {
return document.styleSheets[style];
} else {
//find style sheet
for (var i=0; i
source code of library, not a working code snippet!
Interesting fact: This does not work on jsfiddle.net, because their own style sheets get mixed in with the one from the fiddle, making it impossible to determine in which sheet (based on number) the required CSS-rules reside.