0

Trying to change body background position with scroll position. the background image have these settings:

 background: #fff url(images/bg.png) center 100% no-repeat; background-attachment:fixed;    

I have footer at that page with the height of 120px. I'm trying to change body background position (y axis) when the scroll reached to the footer with mouse wheel or scrollbars.

Edit:

I've manage to make it work both for mouse wheel and bars, in case someone needs to make a footer outside the "fixed" background.

$(window).scroll(function() {
        var def_bg_pos = 300;
        var footer = $(document).height() - 120; // footer height

        if($(window).scrollTop() + $(window).height() > footer) {
            var counter = ($(window).scrollTop() + $(window).height()) - footer;
            $('body').css('background-position', 'center '+ ((counter * -1) - def_bg_pos) + 'px');
        }
        else
            $('body').css('background-position', 'center -'+def_bg_pos+'px');
    });

<body style="background: #fff url(images/bg.png) center -300px no-repeat; background-attachment:fixed;">
CroMagnon
  • 1,218
  • 7
  • 20
  • 32
Y K
  • 81
  • 1
  • 7

0 Answers0