Only show div when in the middle of the page - javascript

I'm hoping to fade in a div when the page has been scrolled away from the top of the page, however I want this div to be hidden again when approaching the bottom of the page.
To be specific, I want the div to be hidden when within 200px of the top or 200px of the bottom.
I have two scripts which work independently, but when both active, a conflict between the two causes the div to flash in and out of view.
How would I combine the following scripts to avoid this conflict? Any help would be most appreciated. Thanks!
$(function () {
$(window).scroll(function () {
if ($(this).scrollTop() > 200) {
$('#myDiv').fadeIn(500);
} else {
$('#myDiv').fadeOut(500);
}
});
});
and
$(function () {
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() > $(document).height() - 200) {
$('#myDiv').fadeIn(500);
} else {
$('#myDiv').fadeOut(500);
}
});
});

how's this:
$(function () {
var $window = $(window);
$window.scroll(function () {
var scrollTop = $(this).scrollTop();
// only fadeIn between 200 from top and 200 from bottom
if (scrollTop > 200 && scrollTop < $(document).height() - $window.height() - 200) {
$('#myDiv').fadeIn(500);
} else {
$('#myDiv').fadeOut(500);
}
});
});
Example fiddle

Related

Removing and adding class via .js

I'm working on a header banner that is hidden at the start but appears when the user scrolls down on the page. When the user scrolls back up to the top of the page it should disappear again and keep doing it until the user exits (there is an exit button on the banner which adds a cookie so if the user exits it won't show again).
The issue I'm having is that either the banner won't show up again when I scroll back up to the top of the page, or it will just keep showing up even after exiting. I've tried several options but nothing has worked so far.
function desktopHeader() {
$(window).on('scroll', function() {
console.log( $(this).scrollTop() );
});
var $headerBanner = $('.module-header-banner');
$('.close-btn').on("click", function () {
$.cookie("headerbanner", "exit", {expires: 2/24});
$('.module-header-banner').addClass("exit").fadeOut();
});
if($.cookie('headerbanner') == null) {
if($(window).scrollTop() > $('.site-header').height()){
$headerBanner.addClass('active').fadeIn();
}
$(window).scroll(function() {
if($(window).scrollTop() > $('.site-header').height()){
$headerBanner.addClass('active');
} else if($(window).scrollTop() < $('.site-header').height()) {
$headerBanner.removeClass('active');
}
});
}
}
At a loss -- if anyone has any advice would be best appreciated. Thanks!
Try to add your scroll event outside of the click function,
here is a updated code
function desktopHeader() {
$(window).on('scroll', function() {
console.log( $(this).scrollTop() );
});
var $headerBanner = $('.module-header-banner');
$('.close-btn').on("click", function () {
$.cookie("headerbanner", "exit", {expires: 2/24});
$('.module-header-banner').addClass("exit").fadeOut();
});
if($.cookie('headerbanner') == null) {
if($(window).scrollTop() > $('.site-header').height()){
$headerBanner.addClass('active').fadeIn();
}
}
$(window).scroll(function() {
if($(window).scrollTop() > $('.site-header').height()){
$headerBanner.addClass('active');
}
else if($(window).scrollTop() < $('.site-header').height()) {
$headerBanner.removeClass('active');
}
});
}

Combine jQuery scroll and load

I'm using the following code to detect if the page is scrolled beyond 150px.
The code works fine but I'd like to know if theres any way to combine the scroll and load functions as currently I'm repeating code.
Appreciate any help.
var nav = $(".header");
$(window).scroll(function () {
if ($(this).scrollTop() > 150) {
nav.addClass("header-bg");
} else {
nav.removeClass("header-bg");
}
});
$(window).load(function () {
if ($(this).scrollTop() > 150) {
nav.addClass("header-bg");
} else {
nav.removeClass("header-bg");
}
});
Nevermind I figured it out, for anyone else who gets stuck with:
var nav = $(".header");
$(window).on("load resize scroll",function(e){
if ($(this).scrollTop() > 150) {
nav.addClass("header-bg");
} else {
nav.removeClass("header-bg");
}
});

header sticks to top of page in only one div

So I have a piece of jquery that will stick a div to the top of the page once scrolled past, this works well !!
now im trying to set it so one the bottom on that div is reached it stops sticking to the head, I can seem to work this.
any help is appreciated.
function sticky_relocate() {
var window_top = $(window).scrollTop();
var div_top = $('#sticky-anchor').offset().top;
if (window_top > div_top) {
$('#sticky').addClass('stick');
}
if ($(window).scrollTop() > 1400) {
$('sticky-anchor').slideDown();
}
else {
$('#sticky').removeClass('stick');
}
}
$(function () {
$(window).scroll(sticky_relocate);
sticky_relocate();
});
Shouldn't
$('#sticky').removeClass('#stick');
be
$('#sticky').removeClass('stick');
??

Auto click when user scrolls to the end of the page

I'm trying to create an infinite scroll feature on my site but it isn't working.
My code:
var post = {}
post.load_moreBtn = $('#home_load_more');
if($(window).scrollTop() + $(window).height() == $(document).height()) {
post.load_moreBtn.trigger('click');
}
post.load_moreBtn.on('click', function () {
$(this).html('<img src="' + base_url + 'images/core/loader2.gif"/>');
post.load_more_messages($(this).attr('data-last_id'));
});
If I put an alert in place of the trigger it works,also if I remove the scroll detection bit, the load more works fine. Just can't get it to autoload, please help.
It's easy with jQuery:
$(function(){ //on document ready
$(document).scroll(function (e) { //bind scroll event
var intBottomMargin = 300; //Pixels from bottom when script should trigger
//if less than intBottomMargin px from bottom
if ($(window).scrollTop() >= $(document).height() - $(window).height() - intBottomMargin) {
$("#home_load_more").click(); //trigger click
}
});
});
I bind this in the

How to make a jquery function run when a div is displayed on the screen

In Short:
I am working on a project and I want to run a function when a particular div is displayed on the screen.
Detail:
There is a scroll to top button fixed on the bottom right of the screen in my template. I want to hide that button when someone scroll down to the footer.
Means when someone scroll down to the footer and the top border of the footer is displayed on the screen, I want a function to run which would hide the go to top button.
Please help me out of this problem...
$(document).ready(function() {
var footer = $("footer");
var f_top = footer.position().top;
$(window).scroll(function() {
if ($(window).scrollTop() + $(window).height() >= f_top ) {
footer.hide();
}
else{
footer.show();
}
});
});
Initialize the window to monitoring its scrolling
$(document).ready(function () {
$(window).scroll(function () {
// get the element that you want check scrolling on it
var off = $("your-selector").offset().top;
var top = $(window).scrollTop() + $(window).height();
if (off <= top) {
// do your job
// for example you can call a function like:
my_method_to_invoke();
}
});
});
The function you want to invoke it:
function my_method_to_invoke() {
// TODO
}
I think you'll need to register a scroll listener on the body that checks to see if the footer is in view and perform the hide if so. Something like this...
$(body).scroll(function () {
scrollCheck();
});
var scrollCheck = function () {
var docTop, docBot, elemTop, elemBot;
docTop = $(window).scrollTop;
docBot = docTop + $(window).height();
elemTop = $(<footer element>).offset().top;
elemBot = elemTop + $(<footer element>).height();
if ((elemBottom >= docTop) && (elemTop <= docBot)) {
$(<button element).hide();
}
}

Categories