Google AMP fad-in Animation
In AMP HTML pages adding visual effect is easy using the amp-fx-collection component which provides a range of collections of effects such as fade-in which a background image opacity keeps on increasing till is visible .
Required Scripts: Importing the amp-fx-collection into the header.
HTML
< script async custom-element = "amp-fx-collection" src = "https://cdn.ampproject.org/v0/amp-fx-collection-0.1.js" > </ script > |
Attributes:
- data-duration: Specifies the duration of the animation it becomes static after the elapsed time.
- data-easing: Varies the animation speed through out the entire duration.
- data-margin-start/end: Specifies at what % of margin of the view port should the animation start.
- data-repeat: To repeat animation even after fully loaded after scroll.
- fade-in-scroll: To change opacity of the image when it scrolls within the view port.
Example:
HTML
<!doctype html> < html amp> < head > < meta charset = "utf-8" > < title >Google AMP fad-in Animation</ title > < script async src = "https://cdn.ampproject.org/v0.js" > </ script > <!-- Import `amp-fx-collection` extension --> < script async custom-element = "amp-fx-collection" src = "https://cdn.ampproject.org/v0/amp-fx-collection-0.1.js" > </ script > < link rel = "canonical" href = "https://amp.dev/documentation/examples/components/amp-fx-collection/index.html" > < meta name = "viewport" content = "width=device-width,minimum-scale=1,initial-scale=1" > < style amp-boilerplate> body { -webkit-animation: -amp-start 8s steps(1, end) 0s 1 normal both; -moz-animation: -amp-start 8s steps(1, end) 0s 1 normal both; -ms-animation: -amp-start 8s steps(1, end) 0s 1 normal both; animation: -amp-start 8s steps(1, end) 0s 1 normal both } @-webkit-keyframes -amp-start { from { visibility: hidden } to { visibility: visible } } @-moz-keyframes -amp-start { from { visibility: hidden } to { visibility: visible } } @-ms-keyframes -amp-start { from { visibility: hidden } to { visibility: visible } } @-o-keyframes -amp-start { from { visibility: hidden } to { visibility: visible } } @keyframes -amp-start { from { visibility: hidden } to { visibility: visible } } </ style > < noscript > < style amp-boilerplate> body { -webkit-animation: none; -moz-animation: none; -ms-animation: none; animation: none } </ style > </ noscript > < style amp-custom> .header { position: relative; overflow: hidden; } .header h1 { color: white; bottom: 10%; left: 10px; position: absolute; z-index: 1; font-size: 1.7em; } .title { background-color: black; color: white; } .parallax-image-window { overflow: hidden; } .parallax-image-window amp-img { margin-bottom: -20%; } </ style > </ head > < body > <!--fade-in with a duration--> < amp-img amp-fx = "fade-in" data-duration = "2000ms" width = "1280" height = "873" layout = "responsive" src = "https://media.w3wiki.net/wp-content/uploads/20201016212652/outputonlinepngtools.png" > </ amp-img > <!-- animation starts past 50% of the viewport--> < amp-img amp-fx = "fade-in" data-margin-start = "50%" width = "1280" height = "873" layout = "responsive" src = "https://contribute.w3wiki.net/wp-content/uploads/xrgwkj7i-Edge-Computing.jpg" > </ amp-img > <!-- Scroll dependent fade-in --> < amp-img amp-fx = "fade-in-scroll" width = "1600" height = "900" layout = "responsive" src = "https://media.w3wiki.net/wp-content/uploads/20200825214742/Top7CodeSharingWebsiteForDevelopers.png" > </ amp-img > <!-- Custom start/end points --> < amp-img amp-fx = "fade-in-scroll" data-margin-start = "20%" data-margin-end = "80%" width = "1600" height = "900" layout = "responsive" src = "https://contribute.w3wiki.net/wp-content/uploads/WorkingWithTextData-min.png" > </ amp-img > </ body > </ html > |
Output: