I have a variable called yPos
increasing and decreasing based on the value of $(window).scrollTop()
on scroll. I would like to set a maximum and minimum value so that the yPos
variable cannot be any lower than the min value and cannot be bigger than the max value, but still change depending on scrollTop
. How should I go about doing this?
Asked
Active
Viewed 1,129 times
1

sadmansh
- 917
- 2
- 9
- 21
-
1what have you tried ? where is the code that changes yPos ? – Sajad Karuthedath Dec 09 '19 at 08:25
-
Does [this link](https://stackoverflow.com/questions/11409895/whats-the-most-elegant-way-to-cap-a-number-to-a-segment) answers your question? – Kaiido Dec 09 '19 at 08:26
-
Do you want to block the user to a certain scroll area? – besciualex Dec 09 '19 at 08:30
3 Answers
3
const max = yourMax;
const min = yourMin;
let yPos = $(window).scrollTop();
if(yPos > max) {
yPos = max;
}
else if(yPos < min) {
yPos = min;
}

Addis
- 2,480
- 2
- 13
- 21
1
Use something like this.
var yPos= 0;
var min_value = 10;
var max_value = 100;
if($(window).scrollTop() >= min_value && $(window).scrollTop()<= max_value)
{
yPos = $(window).scrollTop();
}
note: this just a logic, please don't copy paste this to your code.

Sajad Karuthedath
- 14,987
- 4
- 32
- 49
-
why it is not working ? this is just a logic only how to do it and not the complete answer with fiddle or something like that. – Sajad Karuthedath Dec 09 '19 at 08:35
1
$(document).ready(function(){
var scroll = 0;;
var maxvalue = 100;
var minvalue = 10;
var temp = 0;
$(document).scroll(function(){
temp = $(window).scrollTop();
if(temp > maxvalue)
{
temp = maxvalue;
}
if(temp < minvalue){
temp = minvalue;
}
scroll = temp;
console.log(scroll);
});
});
body{
height:200vh;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Ahmed Ali
- 1,908
- 14
- 28