Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
152
rated 0 times [  158] [ 6]  / answers: 1 / hits: 25077  / 10 Years ago, fri, october 31, 2014, 12:00:00

How is an HTML element animated as soon as it appears on screen?



I found an example on the stack overflow tour page, where info elements slide into the page as soon as you scroll down far enough. What is the trick behind that?


More From » jquery

 Answers
0

You need to use JavaScript to detect the location of the viewport and activate it when it becomes visible.


You can use JavaScript to detect and execute the transition and then CSS or JavaScript to do the animations.


There are many jquery based scripts available to accomplish this. Here is one example:




DEMO




1. Create a Html element you want to check if it's in the viewport.


<div class="demo"></div>

2. Load the jQuery javascript library and jQuery Viewport Checker plugin at the end of your document.


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="viewportchecker.js"></script>

3. Call the plugin on this element and do something in the javascript.


<script>
$(document).ready(function(){
$('.demo').viewportChecker({
// Class to add to the elements when they are visible
classToAdd: 'visible',

// The offset of the elements (let them appear earlier or later)
offset: 100,

// Add the possibility to remove the class if the elements are not visible
repeat: false,

// Callback to do after a class was added to an element. Action will return "add" or "remove", depending if the class was added or removed
callbackFunction: function(elem, action){}
});
});
</script>



DOWNLOAD THIS SCRIPT


[#68956] Tuesday, October 28, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
robertjaysona

Total Points: 276
Total Questions: 110
Total Answers: 116

Location: Finland
Member since Sat, Nov 6, 2021
3 Years ago
;