<div class="mktoImg imgBrowserOnly" id="heroImageBrowser" mktoName="Hero Image" data-background-for="#heroImage"></div>
.imgBrowserOnly {
display: none;
}
/**
* @version v1 2020-04-04
* @copyright © 2020 Sanford Whiteman
* @license Hippocratic 2.1
*/
(function(){
var arrayify = getSelection.call.bind([].slice),
srcSetters = {
"data-background-for" : function(el,src){ el.style.backgroundImage = "url(\"" + src + "\")"; },
"data-src-for" : function(el,src){ el.src=src; }
};
function bindImgBrowsers(){
var imgBrowsers = document.querySelectorAll(".imgBrowserOnly");
arrayify(imgBrowsers)
.forEach(function(browseWrapper){
var hasImgChild = !!browseWrapper.querySelector("img");
if (hasImgChild) {
pushSelected({currentTarget : browseWrapper});
} else {
browseWrapper.addEventListener("load", pushSelected, true);
}
});
}
function pushSelected(e){
var wrapperEl = e.currentTarget,
selectedImgsrc=wrapperEl.querySelector("img").src;
Object.keys(srcSetters)
.forEach(function(setterName){
if(wrapperEl.hasAttribute(setterName)){
var targetEls = document.querySelectorAll(wrapperEl.getAttribute(setterName));
arrayify(targetEls)
.forEach(function(targetEl){
srcSetters[setterName](targetEl,selectedImgSrc);
});
}
});
}
document.addEventListener("DOMContentLoaded",bindImgBrowsers);
})();
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta class="mktoString" id="heroText" mktoName="Hero Text" allowHtml="true">
<title>mktoImg to background-image Demo</title>
<style>
.imgBrowserOnly {
display: none;
}
#heroImage {
height: 250px;
background-size: cover;
}
#heroImage h1.heroText {
color: whitesmoke;
background-color: darkmagenta;
margin: 40px 20px;
padding: 20px;
display: inline-block;
font-family: "Arial Black";
transform: rotateZ(-2deg);
}
</style>
<script>
/**
* @version v1 2020-04-04
* @copyright © 2020 Sanford Whiteman
* @license Hippocratic 2.1
*/
(function(){
var arrayify = getSelection.call.bind([].slice),
srcSetters = {
"data-background-for" : function(el,src){ el.style.backgroundImage = "url(\"" + src + "\")"; },
"data-src-for" : function(el,src){ el.src=src; }
};
function bindImgBrowsers(){
var imgBrowsers = document.querySelectorAll(".imgBrowserOnly");
arrayify(imgBrowsers)
.forEach(function(browseWrapper){
var hasImgChild = !!browseWrapper.querySelector("img");
if (hasImgChild) {
pushSelected({currentTarget : browseWrapper});
} else {
browseWrapper.addEventListener("load", pushSelected, true);
}
});
}
function pushSelected(e){
var wrapperEl = e.currentTarget,
selectedImgsrc=wrapperEl.querySelector("img").src;
Object.keys(srcSetters)
.forEach(function(setterName){
if(wrapperEl.hasAttribute(setterName)){
var targetEls = document.querySelectorAll(wrapperEl.getAttribute(setterName));
arrayify(targetEls)
.forEach(function(targetEl){
srcSetters[setterName](targetEl,selectedImgSrc);
});
}
});
}
document.addEventListener("DOMContentLoaded",bindImgBrowsers);
})();
</script>
</head>
<body>
<div class="mktoImg imgBrowserOnly" id="heroImageBrowser" mktoName="Hero Image" data-background-for="#heroImage"></div>
<div id="heroImage">
<h1 class="heroText">${heroText}</h1>
</div>
</body>
</html>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.