To begin, you must first identify when the user is scrolling upwards. This can be achieved by implementing the following code:
// stores the last scroll value
var lastScrollTop = 0;
// detects the onscroll event
$(window).scroll(function(event) {
// assigns the current scroll value to a variable
var scroll = $(this).scrollTop();
// checks if the current scroll is less than the last scroll
// if true, the user is scrolling up
if(scroll < lastScrollTop) {
// perform desired action for scrolling up here...
}
// updates the last scroll value
lastScrollTop = scroll;
});
Next, you need to remove the CSS of the element. This can be done by removing a specific class, like so:
// selects the element and removes the CSS class
$("div").removeClass("target-classname");
Alternatively, you can directly change the CSS properties of the element using jQuery:
// updating multiple values at once
$("div").css({
"property": "value"
});
// updating a single value
$("div").css("property", "value");
Combining these steps, your code would look like this:
// stores the last scroll value
var lastScrollTop = 0;
// detects the onscroll event
$(window).scroll(function(event) {
// assigns the current scroll value to a variable
var scroll = $(this).scrollTop();
// checks if the current scroll is less than the last scroll
// if true, change the CSS of the div
$("div").css("property", "value");
// updates the last scroll value
lastScrollTop = scroll;
});
Code without comments:
var lastScrollTop = 0;
$(window).scroll(function(event) {
var scroll = $(this).scrollTop();
if(scroll < lastScrollTop) {
$("div").css("property", "value");
}
lastScrollTop = scroll;
});
You could also create a jQuery plug-in for the scroll up/down functions like this:
$.fn.scrollUp = function(callback) {
var self = $(this);
var lastScrollTop = 0;
self.scroll(function(event) {
var scroll = self.scrollTop();
if (scroll < lastScrollTop) {
callback.apply(self, [scroll]);
}
lastScrollTop = scroll;
});
};
$.fn.scrollDown = function(callback) {
var self = $(this);
var lastScrollTop = 0;
self.scroll(function(event) {
var scroll = self.scrollTop();
if (scroll > lastScrollTop) {
callback.apply(self, [scroll]);
}
lastScrollTop = scroll;
});
};
Usage example:
$(window).scrollUp(function(scroll) {
console.log(scroll);
$(this).find("body").html("You are scrolling up<br />" + "Scroll: " + scroll + "<br />");
});
$(window).scrollDown(function(scroll) {
console.log(scroll);
$(this).find("body").html("You are scrolling up<br />" + "Scroll: " + scroll + "<br />");
});
Using the jQuery plug-in for scrolling up event:
$(window).scrollUp(function(scroll) {
$("div").css("property", "value");
});
Code without comments:
$(window).scrollUp(function(scroll) {
$("div").css("property", "value");
});
Best of luck with your implementation!