// observer for feature section
let featuresSection = document.querySelector('#featuresSection');
let callbackFeature = (items) => {
items.forEach((item) => {
if (item.isIntersecting) {
item.target.classList.add("in-page");
} else {
item.target.classList.remove("in-page");
}
});
}
let observerFeature = new IntersectionObserver(callbackFeature, {
threshold: 0.3
});
observerFeature.observe(featuresSection);
.features-section.in-page {
position: -webkit-sticky;
position: -moz-sticky;
position: -ms-sticky;
position: -o-sticky;
position: sticky !important;
top: 0 !important;
background-color: red;
display: block;
}
.features-img {
width:100%
object-fit: contain;
transition: all 0.5s cubic-bezier(0.215, 0.61, 0.355, 1);
}
<!-- Bootstrap CSS -->
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap/dist/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
crossorigin="anonymous"
/>
<div class="vh-100"><div>
<section id="featuresSection" class="features-section ">
<h2 class="text-center">Newest set of Advanced Features</h2>
<h3 class="text-center mb-5 clr-primary">Achieve the impossible</h3>
<div class="row wrapper features-row h-100">
<div class="col-6 px-0 col-md-4 order-md-2 features-img-container">
<img id="ftr-img-1"
class="features-img "
src="https://m.media-amazon.com/images/I/81LNgb-7FnL._AC_SX569_.jpg"
alt=""
/>
<img id="ftr-img-2"
class="features-img d-none d-md-block "
src="https://m.media-amazon.com/images/I/911Gc7IMlmL._AC_SX569_.jpg"
alt=""
/>
<img id="ftr-img-3"
class="features-img d-none d-md-block "
src="https://m.media-amazon.com/images/I/91z5KuonXrL._AC_SX569_.jpg"
alt=""
/>
<img id="ftr-img-4"
class="features-img d-none d-md-block "
src="https://m.media-amazon.com/images/I/81jYRL0oCSL._AC_SX679_.jpg"
alt=""
/>
</div>
<div
class="col-6 pl-3 pr-0 d-flex flex-column justify-content-between col-md-4 order-md-1 pr-md-4 pl-md-0 features-col "
>
<div class="features-info-container">
<h4>High Performance</h4>
<p>
Lorem ipsum dolor sit, amet consectetur adipisicing elit.
Ducimus facilis aut totam amet veniam ab. Eius optio obcaecati
laudantium suscipit?
</p>
</div>
<div class="features-info-container">
<h4>Long Lasting Battery</h4>
<p>
Lorem ipsum dolor sit, amet consectetur adipisicing elit.
Ducimus facilis aut totam amet veniam ab. Eius optio obcaecati
laudantium suscipit?
</p>
</div>
</div>
<div
class="col-12 px-0 d-flex mt-3 mt-md-0 flex-column justify-content-between col-md-4 order-md-3 pr-md-0 pl-md-4 features-col "
>
<div class="features-info-container">
<h4 class="text-md-right">Titanium Case</h4>
<p class="text-md-right">
Lorem ipsum dolor sit, amet consectetur adipisicing elit.
Ducimus facilis aut totam amet veniam ab. Eius optio obcaecati
laudantium suscipit? Lorem ipsum dolor sit amet.
</p>
</div>
<div class="features-info-container">
<h4 class="text-md-right">New GPS Antenna</h4>
<p class="text-md-right">
Lorem ipsum dolor sit, amet consectetur adipisicing elit.
Ducimus facilis aut totam amet veniam ab. Eius optio obcaecati
laudantium suscipit? Lorem ipsum dolor sit amet.
</p>
</div>
</div>
</div>
</section>
What could be the reasons why a sticky element fails to work, other than:
- Having a parent element with overflow:hidden
- Omitting to specify the top position to stick to
- The sticky element being within a flexbox or grid
- Lack of browser support for the sticky property
Despite the fact that my element doesn't exhibit any of those complications, I have meticulously examined for an overflow:hidden in the CSS both using CTRL+F and a script that verifies it in the console. Yet it fails to adhere to the top of the page. Any assistance would be appreciated.
This is the CSS applied to my section: The red background is utilized for testing purposes and functions correctly, but the sticky property does not.
.features-section.in-page {
position: -webkit-sticky;
position: -moz-sticky;
position: -ms-sticky;
position: -o-sticky;
position: sticky !important;
top: 0 !important;
background-color: red;
display: block;
}
Additionally, this is the structure of my section:
<section id="featuresSection" class="features-section ">
<h2 class="text-center">Newest set of Advanced Features</h2>
<h3 class="text-center mb-5 clr-primary">Achieve the impossible</h3>
<div class="row wrapper features-row h-100">
<div class="col-6 px-0 col-md-4 order-md-2 features-img-container">
<img id="ftr-img-1"
class="features-img "
src="https://m.media-amazon.com/images/I/81LNgb-7FnL._AC_SX569_.jpg"
alt=""
/>
<img id="ftr-img-2"
class="features-img d-none d-md-block "
src="https://m.media-amazon.com/images/I/911Gc7IMlmL._AC_SX569_.jpg"
alt=""
/>
<img id="ftr-img-3"
class="features-img d-none d-md-block "
src="https://m.media-amazon.com/images/I/91z5KuonXrL._AC_SX569_.jpg"
alt=""
/>
<img id="ftr-img-4"
class="features-img d-none d-md-block "
src="https://m.media-amazon.com/images/I/81jYRL0oCSL._AC_SX679_.jpg"
alt=""
/>
</div>
<div
class="col-6 pl-3 pr-0 d-flex flex-column justify-content-between col-md-4 order-md-1 pr-md-4 pl-md-0 features-col "
>
<div class="features-info-container">
<h4>High Performance</h4>
<p>
Lorem ipsum dolor sit, amet consectetur adipisicing elit.
Ducimus facilis aut totam amet veniam ab. Eius optio obcaecati
laudantium suscipit?
</p>
</div>
<div class="features-info-container">
<h4>Long Lasting Battery</h4>
<p>
Lorem ipsum dolor sit, amet consectetur adipisicing elit.
Ducimus facilis aut totam amet veniam ab. Eius optio obcaecati
laudantium suscipit?
</p>
</div>
</div>
<div
class="col-12 px-0 d-flex mt-3 mt-md-0 flex-column justify-content-between col-md-4 order-md-3 pr-md-0 pl-md-4 features-col "
>
<div class="features-info-container">
<h4 class="text-md-right">Titanium Case</h4>
<p class="text-md-right">
Lorem ipsum dolor sit, amet consectetur adipisicing elit.
Ducimus facilis aut totam amet veniam ab. Eius optio obcaecati
laudantium suscipit? Lorem ipsum dolor sit amet.
</p>
</div>
<div class="features-info-container">
<h4 class="text-md-right">New GPS Antenna</h4>
<p class="text-md-right">
Lorem ipsum dolor sit, amet consectetur adipisicing elit.
Ducimus facilis aut totam amet veniam ab. Eius optio obcaecati
laudantium suscipit? Lorem ipsum dolor sit amet.
</p>
</div>
</div>
</div>
</section>
This is the JS code employing an Intersection Observer to assign the position sticky and a red background color for debugging purposes:
// observer for feature section
let featuresSection = document.querySelector('#featuresSection');
let callbackFeature = (items) => {
items.forEach((item) => {
if (item.isIntersecting) {
item.target.classList.add("in-page");
} else {
item.target.classList.remove("in-page");
}
});
}
let observerFeature = new IntersectionObserver(callbackFeature, {
threshold: 0.3
});
observerFeature.observe(featuresSection);