1

Why is this not working using body? Before it was using window, however i would like this to use body. But when i add this to the code it doesn't work.

here is my fiddle

and here is my code;

 $(document).ready(function () {
        $('.timeline li .dot:first').addClass("blur");
        $('.timeline li .date:first').addClass("blur2");

    });
    var $window = $('body');

    function isScrolledIntoView($elem, $window) {
        var docViewTop = $window.scrollTop();
        var docViewBottom = docViewTop + $window.height();

        var elemTop = $elem.offset().top;
        var elemBottom = elemTop + $elem.height();

        return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
    }
    var elements = $('.timeline li .dot');
    $('body').on('scroll', function () {
        elements.each(function () {
            $this = $(this);
            if (isScrolledIntoView($this, $window)) {
                $this.addClass("blur");
            }
            else {
                $this.removeClass("blur");
            }
        })
    });

    var elements2 = $('.timeline li .date');
    $('body').on('scroll', function () {
        elements2.each(function () {
            $this = $(this);
            if (isScrolledIntoView($this, $window)) {
                $this.addClass("blur2");
            }
            else {
                $this.removeClass("blur2");
            }
        })
    });

it should make the circles bigger as you scroll but currently its not doing it?

edit: previous fiddle, but this does not work in my code;

5
  • 5
    Because the body doesn't scroll. The whole document scrolls. Commented Jun 22, 2015 at 15:28
  • but it is possible to just make a div scroll? Commented Jun 22, 2015 at 15:34
  • Sure, if it has a scrollbar of it's own. Commented Jun 22, 2015 at 15:35
  • Duplicate A, Duplicate B Commented Jun 22, 2015 at 15:35
  • @FelixKling... if that is the case, then why is the document's scrolling element set to body during its handler? Commented Jul 11, 2017 at 10:33

1 Answer 1

2

I know the question is old and I'm not sure if it's still relevant but replace

$('body').on('scroll', function () {

with

$(window).scroll( function() {

The scroll event doesn't apply to body but to window.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.