Created
June 7, 2013 18:08
-
-
Save danielhaim1/5731203 to your computer and use it in GitHub Desktop.
Change opacity of element based on window scroll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
var fadeStart=1 // 100px scroll or less will equiv to 1 opacity | |
,fadeUntil=500 // 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); | |
}); | |
}); | |
/** example **/ | |
/*** | |
* | |
* <body style="height:9999px;"> | |
* <div id="fading" style="background:#aaa;height:500px;">Fading</div> | |
* </body> | |
* | |
* | |
* | |
* | |
***/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment