Intention:
-
If the below div is in offset the sticky button should be visible.
<div class="cmp-show-pinnedbutton" ></div>
-
if the below div is in offset the sticky button should be hidden.
<div class="cmp-show-pinnedbutton" ></div>
-
In between the classes
<div class="cmp-hide-pinnedbutton" ></div>
<div class="cmp-show-pinnedbutton" ></div>
the sticky button will be visible
Problem
- It works only on Single Instance. If I repeat the them it’s not working.
Working Scenario
<div class="cmp-show-pinnedbutton"></div>
<div class="cmp-show-pinnedbutton"></div>
<p>Some text here</p>// where sticky button visible
<div class="cmp-hide-pinnedbutton"></div>
Not working Scenario
<div class="cmp-show-pinnedbutton"></div>
<div class="cmp-show-pinnedbutton"></div>
<p>Some text here</p>//sticky button visible
<div class="cmp-hide-pinnedbutton"></div>
<p>Some text here</p>//sticky button not visible
<div class="cmp-show-pinnedbutton"></div> ***Not working where sticky button should visible
sticky button should visible
<div class="cmp-hide-pinnedbutton"></div>
Thoughts
- I have referred the below stack overflow Solution but somehow I
coudn’t able to achieve it.
jQuery – Treat multiple instances of the same class separately?
let pinnedbutton = document.getElementsByClassName('cmp-pinned-button')[0];
let hide_component = document.getElementsByClassName('cmp-hide-pinnedbutton')[0],
show_component = document.getElementsByClassName('cmp-show-pinnedbutton')[0],
_screenWidth = window.innerWidth,
observe = parseInt(window.innerHeight) - (_screenWidth < 1279 ? 80 : 20);
console.log(window.innerHeight);
window.addEventListener('scroll', () => {
let hide = hide_component.getBoundingClientRect().y,
show = show_component.getBoundingClientRect().y,
submitMain = pinnedbutton; //submitMain: pinned button div
if (show < observe || hide > observe) {
submitMain.classList.add('pinned_visible');
}
if (hide < observe || show > observe) {
submitMain.classList.remove('pinned_visible');
}
});
.cmp-pinned-button {
position: fixed;
bottom: 0px;
max-height: 65px;
width: 100%;
height: 100%;
background-color: rgba(256,256,256,0.5);
display: flex;
align-items: center;
justify-content: center;
padding: rem(20px 0);
z-index: 999999;
transform: scaleY(1);
transform-origin: top;
transition: all 0.05s ease;
display: none;
text-align: center;
font-size: 25px;
text-decoration: none;
}
.cmp-pinned-button.pinned_visible{
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="cmp-pinned-button" data-component="pinnedbutton">
<div class="button">
<a class="cmp-button" href="#">
<span class="cmp-button__text">MEET WITH AN EXPERT</span>
</a>
</div>
</div>
<p style="height: 500px; background-color: aqua;"></p>
<div class="cmp-show-pinnedbutton" ></div>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<div class="cmp-hide-pinnedbutton"></div>
<p style="height: 500px; background-color: aqua;"></p>
<div class="cmp-show-pinnedbutton" ></div>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<div class="cmp-hide-pinnedbutton"></div>