Skip to content

Commit 5cf208d

Browse files
committed
feat: added possibility to add dynamic content elements
images and iframes
1 parent ad56c5c commit 5cf208d

2 files changed

Lines changed: 45 additions & 11 deletions

File tree

demo/index.html

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,10 @@
3232
margin: 1rem;
3333
}
3434
/* dynamically added images */
35-
.add-image ~ div {
35+
.add-dynamic-content ~ div {
3636
min-height: 100vh;
37-
position: relative;
38-
}
39-
.add-image ~ div img {
40-
position: absolute;
41-
bottom: 0;
37+
display: flex;
38+
align-items: end;
4239
}
4340

4441
/* Displaying the images on smaller viewports */
@@ -198,16 +195,17 @@ <h1>
198195
title="sample document"
199196
></iframe>
200197

201-
<h2>dynamic images</h2>
198+
<h2>dynamic images &amp; iframes</h2>
202199
<p>
203-
Add <code>div</code> element and image underneath (scroll down after
204-
pressing the following button)
200+
Add <code>div</code> element and included image and iframe underneath
201+
(scroll down after pressing the following button)
205202
</p>
206-
<button type="button" class="add-image">
207-
Add dynamic image underneath
203+
<button type="button" class="add-dynamic-content">
204+
Add dynamic image and iframe underneath
208205
</button>
209206
</main>
210207
<script type="module" src="index.js"></script>
208+
></script>
211209
<script>
212210
if ("serviceWorker" in navigator) {
213211
// Differentiate in between the ServiceWorker scripts for the different browser capabilities / support

demo/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,38 @@
11
import loadingAttributePolyfill from '../dist/loading-attribute-polyfill.module.js';
22

3+
// Test for dynamically inserted images and iframes
4+
let addDynamicContent = (event) => {
5+
let divElement = document.createElement('div'),
6+
imageElement = document.createElement('img'),
7+
iframeElement = document.createElement('iframe');
8+
9+
imageElement.setAttribute(
10+
'src',
11+
'img/lazyimg-src-loadinglazy.250x150.guetzli.jpg?loading=lazy&image-width=250&image-height=150'
12+
);
13+
imageElement.setAttribute('loading', 'lazy');
14+
imageElement.setAttribute('alt', '..');
15+
imageElement.setAttribute('width', '250');
16+
imageElement.setAttribute('height', '150');
17+
18+
iframeElement.setAttribute('src', 'iframe.html?loading=lazy');
19+
iframeElement.setAttribute('loading', 'lazy');
20+
iframeElement.setAttribute('title', '..');
21+
iframeElement.setAttribute('width', '320');
22+
iframeElement.setAttribute('height', '180');
23+
24+
divElement.appendChild(imageElement);
25+
divElement.appendChild(iframeElement);
26+
27+
document.querySelector('main').insertAdjacentElement('beforeend', divElement);
28+
29+
// Call for preparing the sample image element included the latest
30+
loadingAttributePolyfill.prepareElement(divElement.querySelector('img'));
31+
loadingAttributePolyfill.prepareElement(divElement.querySelector('iframe'));
32+
33+
event.preventDefault();
34+
};
35+
36+
document
37+
.querySelector('button.add-dynamic-content')
38+
.addEventListener('click', addDynamicContent);

0 commit comments

Comments
 (0)