Jquery/Javascript Opacity animation with scroll
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);
});
JQuery Scroll Animation Causes Objects To Load Invisible Not Visible No Opacity
Class fade
applies opacity: 0;
, replace with other class name in HTML and in jQuery selector
opacity on scroll with jquery
Altered:
jsFiddle
$(document).ready(function() {
$(document).scroll(function(e){
opacidade();
});
var element = $('#element');
var elementHeight = element.outerHeight();
function opacidade(){
var opacityPercent = window.scrollY / $(document).height();
console.log(window.scrollY, opacityPercent);
element.css('opacity', opacityPercent);
}
});
- The scrollY is a pixel value, so unless you limit your possible scroll range [0 - 100], there's no reason to divide it by 100.
- So what you need is divide the scroll by the total document's height (or whatever it's parent that contains it and display a scrollbar)
Adjust CSS Opacity With Element's Scroll with jQuery
Depends on when you want it fully opaque or not, but this could be a start:
»»Fiddle«« (Multiple element class version - set individually)
»»Fiddle«« (Single element class version - if only one element per class)
function fader() {
var r = $('.red'), // The .red DIV, as variable so we do not have to look
// it up multiple times.
b = $('.blue'), // Same for blue.
wh = $(window).height(), // Height of window (visible part).
dt = $(document).scrollTop(), // Pixels document is scrolled down.
/* red offset top is a semi static values which say how many pixels it
* is from the top of the document.
* "Red Offset Top" - "Document Scroll Top" gives us how many pixels
* the red DIV is from top of visible window.
* "Window Height" - that value gives us pixels the red DIV is from top
* normalized to start from 0 when top of DIV is at bottom of window.
* */
redView = wh - (r.offset().top - dt),
// Same for blue DIV
blueView = wh - (b.offset().top - dt),
// Variable to save opacity value.
op;
/* If redView is bigger then 0 it means the DIV has top border above bottom
* of window.
*/
if (redView > 0) {
/* Opacity goes from 0 - 1 so we divide 1 by window height and
* multiplies it with pixels red DIV is from bottom.
* In addition we add the height of the red DIV to window height, such
* that we set opacity until it is out of view (Bottom border is at top
* of window, and not only until it has top border at top of window.)
*/
op = 1 / (wh + r.height()) * redView;
/* If value of calulation is less or equal to one, it is in visible
* view and we set the opacity accordingly.
*/
if (op <= 1)
r.css('opacity', op);
}
if (blueView > 0) {
op = 1 - 1 / (wh + b.height()) * blueView;
if (op >= 0)
b.css('opacity', op);
}
// Add this line for a possible help in understanding:
console.log(
'Window Height:', wh,
'Doc Scroll Top', dt,
'Red offset top:', r.offset().top,
'Red offs.top - Doc Scroll Top', r.offset().top - dt,
'View:', wh - (r.offset().top - dt)
);
}
// Attach scroll event to the function fader()
$(document).bind('scroll', fader);
OK. Added some comments. Might not feel it is the best explanation. A better way to understand it is perhaps to have a look at the values so I also added a console.log()
line inside the fader()
function. Open your console and view the values as you scroll. Fiddle with logging. Also note this performance difference. style
is considerably faster.
Version two:
This set full opacity when element has top at top of window, (not bottom of element). Note that we could use the Math.min()
in the above function as well, to omit the op
variable and if (op <= 1)
and if (op >= 0)
statement, but not at least a quick benchmark on jsperf revealed the if
version to perform slightly better. If you have many elements that you style one should use the if
version.
»»Fiddle««
function fader() {
var r = $('.red'),
b = $('.blue'),
wh = $(window).height(),
dt = $(document).scrollTop(),
redView = wh - (r.offset().top - dt),
blueView = wh - (b.offset().top - dt);
if (redView > 0) {
// Math.min() returns the lowest of given values. Here we do not want
// values above 1.
$('.red').css('opacity', Math.min(1 / wh * redView, 1));
}
if (blueView > 0) {
$('.blue').css('opacity', 1 - Math.min(1 / wh * blueView, 1));
}
}
// Event on scroll
$(document).bind('scroll', fader);
JQuery/Javascript Fade In On Scroll Not Working
The following code works:
$(function() {
$(window).scroll( function(){
$('.fadeInBlock').each( function(i){
var bottom_of_object = $(this).position().top + $(this).outerHeight();
var bottom_of_window = $(window).scrollTop() + $(window).height();
/* Adjust the "200" to either have a delay or that the content starts fading a bit before you reach it */
bottom_of_window = bottom_of_window + 200;
if( bottom_of_window > bottom_of_object ){
$(this).animate({'opacity':'1'},1000);
}
});
});
});
YOU DID NOT INCLUDE JQUERY SO USE
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
And finally uour HTML:
I included an image from an https source as it was throwing an insecure origin error in jsfiddle.
<div id="slide1">
<div class="content">
<h2>This is a title</h2>
<div class="quotes_container">
<h3 class="quotes">“Below this content, an image is supposed to fade in.” </h3>
<div class="fadeInBlock"><img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150"></div>
</div>
</div>
</div>
This works . Tested it on JS Fiddle.
Related Topics
How to Open a File Browser with Default Directory in JavaScript
Html5 Drag & Drop Change Icon/Cursor While Dragging
Get All Computed Style of an Element
Jquery - How to Disable the Entire Page
Arrange Multiple Divs in CSS/Js
Installing a Local Module Using Npm
Changing Iframe Src with JavaScript
What Is the Fastest or Most Elegant Way to Compute a Set Difference Using JavaScript Arrays
Babel-Loader Jsx Syntaxerror: Unexpected Token
Addeventlistener with Ajax Not Working Well
How to Append Timestamp to the JavaScript File in <Script> Tag Url to Avoid Caching
Ios8 Safari After a Pushstate the :Nth-Child() Selectors Not Works
Get Element Stylesheet Style in JavaScript
Using JavaScript to Mark a Link as Visited
Changing Pseudo-Element Style from JavaScript