<!--Main layout-->
<main class="my-4">
<div class="container">
<!--Section: Posts-->
<section class="text-center mb-4" id="posts">
<div class="row">
<div class="col-md-6 mb-4">
<div
class="bg-image hover-overlay ripple shadow-1-strong rounded mb-4"
data-mdb-ripple-color="light"
>
<img
data-mdb-lazy-src="https://mdbootstrap.com/img/Photos/Others/images/29.webp"
data-mdb-lazy-placeholder="https://placehold.it/1321x583?text=Loading"
class="w-100 lazy"
/>
<a href="#!">
<div
class="mask"
style="background-color: rgba(251, 251, 251, 0.2);"
></div>
</a>
</div>
<h5>This is an title of the article</h5>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Quisquam cupiditate veniam voluptatibus laudantium cum dolorem
illo. Quos architecto deserunt saepe.
</p>
<a class="btn btn-info btn-rounded" href="#!" role="button"
>Read more</a
>
</div>
<div class="col-md-6 mb-4">
<div
class="bg-image hover-overlay ripple shadow-1-strong rounded mb-4"
data-mdb-ripple-color="light"
>
<img
data-mdb-lazy-src="https://mdbootstrap.com/img/Photos/Others/images/27.webp"
data-mdb-lazy-placeholder="https://placehold.it/1321x583?text=Loading"
class="w-100 lazy"
/>
<a href="#!">
<div
class="mask"
style="background-color: rgba(251, 251, 251, 0.2);"
></div>
</a>
</div>
<h5>This is an title of the article</h5>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Quisquam cupiditate veniam voluptatibus laudantium cum dolorem
illo. Quos architecto deserunt saepe.
</p>
<a class="btn btn-info btn-rounded" href="#!" role="button"
>Read more</a
>
</div>
</div>
<div class="row">
<div class="col-md-6 mb-4">
<div
class="bg-image hover-overlay ripple shadow-1-strong rounded mb-4"
data-mdb-ripple-color="light"
>
<img
data-mdb-lazy-src="https://mdbootstrap.com/img/Photos/Others/images/25.webp"
data-mdb-lazy-placeholder="https://placehold.it/1321x583?text=Loading"
class="w-100 lazy"
/>
<a href="#!">
<div
class="mask"
style="background-color: rgba(251, 251, 251, 0.2);"
></div>
</a>
</div>
<h5>This is an title of the article</h5>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Quisquam cupiditate veniam voluptatibus laudantium cum dolorem
illo. Quos architecto deserunt saepe.
</p>
<a class="btn btn-info btn-rounded" href="#!" role="button"
>Read more</a
>
</div>
<div class="col-md-6 mb-4">
<div
class="bg-image hover-overlay ripple shadow-1-strong rounded mb-4"
data-mdb-ripple-color="light"
>
<img
data-mdb-lazy-src="https://mdbootstrap.com/img/Photos/Others/images/24.webp"
data-mdb-lazy-placeholder="https://placehold.it/1321x583?text=Loading"
class="w-100 lazy"
/>
<a href="#!">
<div
class="mask"
style="background-color: rgba(251, 251, 251, 0.2);"
></div>
</a>
</div>
<h5>This is an title of the article</h5>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Quisquam cupiditate veniam voluptatibus laudantium cum dolorem
illo. Quos architecto deserunt saepe.
</p>
<a class="btn btn-info btn-rounded" href="#!" role="button"
>Read more</a
>
</div>
</div>
<div class="row">
<div class="col-md-6 mb-4">
<div
class="bg-image hover-overlay ripple shadow-1-strong rounded mb-4"
data-mdb-ripple-color="light"
>
<img
data-mdb-lazy-src="https://mdbootstrap.com/img/Photos/Others/images/31.webp"
data-mdb-lazy-placeholder="https://placehold.it/1321x583?text=Loading"
class="w-100 lazy"
/>
<a href="#!">
<div
class="mask"
style="background-color: rgba(251, 251, 251, 0.2);"
></div>
</a>
</div>
<h5>This is an title of the article</h5>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Quisquam cupiditate veniam voluptatibus laudantium cum dolorem
illo. Quos architecto deserunt saepe.
</p>
<a class="btn btn-info btn-rounded" href="#!" role="button"
>Read more</a
>
</div>
<div class="col-md-6 mb-4">
<div
class="bg-image hover-overlay ripple shadow-1-strong rounded mb-4"
data-mdb-ripple-color="light"
>
<img
data-mdb-lazy-src="https://mdbootstrap.com/img/Photos/Others/images/23.webp"
data-mdb-lazy-placeholder="https://placehold.it/1321x583?text=Loading"
class="w-100 lazy"
/>
<a href="#!">
<div
class="mask"
style="background-color: rgba(251, 251, 251, 0.2);"
></div>
</a>
</div>
<h5>This is an title of the article</h5>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Quisquam cupiditate veniam voluptatibus laudantium cum dolorem
illo. Quos architecto deserunt saepe.
</p>
<a class="btn btn-info btn-rounded" href="#!" role="button"
>Read more</a
>
</div>
</div>
<div class="row" id="spinner" style="display: none;">
<div class="col-md-12">
<div class="spinner-border mx-auto"></div>
</div>
</div>
</section>
<!--Section: Posts-->
</div>
</main>
<!--Main layout-->
const postsContainer = document.getElementById('posts');
const spinner = document.getElementById('spinner');
const items = [
{
img: 'https://mdbcdn.b-cdn.net/img/Photos/Others/images/31.webp',
title: 'This is an title of the article',
text:
'Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam cupiditate veniam voluptatibus laudantium cum dolorem illo. Quos architecto deserunt saepe.',
},
{
img: 'https://mdbcdn.b-cdn.net/img/Photos/Others/images/23.webp',
title: 'This is an title of the article',
text:
'Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam cupiditate veniam voluptatibus laudantium cum dolorem illo. Quos architecto deserunt saepe.',
},
{
img: 'https://mdbcdn.b-cdn.net/img/Photos/Others/images/29.webp',
title: 'This is an title of the article',
text:
'Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam cupiditate veniam voluptatibus laudantium cum dolorem illo. Quos architecto deserunt saepe.',
},
{
img: 'https://mdbcdn.b-cdn.net/img/Photos/Others/images/27.webp',
title: 'This is an title of the article',
text:
'Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam cupiditate veniam voluptatibus laudantium cum dolorem illo. Quos architecto deserunt saepe.',
},
{
img: 'https://mdbcdn.b-cdn.net/img/Photos/Others/images/25.webp',
title: 'This is an title of the article',
text:
'Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam cupiditate veniam voluptatibus laudantium cum dolorem illo. Quos architecto deserunt saepe.',
},
{
img: 'https://mdbcdn.b-cdn.net/img/Photos/Others/images/24.webp',
title: 'This is an title of the article',
text:
'Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam cupiditate veniam voluptatibus laudantium cum dolorem illo. Quos architecto deserunt saepe.',
},
{
img: 'https://mdbcdn.b-cdn.net/img/Photos/Others/images/31.webp',
title: 'This is an title of the article',
text:
'Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam cupiditate veniam voluptatibus laudantium cum dolorem illo. Quos architecto deserunt saepe.',
},
{
img: 'https://mdbcdn.b-cdn.net/img/Photos/Others/images/32.webp',
title: 'This is an title of the article',
text:
'Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam cupiditate veniam voluptatibus laudantium cum dolorem illo. Quos architecto deserunt saepe.',
},
];
const getPostTemplate = (post) => {
// returns the HTML template with post's title, image & text
return `
<div class="col-md-6 mb-4">
<div class="bg-image hover-overlay ripple shadow-1-strong rounded mb-4" data-mdb-ripple-color="light">
<img
src="${post.img}"
class="w-100 lazy"
/>
<a href="#!">
<div
class="mask"
style="background-color: rgba(251, 251, 251, 0.2);"
>
</div>
</a>
</div>
<h5>${post.title}</h5>
<p>
${post.text}
</p>
<a class="btn btn-info btn-rounded" href="#!" role="button"
>Read more</a>
</div>
`;
};
// posts - array of templates
const posts = items.map((item) => getPostTemplate(item));
const generateRow = (firstPost, secondPost) => {
// Returns a div.row element with two columns generated based on arguments
let el = document.createElement('div');
el.classList.add('row');
el.innerHTML = `
${firstPost}
${secondPost}
`;
return el;
};
// rows - array of rows with two posts each
const rows = [];
// iterates over posts and creates a row for every two of them
for (let i = 0; i < posts.length - 1; i += 2) {
rows.push(generateRow(posts[i], posts[i + 1]));
}
// renderedItems - number of items already rendered
let renderedItems = 0;
const renderItems = (index) => {
// timeout simulates delay in loading items (f.e. API call)
setTimeout(() => {
// hide spinner
spinner.style.display = 'none';
postsContainer.appendChild(rows[index]);
}, 1500);
};
const loadItems = () => {
if (renderedItems < rows.length) {
// show spinner
postsContainer.appendChild(spinner);
spinner.style.display = 'flex';
renderItems(renderedItems);
renderedItems++;
// Removes event listener after all items have been loaded
if (renderedItems === rows.length) {
window.removeEventListener('complete.mdb.infiniteScroll', loadItems);
}
}
};
// load items when window is scrolled to the end
window.addEventListener('complete.mdb.infiniteScroll', loadItems);
// init infinite scroll
new mdb.InfiniteScroll(window);