<p>Scroll down this page</p>
<p id="myP" class="test">When you have scrolled 50 pixels from the top of this page, add the class "test" (yellow background color) to this paragraph. Scroll up again to remove the class.
</p>
<div class="bg">
</div>
<!--div id="demo" class="demo">hgjhgjgh</div-->
<div id="scrollinfo" class="demo"></div>
<button onclick="myFunction()">Click me</button>
window.onscroll = function() {
scrollFunction()
};
function scrollFunction() {
var zwei = document.documentElement.scrollTop;
document.getElementById("demo").innerHTML = zwei;
if (document.documentElement.scrollTop < 250) {
document.getElementById("myP").classList.add("show", "test");}
}
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
function myFunction() {
//var eins = document.body.scrollTop;
var zwei = document.documentElement.scrollTop;
//alert("body.scrollTop ist = " + eins + " documentElement.scrollTop ist = " + zwei);
alert("documentElement.scrollTop ist = " + zwei);
}
var zwei = document.documentElement.scrollTop;
var position = $(window).scrollTop();
// should start at 0
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if(scroll > position && document.documentElement.scrollTop > 250) {
document.getElementById("myP").classList.add("hide");
$('#scrollinfo').text('Scrolling Down Scripts');
} else if (scroll < position && document.documentElement.scrollTop < 250) {
document.getElementById("myP").classList.add("show", "test");
$('#scrollinfo').text('Scrolling Up Scripts');
}
position = scroll;
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.