For the sake of completeness:
I modified the script so it creates an img-element inside the div id=”random_header_image” – element.
The code looks now:
<script>
window.onload = function () {
var elem = document.createElement("img");
elem.setAttribute("id", "random_header_img");
var pictures = new Array('https://storch.watch/wp-content/uploads/2022/06/header_edit4_06_500px.jpg','https://storch.watch/wp-content/uploads/2022/06/header_edit4_05_500px-scaled.jpg','https://storch.watch/wp-content/uploads/2022/06/header_edit4_04_500px.jpg','https://storch.watch/wp-content/uploads/2022/06/header_edit4_03_500px.jpg','https://storch.watch/wp-content/uploads/2022/06/header_edit4_02_500px.jpg','https://storch.watch/wp-content/uploads/2022/06/header_edit4_500px-scaled.jpg');
var numPics = pictures.length;
if (document.images) {
var chosenPic = Math.floor((Math.random() * numPics));
elem.setAttribute("src", pictures[chosenPic]);
document.getElementById('random_header_image').appendChild(elem);
}
}
</script>
Personally I could better treat the img-element in css than editing the div id=”random_header_image” – element as background-image in css and got it to scale as expected and stay centered on all devices.
This is my css:
css of surrounding div with id “random_header_image”:
#random_header_image {
justify-content: center;
align-items: center;
padding: 0 !important;
padding-left: 0 !important;
padding-right: 0 !important;
display: block;
height: 400px;
margin-left: calc(-1 * var(--wp--custom--spacing--outer)) !important;
margin-right: calc(-1 * var(--wp--custom--spacing--outer)) !important;
}
css of img-element with id “random_header_img”:
#random_header_img {
object-fit: cover;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 400px;
padding: 0 !important;
animation: fadeIn ease 4s;
-webkit-animation: fadeIn ease 4s;
-moz-animation: fadeIn ease 4s;
-o-animation: fadeIn ease 4s;
-ms-animation: fadeIn ease 4s;
}
-
This reply was modified 2 years, 4 months ago by krischan941.