最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Addremove class when close to bottom of page - Stack Overflow

programmeradmin1浏览0评论

I have the below jquery which is adding or removing a class when the user hits the bottom of the page. I'd like to adjust this code to implement the change when the user is close to the bottom, or perhaps when the bottom es into viewport?

Any help greatly appreciated!

JS

// Add/remove class to/from .logo upon reaching bottom of page
$(window).scroll(function() {
   $(".logo").removeClass("viewport-bottom");
   if($(window).scrollTop() + $(window).height() === $(document).height()) {
       //you are at bottom
       $(".logo").addClass("viewport-bottom");
   }
});

I have the below jquery which is adding or removing a class when the user hits the bottom of the page. I'd like to adjust this code to implement the change when the user is close to the bottom, or perhaps when the bottom es into viewport?

Any help greatly appreciated!

JS

// Add/remove class to/from .logo upon reaching bottom of page
$(window).scroll(function() {
   $(".logo").removeClass("viewport-bottom");
   if($(window).scrollTop() + $(window).height() === $(document).height()) {
       //you are at bottom
       $(".logo").addClass("viewport-bottom");
   }
});
Share Improve this question asked Feb 26, 2016 at 16:42 dungey_140dungey_140 2,8029 gold badges42 silver badges75 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8
$(window).scroll(function() {
   $(".logo").removeClass("viewport-bottom");
   if($(window).scrollTop() + $(window).height() > ($(document).height() - 100) ) {
       //you are at bottom
       $(".logo").addClass("viewport-bottom");
   }
});

Change the subtracted value to suit your needs. Using === would only work when the scroll bar was at that exact pixel.

couldn't you just subtract the number to verify when to add or subtract for instance, wouldn't the below say if it's within 50 pixels of the end, hide and show,

// Add/remove class to/from .logo upon reaching bottom of page
$(window).scroll(function() {
   $(".logo").removeClass("viewport-bottom");
   if($(window).scrollTop() + $(window).height() === $(document).height() - 50) {
       //you are at bottom
       $(".logo").addClass("viewport-bottom");
   }
});
发布评论

评论列表(0)

  1. 暂无评论