JS Smooth scrolling, scroll to bottom of final / bottom div instead of top

sam

I've got a link that scrolls to areas of the same page. So the page doesn't jump around, I've added some JavaScript smooth scrolling (see below).

The issue is, the script scrolls the page to the top of the target div. It does this with some easing, so it slows down as it gets to the final div. This is fine, but if the last div is less than the height of the browser window, it will never be able to scroll to the top of the div. It just hits the bottom of the page and its looks broken as there is no easing.

I've tried several ways to make the final div scroll to the bottom of the div, but to no avail. Any ideas how to do this?

I've made a jsfiddle of the issue here: https://jsfiddle.net/gky7y6gu/2/

$(function() {
  $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});
Fraser Crosbie

I added some logic to make sure the scrollTop never exceeds the height of the body minus the height of the window. This way the easeOut is always visible.

https://jsfiddle.net/gky7y6gu/3/

$(function() {
  $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
      var target = $(this.hash);
      if (target.length) {
        var scrollTo = target.offset().top;
        var bodyHeight = $('body').height();
        var windowHeight = $(window).height();
        if (bodyHeight - windowHeight < scrollTo) {
          scrollTo = bodyHeight - windowHeight;
        }
        $('html,body').animate({
          scrollTop: scrollTo
        }, 1000);
        return false;
      }
    }
  });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JS for smooth scroll to the bottom of the page

From Dev

JS for smooth scroll to the bottom of the page

From Dev

Why is Infinite scrolling not working when I scroll bottom, instead it triggers on scroll top?

From Dev

Scroll top, then bottom, then top, then bottom

From Dev

Scroll in recyclerview scrolls to bottom of view instead of top

From Dev

scroll top when clicking on a div, then scroll bottom if scroll is on top

From Dev

Links jump to top and bottom but smooth scrolling doesn't work

From Dev

Div, pinned to bottom that pins to top on scroll

From Dev

div + scroll + fixed bottom + variable top

From Dev

jQuery - Smooth scroll only working to top - need it to work to bottom aswell

From Dev

jQuery - Smooth scroll only working to top - need it to work to bottom aswell

From Dev

Div get bottom position instead of top

From Dev

Make DIV float to the bottom instead on top

From Dev

Programmatically scrolling datagridview by increments until it hits bottom, then scroll back to top

From Dev

how to scroll top and bottom

From Dev

Scroll to Top and Scroll to Bottom in Jquery

From Dev

Scroll to Top and Scroll to Bottom in Jquery

From Dev

Javascript scroll to the top and scroll to the bottom

From Dev

Div upon div scrolling while reach to bottom or top

From Dev

Div upon div scrolling while reach to bottom or top

From Dev

Js div scroll to bottom in React component

From Dev

div overflow scroll: when scrolled to bottom, content starts scrolling

From Dev

JavaScript - Scroll to bottom of Div - Keeps on scrolling down without stop

From Dev

How to make a div stick to top or bottom, depending on how the user is scrolling?

From Dev

How to stick the bottom part of a div to top of the screen when scrolling down?

From Java

Scroll to bottom of div?

From Dev

how to scroll slowly from top to bottom and bottom to top when click on same div?

From Dev

Align on top and on bottom in the DIV

From Dev

Fix div at top and bottom

Related Related

  1. 1

    JS for smooth scroll to the bottom of the page

  2. 2

    JS for smooth scroll to the bottom of the page

  3. 3

    Why is Infinite scrolling not working when I scroll bottom, instead it triggers on scroll top?

  4. 4

    Scroll top, then bottom, then top, then bottom

  5. 5

    Scroll in recyclerview scrolls to bottom of view instead of top

  6. 6

    scroll top when clicking on a div, then scroll bottom if scroll is on top

  7. 7

    Links jump to top and bottom but smooth scrolling doesn't work

  8. 8

    Div, pinned to bottom that pins to top on scroll

  9. 9

    div + scroll + fixed bottom + variable top

  10. 10

    jQuery - Smooth scroll only working to top - need it to work to bottom aswell

  11. 11

    jQuery - Smooth scroll only working to top - need it to work to bottom aswell

  12. 12

    Div get bottom position instead of top

  13. 13

    Make DIV float to the bottom instead on top

  14. 14

    Programmatically scrolling datagridview by increments until it hits bottom, then scroll back to top

  15. 15

    how to scroll top and bottom

  16. 16

    Scroll to Top and Scroll to Bottom in Jquery

  17. 17

    Scroll to Top and Scroll to Bottom in Jquery

  18. 18

    Javascript scroll to the top and scroll to the bottom

  19. 19

    Div upon div scrolling while reach to bottom or top

  20. 20

    Div upon div scrolling while reach to bottom or top

  21. 21

    Js div scroll to bottom in React component

  22. 22

    div overflow scroll: when scrolled to bottom, content starts scrolling

  23. 23

    JavaScript - Scroll to bottom of Div - Keeps on scrolling down without stop

  24. 24

    How to make a div stick to top or bottom, depending on how the user is scrolling?

  25. 25

    How to stick the bottom part of a div to top of the screen when scrolling down?

  26. 26

    Scroll to bottom of div?

  27. 27

    how to scroll slowly from top to bottom and bottom to top when click on same div?

  28. 28

    Align on top and on bottom in the DIV

  29. 29

    Fix div at top and bottom

HotTag

Archive