Google AMP – List ”; Previous Next Amp-list is an amp-component which calls a CORS json endpoint and displays the data in the form of a json file inside a template. Let us understand this with the help of working examples. To work with amp-list, we need to include following script − <script async custom-element = “amp-list” src = “https://cdn.ampproject.org/v0/amp-list-0.1.js”> </script> This is the format of amp-list tag − <amp-list width = “auto” height = “100” layout = “fixed-height” src = “amplist.json” class = “m1”> <template type = “amp-mustache”> <div class = “images_for_display”> <amp-img width = “150” height = “100” alt = “{{title}}” src = “{{url}}”> </amp-img> </div> </template> </amp-list> The src used for amp-list is a json file which has the details to be listed. We can use normal html tags or amp-components inside amp-list to display the data from the json file. Template type amp-mustache is used for data-binding the data to be displayed. Let us understand this with the help of a working example as shown below − Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp List</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.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> <script async custom-element = “amp-list” src = “https://cdn.ampproject.org/v0/amp-list-0.1.js”> </script> <script async custom-template = “amp-mustache” src =”https://cdn.ampproject.org/v0/amp-mustache-0.2.js”> </script> <style amp-custom> amp-img { border: 1px solid #ddd; border-radius: 4px; padding: 5px; } </style> </head> <body> <h3>Google AMP – Amp List</h3> <amp-list width = “auto” height = “100” layout = “fixed-height” src = “amplist.json” class = “m1”> <template type = “amp-mustache”> <div class = “images_for_display”> <amp-img width = “150” height = “100” alt = “{{title}}” src = “{{url}}”> </amp-img> </div> </template> </amp-list> </body> </html> Output The output of the working example shown above is as shown below − The json file used in the working example given above is shown here − { “items”: [ { “title”: “Christmas Image 1”, “url”: “images/christmas1.jpg” }, { “title”: “Christmas Image 2”, “url”: “images/christmas2.jpg” }, { “title”: “Christmas Image 3”, “url”: “images/christmas3.jpg” }, { “title”: “Christmas Image 4”, “url”: “images/christmas4.jpg” } ] } We can refresh the list using event on the amp-list as shown in the code given below − Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp List</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.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> <script async custom-element = “amp-list” src = “https://cdn.ampproject.org/v0/amp-list-0.1.js”> </script> <script async custom-template = “amp-mustache” src = “https://cdn.ampproject.org/v0/amp-mustache-0.2.js”> </script> <style amp-custom> amp-img { border: 1px solid #ddd; border-radius: 4px; padding: 5px; } button{ background-color: #ACAD5C; color: white; padding: 12px 20px; border: none; border-radius: 4px; cursor: pointer; float: right; } </style> </head> <body> <h3>Google AMP – Amp List</h3> <button on = “tap:amplist.refresh”>Refresh Images</button> <amp-list id = “amplist” width = “auto” height = “100” layout = “fixed-height” src = “amplist.json” class = “m1”> <template type = “amp-mustache”> <div class = “images_for_display”> <amp-img width = “150” height = “100” alt = “{{title}}” src = “{{url}}”></amp-img> </div> </template> </amp-list> </body> </html> Output The output for the working example given above is as shown here − There is a button added which when clicked calls the refresh action using the on event as shown below − <button on = “tap:amplist.refresh”> Refresh Images </button> //amplist is the id used for amp-list Onclick of the button the json file is called again and the contents are loaded. If there are images already loaded, they will be cached. Print Page Previous Next Advertisements ”;
Category: google Amp
Google AMP – Fit Text
Google AMP – Fit Text ”; Previous Next Amp tag amp-fit-text will reduce the font-size, if the space is not sufficient to render the display. This chapter discusses this tag in detail. To get amp-fit-text working, we need to add the following script − <script async custom-element = “amp-fit-text” src = “https://cdn.ampproject.org/v0/amp-fit-text-0.1.js”> </script> Amp Fit-Text Tag The format for amp-fit text tag is shown below − <amp-fit-text width = “200” height = “200” layout = “responsive”> Text here </amp-fit-text> Example Let us understand this tag better with the help of an example. <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp Fit-Text</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.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> <script async custom-element = “amp-fit-text” src = “https://cdn.ampproject.org/v0/amp-fit-text-0.1.js”> </script> </head> <body> <h1>Google AMP – Amp Fit-Text</h1> <div style = “width:150px;height:150px; “> <amp-fit-text width = “150” height = “150” layout = “responsive”> <b>Welcome To TutorialsPoint – You are browsing the best resource for Online Education</b> </amp-fit-text> </div> </body> </html> Output The output of the code given above is as shown below − If you see the display using amp-fit-text, the content tries to adjust as per the space available. Amp-fit-text comes with 2 attributes max-font-size and min-font-size. When we use max-font-size, and if the space is not available to render the text, it will try to reduce the size and adjust inside the space available. Incase we specify min-font-size and if the space is not available it will truncate the text and show dots where the text is hidden. Example Let us see a working example where we will specify both max-font-size and min-font-size to amp-fit-text. <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp Fit-Text</title> <link rel = “canonical” href = ” http://example.ampproject.org/article-metadata.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> <script async custom-element = “amp-fit-text” src = “https://cdn.ampproject.org/v0/amp-fit-text-0.1.js”> </script> </head> <body> <h1>Google AMP – Amp Fit-Text</h1> <div style = “width:150px;height:150px; “> <amp-fit-text width = “150” height = “150” layout = “responsive” max-font-size = “30” min-font-size = “25”> <b>Welcome To TutorialsPoint – You are browsing the best resource for Online Education</b> </amp-fit-text> </div> </body> </html> Output Print Page Previous Next Advertisements ”;
Google AMP – Date Countdown
Google AMP – Date Countdown ”; Previous Next Yet another amp component called Amp Date countdown which is used to display days, hours, minutes , seconds till a given date ie Y2K38 ( 2038) by default.The display can be done as per locales of your choice; by default it is en (english).Amp-date-countdown uses amp-mustache template for rendering data. In this chapter, we will take a look at some working examples to understand amp-date-countdown in more details. To work with amp-date-countdown, we need to add the following script For amp-date-countdown <script async custom-element = “amp-date-countdown” src = “https://cdn.ampproject.org/v0/amp-date-countdown-0.1.js”> </script> For amp-mustache <script async custom-template = “amp-mustache” src = “https://cdn.ampproject.org/v0/amp-mustache-0.1.js”> </script> Amp-date-countdown Tag The amp-date-countdown tag is as follows − <amp-date-countdown timestamp-seconds = “2100466648” layout = “fixed-height” height = “50”> <template type = “amp-mustache”> <p class = “p1”> {{d}} days, {{h}} hours, {{m}} minutes and {{s}} seconds until <a href = “https://en.wikipedia.org/wiki/Year_2038_problem”> Y2K38 </a>. </p> </template> </amp-date-countdown> Attributes for amp-date-countdown The attributes for amp-date-countdown are listed in the table here − Sr.No Attribute & Description 1 end-date An ISO formatted date to count down to. For example, 2025-08-01T00:00:00+08:00 2 timestamp-ms A POSIX epoch value in milliseconds; assumed to be UTC timezone. For example, timestamp-ms=”1521880470000″ 3 timestamp-seconds A POSIX epoch value in seconds; assumed to be UTC timezone. For example, timestamp-seconds=”1521880470″ 4 timeleft-ms A value in milliseconds which is left to be counting down. For example, 50 hours left timeleft-ms=”180,000,000″ 5 offset-seconds (optional) A positive or negative number which indicated the number of seconds to be added or subtracted from the given end-date. For example, offset-seconds=”60″ adds 60 seconds to the end-date 6 when-ended (optional) Specifies whether to stop the timer when it reaches 0 seconds. The value can be set to stop (default) to indicate the timer to stop at 0 seconds and will not pass the final date or continue to indicate the timer should continue after reaching 0 seconds. 7 locale (optional) An internationalization language string for each timer unit. The default value is en (for English). Supported values are listed in below. Format The formats that amp-date-countdown uses to display the countdown are given in the following table − Sr.No Format & Description 1 d Display day as 0,1,2,3…infinity 2 dd Display day as 00,01,02,03…infinity 3 h Display hour as 0,1,2,3…infinity 4 hh Display hour as 00,01,02,03…infinity 5 m Display minute as 0,1,2,3,4 … infinity 6 mm Display minute as 00,01,02,03….infinity 7 s Display second as 0,1,2,3…infinity 8 ss Display second as 00,01,02,03 ….infinity 9 days Display day or days string as per locale 10 hours Display hour or hours string as per locale 11 minutes Display minute or minutes string as per locale 12 seconds Display second or seconds string as per locale Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp Date-Countdown</title> <link rel = “canonical” href = ” http://example.ampproject.org/article-metadata.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> <script async custom-element = “amp-date-countdown” src = “https://cdn.ampproject.org/v0/amp-date-countdown-0.1.js”> </script> <script async custom-template=”amp-mustache” src= “https://cdn.ampproject.org/v0/amp-mustache-0.1.js”> </script> </head> <body> <h1>Google AMP – Amp Date-Countdown</h1> <amp-date-countdown timestamp-seconds = “2145683234” layout = “fixed-height” height = “50”> <template type = “amp-mustache”> <p class = “p1”> {{d}} days, {{h}} hours, {{m}} minutes and {{s}} seconds until <a href = “https://en.wikipedia.org/wiki/Year_2038_problem”> Y2K38 </a>. </p> </template> </amp-date-countdown> </body> </html> Output Example Let us understand the amp-countdown attributes offset-seconds with a working example − <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp Date-Countdown</title> <link rel = “canonical” href = ” http://example.ampproject.org/article-metadata.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> <script async custom-element = “amp-date-countdown” src = “https://cdn.ampproject.org/v0/amp-date-countdown-0.1.js”> </script> <script async custom-template = “amp-mustache” src = “https://cdn.ampproject.org/v0/amp-mustache-0.1.js”> </script> </head> <body> <h1>Google AMP – Amp Date-Countdown</h1> <amp-date-countdown end-date = “2020-01-19T08:14:08.000Z” offset-seconds = “-50” layout = “fixed-height” height = “100”> <template type = “amp-mustache”> <p class = “p1”> {{d}} days, {{h}} hours, {{m}} minutes and {{s}} seconds until 50 seconds before 2020. </p> </template> </amp-date-countdown> </body> </html> Output List of Locales Supported The following is the list of locales supported by amp-date-countdown − Sr.No Name & Locale 1 en English 2 es Spanish 3 fr French 4 de German 5 id Indonesian 6 it Italian 7 ja Japanese 8 ko Korean 9 nl Dutch 10 pt Portuguese 11 ru Russian 12 th Thai 13 tr Turkish 14 vi Vietnamese 15 zh-cn Chinese Simplified 16 zh-tw Chinese Traditional Now, we will try out one example to display the countdown using one of the locale above. Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp Date-Countdown</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.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> <script async custom-element = “amp-date-countdown” src = “https://cdn.ampproject.org/v0/amp-date-countdown-0.1.js”> </script> <script async custom-template = “amp-mustache” src = “https://cdn.ampproject.org/v0/amp-mustache-0.1.js”> </script> </head> <body> <h1>Google AMP – Amp Date-Countdown</h1> <amp-date-countdown locale = “ja” end-date = “2020-01-19T08:14:08.000Z” offset-seconds = “-50” layout = “fixed-height” height = “100”> <template type = “amp-mustache”> <p
Google AMP – Video
Google AMP – Video ”; Previous Next Amp-video in amp is a standard html5 video used to play direct video embeds. In this chapter, let us understand how to work with and use amp-video. To work with amp-video we need to add following script − <script async custom-element = “amp-video” src = “https://cdn.ampproject.org/v0/amp-video-0.1.js”> </script> Amp-video has src attribute which has the video resource to be loaded, which is lazily loaded by amp at runtime. Besides, all the features are almost same as html5 video tag. The following are the nodes that are to be added to amp video − Source − You can add different media files to be played using this tag. Track − This tag lets you enable the subtitles for the video. Placeholder − This placeholder tag will show content before the video starts. Fallback − This tag will be called when the browser does not support HTML5 video. Format of amp-video tag The format for amp-video tag is shown here − <amp-video controls width = “640” height = “360” layout = “responsive” poster = “images/videoposter.png”> <source src = “video/bunny.webm” type = “video/webm” /> <source src = “video/samplevideo.mp4” type = “video/mp4” /> <div fallback> <p>This browser does not support the video element.</p> </div> </amp-video> Let us understand amp-video using a working example as shown below − Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp Video</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.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> <script async custom-element = “amp-video” src = “https://cdn.ampproject.org/v0/amp-video-0.1.js”> </script> </head> <body> <h3>Google AMP – Amp Video</h3> <amp-video controls width = “640” height = “360” layout = “responsive” poster = “images/videoposter.png”> <source src = “video/bunny.webm” type = “video/webm” /> <source src = “video/samplevideo.mp4” type = “video/mp4” /> <div fallback> <p>This browser does not support the video element.</p> </div> </amp-video> </body> </html> Output The output of the code given above is as shown below − Attributes Available for amp-video The attributes available for amp-video are listed in the table here − Sr.No Attributes & Description 1 src If the <source> node is not present, then src has to be specified and it has be https:// url. 2 poster The poster takes img url which is displayed before the video starts. 3 autoplay Having this attribute on amp-video will autoplay the video if browser supports .The video will play in a muted mode and user will have to tap on the video to unmute it. 4 controls Having this attribute on amp-video will show controls on the video similar to html5 video. 5 loop If this attribute is present on amp-video, the video will play again once finished. 6 crossorigin This attribute comes into picture if the resource to play video are on a different origin. 7 rotate-to-fullscreen If the video is visible, the video displays fullscreen after the user rotates their device into landscape mode Autoplay AMP Video We can use autoplay attribute incase we need to autoplay the video. This feature will work as per browser support. Note that the video will be in mute state when autoplaying. When user taps on the video, it will be unmuted. Let us the autoplay feature with the help of a working example as given below − Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp Video</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.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> <script async custom-element = “amp-video” src = ” https://cdn.ampproject.org/v0/amp-video-0.1.js”> </script> </head> <body> <h3>Google AMP – Amp Video Autoplay</h3> <amp-video controls width = “640” height = “360” layout = “responsive” poster = “images/videoposter.png” autoplay> <source src = “video/bunny.webm” type = “video/webm” /> <source src = “video/samplevideo.mp4” type = “video/mp4” /> <div fallback> <p>This browser does not support the video element.</p> </div> </amp-video> </body> </html> You can activate controls to the video by adding controls attribute as shown in the following code − <amp-video controls width = “640” height = “360” layout = “responsive” poster = “images/videoposter.png” autoplay> <source src = “video/bunny.webm” type = “video/webm” /> <source src = “video/samplevideo.mp4” type = “video/mp4″ /> <div fallback> <p>This browser does not support the video element.</p> </div> </amp-video> Print Page Previous Next Advertisements ”;
Google AMP – Story
Google AMP – Story ”; Previous Next Amp-story is a amp-component used to display content keeping the user engaged with the story. For example using a series of images telling about a brand. To get working with amp-story, we need to include the script as shown below − <script async custom-element = “amp-story” src = “https://cdn.ampproject.org/v0/amp-story-1.0.js”> </script> In this chapter, let us understand what amp-story is and how it works. Assume that we have a image gallery and want to display the same on the page. In the amp page we can make it look beautiful and interactive to the user using amp-story component. The format of amp-story tag looks like as shown below − <amp-story standalone> —> Main story Tag <amp-story-page id = “page-1”> —> Pages inside the story e.g page1 <amp-story-grid-layer template = “fill”> –> Layers for the page1.You can have more than one layer. //Add html elements here or amp components </amp-story-grid-layer> <amp-story-grid-layer template = “fill”> –> Layers for the page1.You can have more than one layer. //Add html elements here or amp components </amp-story-grid-layer> … </amp-story-page> <amp-story-page id = “page-2”> —> Pages inside the story e.g page2 <amp-story-grid-layer template = “fill”> –> Layers for the page2.You can have more than one layer. //Add html elements here or amp components </amp-story-grid-layer> … </amp-story-page> … </amp-story> There are some additional attributes added for amp-story as follows − <amp-story standalone title = “My Story” publisher = “The AMP Team” publisher-logo-src = “publisherlogo image here” poster-portrait-src = “poster portrait here” poster-square-src = “poster square image here” poster-landscape-src = “poster landscape image here”> This feature makes the story telling using amp very interactive. The following code shows a working example for amp-story. The output for the same is shown for desktop as well as mobile mode. Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <script async custom-element = “amp-story” src = “https://cdn.ampproject.org/v0/amp-story-1.0.js”></script> <title>Google AMP – Story</title> <meta name = “viewport” content = “width = device-width,minimum-scale = 1,initial-scale=1”> <link rel = “canonical” href = “/stories/introduction/amp_story_hello_world/”> <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> amp-story { font-family: Roboto, Helvetica, Arial, sans-serif; } amp-story-page * { color: white; text-align: center; } </style> </head> <body> <amp-story standalone title = “Stories in AMP – Hello World” publisher = “AMP Project”> <amp-story-page id = “page-1”> <amp-story-grid-layer template = “fill”> <amp-img src = “images/christmas1.jpg” width = “300” height = “250” layout = “responsive”> </amp-img> </amp-story-grid-layer> <amp-story-grid-layer template = “vertical”> <h1>Hello World</h1> <p>This is an AMP Story.</p> </amp-story-grid-layer> </amp-story-page> <amp-story-page id = “page-2”> <amp-story-grid-layer template = “fill”> <amp-img src = “images/christmas5.jpg” width = “300” height = “250” layout = “responsive”> </amp-img> </amp-story-grid-layer> <amp-story-grid-layer template = “vertical”> <h1>Hello World</h1> <p>This is an AMP Story.</p> </amp-story-grid-layer> </amp-story-page> <amp-story-page id = “page-3”> <amp-story-grid-layer template = “fill”> <amp-img src = “images/christmas3.jpg” width = “300” height = “250” layout = “responsive”> </amp-img> </amp-story-grid-layer> <amp-story-grid-layer template = “vertical”> <h1>Hello World</h1> <p>This is an AMP Story.</p> </amp-story-grid-layer> </amp-story-page> <amp-story-page id = “page-4”> <amp-story-grid-layer template = “fill”> <amp-img src = “images/christmas4.jpg” width = “300” height=”250″ layout = “responsive”> </amp-img> </amp-story-grid-layer> <amp-story-grid-layer template = “vertical”> <h1>Hello World</h1> <p>This is an AMP Story.</p> </amp-story-grid-layer> </amp-story-page> <amp-story-bookend src = “ampstory.json” layout = “nodisplay”> </amp-story-bookend> </amp-story> <body> </html> ampstory.json { “bookendVersion”: “v1.0”, “shareProviders”: [ “email”, “twitter”, “tumblr”, { “provider”: “facebook”, “app_id”: “254325784911610” } ], “components”: [ { “type”: “heading”, “text”: “Introduction” }, { “type”: “small”, “title”: “Next Story is on Car Brands”, “url”: “ampcarbrand.html”, “image”: “images/audi.jpg” } ] } Output on Desktop This is how the story section is displayed. You may also add videos, or any other amp component to make the story more interactive. Once it reaches end of the story it will show what is given in the amp-bookend as shown below − <amp-story-bookend src = “ampstory.json” layout = “nodisplay”> </amp-story-bookend> We have given a ampstory.json file to the amp-bookend.The json file has the details of the preview of the next story as shown below. When a user clicks on the three dots shown at right side, the following screen will be displayed − It gives a replay button which will load the story again. You can click the image of the car which will display the story of the car brands. The output in the mobile mode is as follows − Print Page Previous Next Advertisements ”;
Google AMP – Date Picker
Google AMP – Date Picker ”; Previous Next AMP Datepicker is an amp component which displays calendar on the page wherein the user can select dates. AMP datepicker can be displayed like a static calendar or based on input selection, that is with a click of a button. To get amp-date-picker working we need to add following script to the page − <script async custom-element = “amp-date-picker” src = “https://cdn.ampproject.org/v0/amp-date-picker-0.1.js”> </script> Amp-date-picker Tag The tag of amp-date-picker looks as follows − <amp-date-picker layout = “fixed-height” height = “360”></amp-date-picker> Supported Attributes The following attributes are supported for amp-date-picker − Sr.No Attribute & Description 1 mode Options available are static and overlay. For static, calendar will open by default on the page. For Overlay, the calendar will open upon interaction. 2 mode Options available are single and range. With single, you can select only one date on the calendar. With range, you can select more than one date but in a continuous range. 3 input-selector This can be a queryselector for date input. For example, for id is is #nameoftheid for class it is. nameoftheclass. The date will be updated for the tag to which the id is assigned. 4 start-input-selector This can be a queryselector for date input. For example, for id is is #nameoftheid for class it is .nameoftheclass. The date will be updated for the tag to which the id is assigned. 5 end-input-selector This can be a queryselector for date input. For example for id is is #nameoftheid for class it is .nameoftheclass. The date will be updated for the tag to which the id is assigned. 6 min The earliest date that the user may select. This must be formatted as an ISO 8601 date. If no min attribute is present, the current date will be the minimum date. 7 max The latest date that the user may select. This must be formatted as an ISO 8601 date. If no max attribute is present, the date picker will have no maximum date. 8 month-format The month format you need to display the date selected. By default, the values are “MMMM YYYY” 9 format The format in which you want the date to displayed in the input box or any htmlelement whose selector is used. By default it is “YYYY-MM-DD” 10 week-day-format Format to display day of the week. 11 locale Locale to display calendar view. By default it is en. 12 minimum-nights The number of nights that the user must select in a date range. The default is “1”. A value of “0” allows users to select the same date for the start and end dates. 13 number-of-months The number of months to display at one time in the calendar view. The default is “1”. 14 first-day-of-week The day to specify as the first day of the week (0-6). The default value is “0” (Sunday). 15 day-size The size in px of the date cells in the calendar view table. The default is 39. The main attributes are type and mode. For mode, we have static and overlay type calendars. For type we can have single and range options. With type = ”single” we can select only one date from the calendar and for type = ”range” we can select more than one data in a range. Now, let us understand amp-date-picker for static and overlay type calendars through some working examples. AMP Static date picker For static type date picker we need to specify the mode=static as shown in the example below. Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp Date-Picker Static </title> <link rel = “canonical” href = ” http://example.ampproject.org/article-metadata.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> <script async custom-element = “amp-date-picker” src = “https://cdn.ampproject.org/v0/amp-date-picker-0.1.js”> </script> <script async custom-element = “amp-bind” src = “https://cdn.ampproject.org/v0/amp-bind-0.1.js”> </script> <script async custom-template = “amp-mustache” src = “https://cdn.ampproject.org/v0/amp-mustache-0.1.js”> </script> <style> input[type = text]{ width: 50%; padding: 12px; border: 1px solid #ccc; border-radius: 4px; resize: vertical; } label { padding: 12px 12px 12px 0;display: inline-block; } .col-label { float: left;width: 25%;margin-top: 6px; } .col-content { float: left;width: 75%;margin-top: 6px; } .row:after { content: “”;display: table;clear: both; } .amp_example { background-color: #f1f1f1; padding: 0.01em 16px; margin: 20px 0; box-shadow: 0 2px 4px 0 rgba(0,0,0,0.16),0 2px 10px 0 rgba(0,0,0,0.12)!important; } h3{font-family: “Segoe UI”,Arial,sans-serif; font-weight: 400;margin: 10px 0;} </style> </head> <body> <div class = “amp_example”> <h3>Google AMP – Amp Date-Picker using type = single</h3> <amp-date-picker id = “static-date” type = “single” mode = “static” layout = “fixed-height” height = “600” format = “YYYY-MM-DD” input-selector = “#date”> <div class = “row”> <div class = “col-label”> <label for = “start”> Date is: </label> </div> <div class = “col-content”> <input type = “text” id = “date” name = “date” placeholder = “Date Selected Is…”> </div> </div> </amp-date-picker> <div> </body> </html> Observe that in this example we are displaying calendar i.e datepicker by default on the screen. The date selected by the user is shown in the text field as shown in the demo screen shown below − Output How to get the date selected from amp-date-picker? If you check the above example, there is an attribute called input-selector which is given the id of the text field. When user selects the date , it is shown inside the input field. <amp-date-picker id = “static-date” type = “single” mode = “static” layout = “fixed-height” height = “600” format = “YYYY-MM-DD” input-selector = “#date” <div class = “row”> <div class = “col-label”> <label for = “start”>Date is:</label> </div> <div class = “col-content”> <input type = “text” id = “date” name = “date” placeholder = “Date Selected Is…”>
Google AMP – Mathml
Google AMP – Mathml ”; Previous Next Using MathML, we can display maths formula. In this chapter let us see a working example how to use MathML and work with few mathematical formulae to display the same. To work with MathML, we need to include the following javascript file − <script async custom-element = “amp-mathml” src = “https://cdn.ampproject.org/v0/amp-mathml-0.1.js”> </script> MathML AMP tag The mathML amp tag has the format as shown here − <amp-mathml layout = “container” data-formula = “[x = {-b pm sqrt{b^2-4ac} over 2a}.]”> </amp-mathml> Note that the data-formula is the mandatory attribute to which the formula is given. Example Let us understand this tag better with the help of an example. <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – MathML</title> <link rel = “canonical” href = ” http://example.ampproject.org/article-metadata.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> <script async custom-element = “amp-mathml” src = “https://cdn.ampproject.org/v0/amp-mathml-0.1.js”> </script> </head> <body> <h1>Google AMP – MathML Example</h1> <amp-mathml layout = “container” data-formula = “[x = {-b pm sqrt{b^2-4ac} over 2a}.]”> </amp-mathml> </body> </html> Output Amp-mathml tags when it executes renders the display in an iframe as shown below − Print Page Previous Next Advertisements ”;
Google AMP – Iframes
Google AMP – Iframes ”; Previous Next Google amp-iframe is used to show iframes on the page.There are some conditions to be added to amp-iframe and thus we cannot use normal iframes on the page. This chapter discusses more about this. Conditions to be Followed for iFrames The conditions to be taken care while using iframe in AMP pages are as follows − The url used on an iframe has to be a https request or data-URI or using srcdoc attribute. amp-iframe by default will have sandbox attribute added to it. The sandbox attribute will be set to empty. A empty value to sandbox means that the iframe is maximum sandboxed (extra restriction on iframe). We can add values to the sandbox which will discuss with the help of an example below. A amp-iframe cannot be displayed at the top of the page, it should be almost 600px away from the top or within the first 75% of the viewport when scrolled on top. Incase you have to display iframe at the start, you need to add placeholder to the iframe which we will discuss with the help of examples later in the tutorial. amp-iframe must not have same origin as the container. For example, if your main site is on www.xyz.com , you cannot have iframe src as www.xyz.com/urlname. It can take other such as.xyz.com, example.xyz.com etc. To work with iframes, we need to add the following script − <script async custom-element = “amp-iframe” src = “https://cdn.ampproject.org/v0/amp-iframe-0.1.js”></script> Amp-iframe format is as follows − <amp-iframe width = “600” title = “Google map” height = “400” layout = “responsive” sandbox = “allow-scripts allow-same-origin allow-popups” frameborder = “0” src = “https://maps.google.com/maps?q=telangana&t=&z=13&ie=UTF8&iwloc=&output=embed”> </amp-iframe> Let us understand this with the help of a working example where will use iframe to display Google maps as given below. Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp Iframe</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.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> <script async custom-element = “amp-iframe” src = “https://cdn.ampproject.org/v0/amp-iframe-0.1.js” ></script> <style> div { height:850px; text-align:center; } </style> </head> <body> <h3>Google AMP – Amp Iframe</h3> <div> Google Maps in Iframe </div> <h3>Google AMP – Amp Iframe</h3> <amp-iframe width = “600” title = “Google map” height = “400” layout = “responsive” sandbox = “allow-scripts allow-same-origin allow-popups” frameborder = “0” src = “https://maps.google.com/maps?q=telangana&t=&z=13&ie=UTF8&iwloc=&output=embed”> </amp-iframe> </body> </html> Output Observe that we have placed the iframe at more than 600px from the top. It gives an error as shown below − In the example above, we have used sandbox with values as given below − sandbox = “allow-scripts allow-same-origin allow-popups” Sandbox attribute acts like a permission to the contents to be loaded inside iframe. Here we are allowing all the scripts to be loaded which are coming from the Google maps links. Incase we are not giving sandbox attribute, this is the error displayed which blocks the content to be loaded in the iframe − Note that we have to give the right permission to the sandbox. You can find the details of all the permissions to be given to sandbox here − https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#attr-sandbox. We can make use of placeholder attribute inside an amp-iframe to get rid of the more than 600px condition. A working example for the same is given below − <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp Iframe</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.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> <script async custom-element = “amp-iframe” src = “https://cdn.ampproject.org/v0/amp-iframe-0.1.js”> </script> <style> div { height:850px; text-align:center; } </style> </head> <body> <h3>Google AMP – Amp Iframe</h3> <amp-iframe width = “600” title = “Google map” height = “400” layout = “responsive” sandbox = “allow-scripts allow-same-origin allow-popups” frameborder = “0” src = “https://maps.google.com/maps?q=telangana&t=&z=13&ie=UTF8&iwloc=&output=embed”> <amp-img layout = “fill” src = “images/loading.jpg” placeholder></amp-img> </amp-iframe> </body> </html> We have used amp-img as a placeholder as follows − <amp-iframe width = “600” title = “Google map” height = “400” layout = “responsive” sandbox = “allow-scripts allow-same-origin allow-popups” frameborder = “0” src = “https://maps.google.com/maps?q=telangana&t=&z=13&ie = UTF8&iwloc = &output = embed”> <amp-img layout = “fill” src = “images/loading.jpg” placeholder></amp-img> </amp-iframe> In this case, the restriction of 600px and amp-iframe in 75% viewport is not considered. A loading indicator (three dots) are shown on the image is used as placeholder, which is basically for the amp-iframe src. Once the iframe contents are loaded, the image is removed and the iframe content is shown as displayed in the output shown below − Output Print Page Previous Next Advertisements ”;
Google AMP – Selector
Google AMP – Selector ”; Previous Next Amp-selector is an amp component which displays menu of options and the user can select between the options. The options displayed can be text, images or any other amp-component. In this chapter, let us discuss this in detail. To work with amp-selector, we need to include following javascript files − <script async custom-element = “amp-selector” src = “https://cdn.ampproject.org/v0/amp-selector-0.1.js”> </script> Format of amp-selector The following code shows the sample for format of amp-selector − <amp-selector layout = “container”> <amp-img src = “images/christmas1.jpg” width = “60” height = “40” option = “1”> <amp-img src = “images/christmas2.jpg” width = “60” height = “40” option = “2”> </amp-img> <amp-img src = “images/christmas3.jpg” width = “60” height = “40” option = “3”> </amp-img> <amp-img src = “images/christmas4.jpg” width = “60” height = “40” option = “4”> </amp-img> </amp-selector> You can use standard html tags or amp components inside amp-selector.The contents are displayed like menu on the screen and the user can select between them.The menus displayed can be a single select or multiselect. Let us understand this with the help of an example of single and multi select as given below. Amp Selector Single Select The following code is a sample for amp-selector single select − <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp Selector</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.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> <script async custom-element=”amp-selector” src = “https://cdn.ampproject.org/v0/amp-selector-0.1.js”> </script> <style amp-custom> amp-selector:not([disabled]) amp-img[option][selected]:not([disabled]) { outline-color: #b6A848; outline-width: 2px; } </style> </head> <body> <h3>Google AMP – Amp Selector</h3> <amp-selector layout = “container”> <amp-img src = “images/christmas1.jpg” width = “60” height = “40” option=”1″> </amp-img> <amp-img src=”images/christmas2.jpg” widt h = 60″ height = “40” option = “2”> </amp-img> <amp-img src = “images/christmas3.jpg” width = “60” height = “40” option = “3”> </amp-img> <amp-img src = “images/christmas4.jpg” width = “60” height = “40” option = “4”> </amp-img> </amp-selector> </body> </html> Output The output of the above given code is as shown below − Note that in the above example we have used amp-selector and used images inside to show the options. It is a single select selector so you can select any one image as shown in the output. Amp Selector Multiple Select In this example, let us display amp-selector with images using multiple attributes, we can select multiple options from the selector. Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8 “> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp Selector</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.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> <script async custom-element = “amp-selector” src = “https://cdn.ampproject.org/v0/amp-selector-0.1.js”> </script> <style amp-custom> amp-selector:not([disabled]) amp-img[option][selected]:not([disabled]) { outline-color: blue; outline-width: 2px; } </style> </head> <body> <h3>Google AMP – Amp Selector</h3> <amp-selector layout = “container” multiple> <amp-img src = “images/christmas1.jpg” width = “60” height =”40″ option = “1”> </amp-img> <amp-img src=”images/christmas2.jpg” width = “60” height = “40” option = “2”> </amp-img> <amp-img src =”images/christmas3.jpg” width = “60” height = “40” option = “3”> </amp-img> <amp-img src = “images/christmas4.jpg” width = “60” height = “40” option = “4”> </amp-img> </amp-selector> </body> </html> Output The output of the code shown above is given below − We can also use amp-selector to show radio buttons as shown in the code given below − Example <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Amp Selector</title> <link rel = “canonical” href = “http://example.ampproject.org/article-metadata.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> <script async custom-element = “amp-selector” src = “https://cdn.ampproject.org/v0/amp-selector-0.1.js”> </script> <style amp-custom> .radio-menu { list-style: none; } .radio-menu [option][selected] { outline: none; } .radio-menu [option] { display: flex; align-items: center; } .radio-menu [option]:before { transition: background 0.25s ease-in-out; content: “”; display: inline-block; width: 24px; height: 24px; margin: 8px; border-radius: 100%; border: solid 1px black; } .radio-menu [option = red][selected]:before { text-align: center; content: “✓”; color: white; background: red; } .radio-menu [option = green][selected]:before { text-align: center; content: “✓”; color: white; background: green; } .radio-menu [option = blue][selected]:before { text-align: center; content: “✓”; color: white; background: blue; } </style> </head> <body> <h3>Google AMP – Amp Selector</h3> <amp-selector class = “radio-menu” layout = “container” name = “my-selector”> <div option = “red”>Red</div> <div option = “green”>Green</div> <div option = “blue”>Blue</div> </amp-selector> </body> </html> Output Print Page Previous Next Advertisements ”;
Google AMP – Form
Google AMP – Form ”; Previous Next This chapter explains how to work with form in Google AMP. Note that forms tag remains the same as in standard HTML. AMP has added special restriction on the use of forms due to which we need to add the amp-form JavaScript file to work with forms. Script for amp-form <script async custom-element = “amp-form” src = “https://cdn.ampproject.org/v0/ampform-0.1.js”></script> To use forms in a AMP page, we need to include the above script in the .html file. The amp-form JavaScript file supports http and xmlhttprequest for form submission. Using HTTP request the page is reloaded and with xmlhttprequest it does not reload the page acts like ajax request. Form tag in AMP For xmlhttprequest : <form method = “post” class = “p2” action-xhr = “submitform.php” target = “_top”> //Input fields here </form> For http : <form method = “post” class = “p2” action = “submitform.php” target = “_top”> //Input fields here </form> Amp-form provides special attributes i.e, submit-error and submit-success to handle error and success when form is submitted. Example An example for amp-form is shown below − <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Form</title> <link rel = “canonical” href = “ampform.html”> <meta name = “viewport” conten t = “width = device-width, minimum-scale = 1,initialscale = 1”> <style amp-boilerplate> body{ -webkit-animation: -amp-start 8s steps(1,end) 0s1 normal both;-moz-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-msanimation: -amp-start 8s steps(1,end) 0s 1 normal both;animation: -amp-start 8s steps(1,end) 0s 1 normal both } @-webkit-keyframes -ampstart{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -ampstart{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -ampstart{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -ampstart{from{visibility:hidden}to{visibility:visible}}@keyframes -ampstart{from{visibility:hidden}to{visibility:visible}} </style> <noscript> <style amp-boilerplate> body{ -webkit-animation:none; -moz-animation:none; -msanimation:none; animation:none } </style> </noscript> <script async custom-element = “amp-form” src = “https://cdn.ampproject.org/v0/amp-form-0.1.js”> </script> <script async custom-template = “amp-mustache” src = “https://cdn.ampproject.org/v0/amp-mustache-0.2.js”> </script> <style amp-custom> form.amp-form-submit-success [submit-success], form.amp-form-submit-error [submit-error]{ margin-top: 16px; } form.amp-form-submit-success [submit-success] { color: white; background-color:gray; } form.amp-form-submit-error [submit-error] { color: red; } form.amp-form-submit-success.hide-inputs > input { display: none; } </style> </head> <body> <h3>Google AMP – Form</h3> <form method = “post” class = “p2” action-xhr = “submitform.php” target = “_top”> <p>AMP – Form Example</p> <div> <input type = “text” name = “name” placeholder = “Enter Name” required><br/><br/> <input type = “email” name = “email” placeholder = “Enter Email” required> <br/> <br/> </div> <input type = “submit” value = “Submit”> <div submit-success> <template type = “amp-mustache”> Form Submitted! Thanks {{name}}. </template> </div> <div submit-error> <template type = “amp-mustache”> Error! {{name}}, please try again. </template> </div> </form> </body> </html> Output When you executed the code shown above, you will find the result as shown below − Now, enter the details and click the Submit button. The output screen displayed is as follows − Observe that we have used amp-mustache for data-binding. The form is using action-xhr ie xmlhttprequest to submit form. We have used submitform.php file which returns the data in json format. <form method = “post” class = “p2” action-xhr = “submitform.php” target = “_top”> </form> submitform.php <?php if(!empty($_POST)){ $domain_url = (isset($_SERVER[”HTTPS”]) ? “https” : “http”) . “://$_SERVER[HTTP_HOST]”; header(“Content-type: application/json”); header(“AMP-Access-Control-Allow-Source-Origin: ” . $domain_url); header(“Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin”); $myJSON = json_encode($_POST); echo $myJSON; } ?> For the form to work using xmlhttprequest, we need to add headers as per the CORS specification. Details of response headers added to submitform.php are shown below − For the form to work, we need to add headers such as access-control-expose-headers with value AMP-Access-Control-Allow-Source-Origin and amp-access-controlallow- source-origin −http://localhost:8080. Note that we are using a php file and apache server. In php file, we have added the required headers as shown below − <?php if(!empty($_POST)){ $domain_url = (isset($_SERVER[”HTTPS”]) ? “https” : “http”) . “://$_SERVER[HTTP_HOST]”; header(“Content-type: application/json”); header(“AMP-Access-Control-Allow-Source-Origin: ” . $domain_url); header(“Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin”); $myJSON = json_encode($_POST); echo $myJSON; } ? ?> In case we use a normal http request the page will get reloaded as shown below − For http request we have used form as follows − <form method = “GET” class = “p2” action = “submitform.php” target = “_top”> </form> Example Observe the following code for a better understanding − <!doctype html> <html amp lang = “en”> <head> <meta charset = “utf-8”> <script async src = “https://cdn.ampproject.org/v0.js”></script> <title>Google AMP – Form</title> <link rel = “canonical” href = “ampform.html”> <meta name = “viewport” content = “width = device-width,minimum-scale = 1,initialscale = 1”> <style amp-boilerplate> body{ -webkit-animation: -amp-start 8s steps(1,end) 0s1 normal both;-moz-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-msanimation: -amp-start 8s steps(1,end) 0s 1 normal both;animation: -amp-start 8s steps(1,end) 0s 1 normal both } @-webkit-keyframes -ampstart{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -ampstart{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -ampstart{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -ampstart{from{visibility:hidden}to{visibility:visible}}@keyframes -ampstart{from{visibility:hidden}to{visibility:visible}} </style> <noscript> <style amp-boilerplate> body { -webkit-animation:none; -moz-animation:none; -msanimation:none; animation:none} >/style> </noscript> <script async custom-element = “amp-form” src = “https://cdn.ampproject.org/v0/amp-form-0.1.js”> </script> <script async custom-template = “amp-mustache” src = “https://cdn.ampproject.org/v0/amp-mustache-0.2.js”> </script> <style amp-custom> form.amp-form-submit-success [submit-success], form.amp-form-submit-error [submit-error]{ margin-top: 16px; } form.amp-form-submit-success [submit-success] { color: white; background-color:gray; } form.amp-form-submit-error [submit-error] { color: red; } form.amp-form-submit-success.hide-inputs > input { display: none; } </style> </head> <body> <h3>Google AMP – Form</h3> <form method = “GET” class = “p2” action = “submitform.php” target = “_top”> <p>AMP – Form Example</p> <div> <input type = “text” name = “name” placeholder = “Enter Name” required> <br/> <br/> <input type = “email” name = “email” placeholder = “Enter Email” required> <br/> <br/> <div> <input type = “submit” value = “Submit”> <div submit-success> <template type = “amp-mustache”> Form Submitted! Thanks {{name}}. </template> </div> <div submit-error> <template type = “amp-mustache”> Error! {{name}}, please try again. </template> </div> </form> </body> </html> Output When you executed the code shown above, you will find the result as shown below − Print Page Previous Next Advertisements ”;