I have a scrollTo function on my page where when you click on a specific button you scroll to a section with a unique ID.
The problem is I am using lazy loading for the images on my website so, this will cause the ScrollTo to stop halfway through the page because of the images which are lazy-loaded.
After all, images are loaded and I click again on the button it works just fine.
My lazy load code:
(() => {
const runLazy = () => {
let images = [...document.querySelectorAll('[data-lazy]')];
const settings = {
rootMargin: '0px',
threshold: 0.02
};
let observer = new IntersectionObserver((imageEntites) => {
imageEntites.forEach((image) => {
if (image.isIntersecting) {
observer.unobserve(image.target);
image.target.src = image.target.dataset.lazy;
image.target.onload = () =>
image.target.classList.add('loaded');
}
});
}, settings);
images.forEach((image) => observer.observe(image));
};
runLazy();
})();
My scroll to code:
(() => {
document.querySelectorAll('a[href^="#"]').forEach((elem) => {
elem.addEventListener('click', (e) => {
e.preventDefault();
let block = document.querySelector(elem.getAttribute('href')),
offset = elem.dataset.offset
? parseInt(elem.dataset.offset)
: 0,
bodyOffset = document.body.getBoundingClientRect().top;
window.scrollTo({
top: block.getBoundingClientRect().top - bodyOffset + offset,
behavior: 'smooth'
});
});
});
})();
Is there a way to fix this?
I have a scrollTo function on my page where when you click on a specific button you scroll to a section with a unique ID.
The problem is I am using lazy loading for the images on my website so, this will cause the ScrollTo to stop halfway through the page because of the images which are lazy-loaded.
After all, images are loaded and I click again on the button it works just fine.
My lazy load code:
(() => {
const runLazy = () => {
let images = [...document.querySelectorAll('[data-lazy]')];
const settings = {
rootMargin: '0px',
threshold: 0.02
};
let observer = new IntersectionObserver((imageEntites) => {
imageEntites.forEach((image) => {
if (image.isIntersecting) {
observer.unobserve(image.target);
image.target.src = image.target.dataset.lazy;
image.target.onload = () =>
image.target.classList.add('loaded');
}
});
}, settings);
images.forEach((image) => observer.observe(image));
};
runLazy();
})();
My scroll to code:
(() => {
document.querySelectorAll('a[href^="#"]').forEach((elem) => {
elem.addEventListener('click', (e) => {
e.preventDefault();
let block = document.querySelector(elem.getAttribute('href')),
offset = elem.dataset.offset
? parseInt(elem.dataset.offset)
: 0,
bodyOffset = document.body.getBoundingClientRect().top;
window.scrollTo({
top: block.getBoundingClientRect().top - bodyOffset + offset,
behavior: 'smooth'
});
});
});
})();
Is there a way to fix this?
Share Improve this question asked Jul 21, 2021 at 9:54 GalanthusGalanthus 2,2905 gold badges19 silver badges42 bronze badges 1- Do the images loading in change the height of the page? If you can prefill the space that the images will take up with a placeholder of the same size, then the images loading should have no effect on the scroll. – DBS Commented Jul 21, 2021 at 10:05
3 Answers
Reset to default 4This seems to be caused by the image size change event during lazy loading.
So you could just set fixed height
and width
to the lazy load images, to skip this issue.
Edit:
Since fixed image size is not suitable, you can fix this with location.href = '#your-image-tag'
, plus window.scrollBy
in image.onload
event.
key code:
(() => {
document.querySelectorAll('a[href^="#"]').forEach((elem) => {
elem.addEventListener('click', (e) => {
e.preventDefault();
location.href = elem.getAttribute('href')
});
});
})()
image.target.onload = () => {
image.target.classList.add("loaded");
// TODO: check current image below or upper the target image
window.scrollBy(0, image.target.clientHeight)
// or window.scrollBy(0, 0 - image.target.clientHeight)
}
live demo: https://codesandbox.io/s/focused-field-2q3py?file=/src/index.js
I tried any solutions, but this is better for me:
element.scrollIntoView({
block: 'start',
behavior: "smooth"
});
check this out:
$("a").click(function(e) {
e.preventDefault();
var trigger = $(this).attr("href");
$('html,body').animate({scrollTop: $(trigger).offset().top},300);
setTimeout(function(){$('html,body').animate({scrollTop: $(trigger).offset().top},300);},300);
});
section{width:100%;height:600px}
#section1{background-color:red}
#section2{background-color:navy}
#section3{background-color:blue}
#section4{background-color:yellow}
#section5{background-color:green}
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<a href="#section1">section1</a>
<a href="#section2">section2</a>
<a href="#section3">section3</a>
<a href="#section4">section4</a>
<a href="#section5">section5</a>
<section id="section1"></section>
<section id="section2"></section>
<section id="section3"></section>
<section id="section4"></section>
<section id="section5"></section>
After the first scroll, if the page fails to scroll properly due to lazy loading, the mand with a delay of 300 milliseconds will solve this problem.