8

I'm looking to change the opacity on an object (and have the transition be animated) based on a users scroll. example(http://davegamache.com/)

I've searched everywhere like here, but it ends up pointing me to the waypoints plugin (http://stackoverflow.com/questions/6316757/opacity-based-on-scroll-position)

I've implemented the [waypoints][1] plugin and have the object fading once it's higher than 100px. [Using the offet attribute] but would like to basically control the opacity of an object and have the animation be visible like the above example.

I've searched all over- this is my last resort. Any help is greatly appreciated.

1
  • just add a starting and ending point to my answer will certainly be much more what you want Commented Jan 2, 2012 at 15:32

6 Answers 6

27

working exemple with starting and ending point here: http://jsfiddle.net/z7E9u/1/

I copy paste basic code here

    var fadeStart=100 // 100px scroll or less will equiv to 1 opacity
    ,fadeUntil=200 // 200px scroll or more will equiv to 0 opacity
    ,fading = $('#fading')
;

$(window).bind('scroll', function(){
    var offset = $(document).scrollTop()
        ,opacity=0
    ;
    if( offset<=fadeStart ){
        opacity=1;
    }else if( offset<=fadeUntil ){
        opacity=1-offset/fadeUntil;
    }
    fading.css('opacity',opacity).html(opacity);
});
Sign up to request clarification or add additional context in comments.

1 Comment

What would be the reverse to this? I mean to change opacity from 0 to 1.
15

Here's a working example: http://jsfiddle.net/meEf4/

And the code:

var target = $('div');
var targetHeight = target.outerHeight();

$(document).scroll(function(e){
    var scrollPercent = (targetHeight - window.scrollY) / targetHeight;
    if(scrollPercent >= 0){
        target.css('opacity', scrollPercent);
    }
});

All we do is grab the current scroll position of the window, figure out what percentage of the element in question is now off-screen, and set its opacity with that percentage.

2 Comments

This is great. I appreciate your quick response.
Brilliant solution! :)
2

As I have lower than 50 reputation I cannot reply to Lonut's question, how to do the reverse. Here is my code if you would like the reverse, quite handy for navigation bars.

$(window).scroll(function () {
        var offset = $(document).scrollTop()
        var opacity = 0;
        if (offset <= 0) {
            opacity = 0;
        } else if (offset > 0 & offset <= 200) {
            opacity = (offset - 1) / 200;
        }
        else {
            opacity = 1;
        }
        $('.black-background').css('opacity', opacity).html(opacity);
    });

Comments

0

I looked at the source code of that site. it uses: $(document).scrollTop(); to determine the scroll height, and $(window).scroll(function(){}) to bind an event listener to scrolling.

try this:

$(window).scroll(function(){
    var fromtop = $(document).scrollTop();       // pixels from top of screen
    $('#fademeout').css({opacity: 100-fromtop}); // use a better formula for better fading
});

Comments

0

I know I am a bit late to the party, but here's my approach:

$(window).scroll(function(){
  var st = $(window).scrollTop();
  var range = 300 // finetune this to the desired effect
  $('.yourelement').css("opacity", 1- st / range); // animate your element
});

Comments

-1

I like this solution

var fadeStart=100 // 100px scroll or less will equiv to 1 opacity
   ,fadeUntil=200 // 200px scroll or more will equiv to 0 opacity
   ,fading = $('#fading')
;

$(window).bind('scroll', function(){
    var offset = $(document).scrollTop()
        ,opacity=0
    ;
    if( offset<=fadeStart ){
        opacity=1;
    }else if( offset<=fadeUntil ){
        opacity=1-offset/fadeUntil;
    }
    fading.css('opacity',opacity).html(opacity);
});

How could you use the mouse scrolling for the fading ONLY until eg 0.2 opacity is reached and then scroll the page too? The solutions i found so far disable the mouse scrolling function completely

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.