I’m trying to add lazy loading on Hugo. But I’m stuck and I’m requesting some help.
I have the following code to load each blog post on Hugo:
<section class="content-rich-text blog-styling xs-pb-10">
<div class="blog-styling">
<div class="content-rich-text-inner prl-5 blog-styling">
<div class="content-rich-text-leading blog-styling">
{{ .Content }}
</div>
{{ if hugo.Environment | eq "development" }}
<script type="text/javascript">
var images = document.getElementsByTagName("img");
for (var i = 0; i < images.length; i++) {
images[i].src = images[i].src.replace("/", "/");
}
</script>
{{ end }}
<!-- {{- partial "components/comments.html" . }} -->
</div>
</section>
Up to now, the code is working and the images are OK. However, I can’t find the best way to add lazy loading, given that I’m using the following code section to load images from the content:
<script type="text/javascript">
var images = document.getElementsByTagName("img");
for (var i = 0; i < images.length; i++) {
images[i].src = images[i].src.replace("/", "/");
}
</script>
What’s the best solution to load these images while still adding lazy loading?
Is there an alternative to load images efficiently on Hugo?
Any solution is greatly appreciated. Thank you guy.