Design trends are constantly changing and something we are using in a lot of recent projects is animations triggered when the visitor scrolls to a certain point on the page. Here is a quick guide on how to detect if an element is scrolled into view and implement a CSS animation that is triggered by a jQuery scroll event. Show See the code in action here: http://jsfiddle.net/vep4sryp/1/ The HTML<div class="container"> <div class="inner box1"> </div>
<div class="inner box2"> </div>
</div>The jQuery Scroll Detection$(window).scroll(function(){ // This is then function used to detect if the element is scrolled into view function elementScrolled(elem) { }
// This is where we use the function to detect if ".box2" is scrolled into view, and when it is add the class ".animated" to the <p> child element
if(elementScrolled('.box2')) { }
});
&91;/code&93;<h2>The CSS</h2> [code] .container { width: 100%; } .inner { width: 980px; margin: 0 auto; height: 500px; } .inner.box2 p { visibility: hidden; } .inner.box2 p.animated { -webkit-animation-name: fadeInUp; animation-name: fadeInUp; -webkit-animation-duration: 1.5s; animation-duration: 1.5s; -webkit-animation-fill-mode: both; animation-fill-mode: both; -webkit-animation-delay: 0.5s; animation-delay: 0.5s; } CSS Animation@-webkit-keyframes fadeInUp { 0% { }
100% { }
}
@keyframes fadeInUp {
0% { }
100% { }
}This is just a basic example, and can be adapted to suit your needs. If you need something a bit more complex jQuery Waypoints another good solution to detect elements in the viewport. We have also shown a basic CSS animation example, a great tool for CSS animations can be seen here. If you found this tutorial helpful, or need some help implementing it into your website let us know in the comments. The scrollTop() method in jQuery is used to set or return the vertical scrollbar position for the selected elements. With the help of this method, we can find the mouse scroll direction. Syntax: $(selector).scrollTop(position) Parameters: This method accepts single parameter position which is optional. It is used to specify the vertical scrollbar position in pixels. Return Value: It returns the vertical position of the scrollbar of selected element. Example: The An element's
When
4 element), the
5 of the window is returned. . Warning: On systems using display scaling, A number. In this example, try scrolling the inner container with the dashed border, and see how the value of How to check if element is scrolled to top in JavaScript?You can use the offset() method to obtain where an element is relative to the document, and you can use the scrollTop() method to figure out where the top of the document is. If the element offset is at 1000 and scrollTop is more than 1000, you know that you've scrolled the element above off the screen. How do you scrollTo the top in jQuery?In jQuery, the scrollTo() method is used to set or return the vertical scrollbar position for a selected element. This behavior can be used to scroll to the top of the page by applying this method on the window property. Setting the position parameter to 0 scrolls the page to the top. How do you find the scroll position of an element?To get or set the scroll position of an element, you follow these steps:. First, select the element using the selecting methods such as querySelector() .. Second, access the scroll position of the element via the scrollLeft and scrollTop properties.. How do you know if an element is scrollable?Using scrollTop and scrollLeft properties Select the particular element. Use the scrollTop and scrollLeft properties. If these are greater than 0, scrollbars are present. If these are 0, then first set them to 1, and test again to know if getting a result of 1. |