I have coded using jquery and css to reveal my contact form while I scrolling down the page. But I tried to set a range of element display while scrolling. At this time I have only setup the scrolling down and hiding part. Need to develop code for Display this object within 200px - 1024px range only.
Anyone have idea for this. Please send me an answer.
Please see this animated gif: https://gph.is/2RpQrOp OR https://ibb.co/6y7wCmK
var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
if (1600 > currentScrollPos) {
document.getElementById("sticky").style.top = "200px";
} else {
document.getElementById("sticky").style.top = "-1000px";
}
prevScrollpos = currentScrollPos;
}
/* Test contact-sticky*/
.sticky {
position: fixed;
transition: top 0.3s;
top: -1000px;
left: 827px;
right: 46px;
background-color: #ebc11d;
padding: 50px;
font-size: 14px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
}
<p class="sticky">Contact form</p>
There are few issues with your markup and css.
document.getElementById("sticky")
But there isn't one. Either add ID there or get it from class using getElementByClassName
document.getElementById("sticky").style.top
changes in-line styling where as top: -1000px;
is defined from stylesheet.var prevScrollpos = window.pageYOffset;
var currentScrollPos = window.pageYOffset;
if (10 < currentScrollPos && 210 > currentScrollPos) {
document.getElementById("sticky").style.top = "0px";
} else {
document.getElementById("sticky").style.top = "-1000px";
}
window.onscroll = function() {
currentScrollPos = window.pageYOffset;
if (10 < currentScrollPos && 210 > currentScrollPos) {
document.getElementById("sticky").style.top = "000px";
} else {
document.getElementById("sticky").style.top = "-1000px";
}
prevScrollpos = currentScrollPos;
}
/* Test contact-sticky*/
.sticky {
position: fixed;
transition: top 0.3s;
background-color: #ebc11d;
padding: 50px;
font-size: 14px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
}
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<p class="sticky" id="sticky">Contact form</p>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments