Posted in js
6
6:20 am, August 31, 2018

detect window scroll position jquery

This one adds or removes a class if the document is scrolled 50 px near the top of the window.

$(window).scroll(function() {
  if ($(document).scrollTop() > 50) {
    $('nav').addClass('shrink');
  } else {
    $('nav').removeClass('shrink');
  }
});

We can monitor the scroll position with the following code, this will console log each scroll position counting from the top into console log.

$(window).scroll(function() {
  console.log($(document).scrollTop());
});

lets say that we wanted the background of the page become pink (as you do) when we scroll down 100 px

something like this should do it

script

$(window).scroll(function() {
  if ($(document).scrollTop() > 100) {
    $('h1').addClass('pink');
  } else {
    $('h1').removeClass('pink');
  }
});

css

.pink { color:#f3b4f0; }

scroll down to see the h1 and h3 color change, then back to the top to go back to original.

$(window).scroll(function() {
  if ($(document).scrollTop() > 100) {
    $('h1').addClass('pink');
  } else {
    $('h1').removeClass('pink');
  }
});

you can also add the scroll top position into a div, if you cant be bothered looking at the console window.

Scroll Position: 0

$(window).scroll(function() {
  $('#scrollpos').html($(document).scrollTop());
});

lets stick that to the bottom of the window, just so we can see where it at.

.scroll-pos { 
	font-size:16px;
	background:rgba(0,0,0,0.9);
	color:#FFF;
	text-shadow:0px 1px 2px #000;
	position:fixed;
	bottom:0;
	right:0;
	padding:5px 10px;
}

With this code you can add it to a navigation to make it stick on scroll at a certain position. I might try that next.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Mmm.. padding

Javascript

$(window).scroll(function() {
  console.log($(document).scrollTop());
});
$(window).scroll(function() {
  if ($(document).scrollTop() > 100) {
    $('h1').addClass('pink');
  } else {
    $('h1').removeClass('pink');
  }
});
$(window).scroll(function() {
  $('#scrollpos').html($(document).scrollTop());
});

View Statistics
This Week
81
This Month
331
This Year
0

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Items in js
fix for Uncaught (in promise) Error: reCAPTCHA placeholder element must be empty enable tinymce on a target textarea by id Generate Random Whole Numbers with JavaScript Function Generate Random Fractions with JavaScript jquery document ready make clickable element with clickable class using getElementById and innerHTML to change the html of content no jquery preserve tabs in textarea when tab key is pressed jqueryui includes css and js using vue and json data jqueryui date selector jquery accordion fancybox youtube showing video links in a lightbox jquery jqueryui vue script includes get select option form value with jquery show the year with js jquery clone and append elements stacktable jQuery plugin for stacking tables on small screens load content with jquery find all elements add class jquery digital clock with jquery using regex with replace to replace all instances of something in a string random string generator guid set and check a cookie using js cookie validate email address from string check length of element jquery access hacker news json firebase api via jquery testing chartjs clipboard copy js tiny mce editor tinymce do something later with settimeout or loop with setinterval validate form data using javascript to check required html elements mithril testing parallax js scroll testing change the water colour in google maps for an already initialised map scrollbar replacement simplebar load google sheet data into json string with jquery change favicon with jquery add this Slick Slider Carousel change the window title flems embed in url detect window scroll position jquery truncate string using jquery round number with js google map with overlay data
Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

Join me on Substack if you want me to send you a collection of the things i have done or found or read for the week. Or follow me on twitter if you prefer, i dont post much but i probably should!

❤👩‍💻🕹

Random Quote
For a long time it had seemed to me that life was about to begin - real life. But there was always some obstacle in the way, something to be gotten through first, some unfinished business, time still to be served, or a debt to be paid. Then life would begin. At last it dawned on me that these obstacles were my life.
Alfred D. Souza