HTML для AMP

Сложность: Продвинутый

HTML для AMP (Accelerated Mobile Pages)

Создание быстрых мобильных страниц с использованием AMP HTML.

1. Базовая структура AMP страницы

<!doctype html>
<html amp lang="ru">
<head>
    <meta charset="utf-8">
    <script async src="https://cdn.ampproject.org/v0.js"></script>
    <title>Моя AMP страница</title>
    <link rel="canonical" href="https://example.com/regular-page.html">
    
    <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
    
    <script type="application/ld+json">
    {
      "@context": "https://schema.org",
      "@type": "NewsArticle",
      "headline": "Заголовок статьи",
      "image": ["https://example.com/photo.jpg"],
      "datePublished": "2024-01-15T08:00:00+08:00",
      "author": {
        "@type": "Person",
        "name": "Иван Иванов"
      }
    }
    </script>
    
    <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>
        body {
            font-family: "Roboto", sans-serif;
            margin: 0;
            padding: 0;
            background: #f5f5f5;
        }
        
        .header {
            background: #4a90e2;
            color: white;
            padding: 15px;
            text-align: center;
        }
        
        .content {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background: white;
        }
        
        .amp-img {
            max-width: 100%;
            height: auto;
        }
    </style>
</head>
<body>
    <header class="header">
        <h1>Моя AMP страница</h1>
    </header>
    
    <main class="content">
        <article>
            <h2>Заголовок статьи</h2>
            
            <amp-img 
                src="https://example.com/image.jpg" 
                width="800" 
                height="400" 
                layout="responsive"
                alt="Описание изображения"
                class="amp-img">
            </amp-img>
            
            <p>Это содержимое AMP страницы. AMP гарантирует быструю загрузку на мобильных устройствах.</p>
            
            <amp-carousel width="800" height="400" layout="responsive" type="slides">
                <amp-img src="https://example.com/slide1.jpg" width="800" height="400" layout="responsive" alt="Слайд 1"></amp-img>
                <amp-img src="https://example.com/slide2.jpg" width="800" height="400" layout="responsive" alt="Слайд 2"></amp-img>
                <amp-img src="https://example.com/slide3.jpg" width="800" height="400" layout="responsive" alt="Слайд 3"></amp-img>
            </amp-carousel>
            
            <p>Дополнительный контент статьи...</p>
        </article>
    </main>
    
    <footer style="text-align: center; padding: 20px; background: #333; color: white;">
        <p>© 2024 Мой сайт</p>
    </footer>
</body>
</html>

2. AMP компоненты

<!doctype html>
<html amp lang="ru">
<head>
    <meta charset="utf-8">
    <script async src="https://cdn.ampproject.org/v0.js"></script>
    
    <!-- AMP компоненты -->
    <script async custom-element="amp-carousel" src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js"></script>
    <script async custom-element="amp-video" src="https://cdn.ampproject.org/v0/amp-video-0.1.js"></script>
    <script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
    <script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>
    <script async custom-element="amp-sidebar" src="https://cdn.ampproject.org/v0/amp-sidebar-0.1.js"></script>
    
    <title>AMP с компонентами</title>
    <link rel="canonical" href="https://example.com/amp-page.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>
        body { margin: 0; font-family: Arial, sans-serif; }
        .header { background: #2c3e50; color: white; padding: 15px; }
        .menu-button { background: none; border: none; color: white; font-size: 20px; cursor: pointer; }
        .content { padding: 20px; }
        .sidebar { background: white; padding: 20px; width: 300px; }
    </style>
</head>
<body>
    <!-- Sidebar -->
    <amp-sidebar id="sidebar" layout="nodisplay" side="left">
        <div class="sidebar">
            <button on="tap:sidebar.close">✕ Закрыть</button>
            <nav>
                <ul style="list-style: none; padding: 0;">
                    <li><a href="#home">Главная</a></li>
                    <li><a href="#about">О нас</a></li>
                    <li><a href="#contact">Контакты</a></li>
                </ul>
            </nav>
        </div>
    </amp-sidebar>
    
    <header class="header">
        <button class="menu-button" on="tap:sidebar.open">☰ Меню</button>
        <h1 style="margin: 0;">AMP с компонентами</h1>
    </header>
    
    <main class="content">
        <!-- Видео -->
        <amp-video 
            width="640" 
            height="360" 
            layout="responsive"
            poster="https://example.com/poster.jpg"
            controls>
            <source src="https://example.com/video.mp4" type="video/mp4">
            <div fallback>
                <p>Ваш браузер не поддерживает видео.</p>
            </div>
        </amp-video>
        
        <!-- Форма -->
        <amp-form method="post" action-xhr="https://example.com/submit">
            <fieldset>
                <label>
                    Имя:
                    <input type="text" name="name" required>
                </label>
                
                <label>
                    Email:
                    <input type="email" name="email" required>
                </label>
                
                <label>
                    Сообщение:
                    <textarea name="message" rows="4"></textarea>
                </label>
                
                <input type="submit" value="Отправить">
            </fieldset>
            
            <div submit-success>
                <template type="amp-mustache">
                    Сообщение отправлено: {{message}}
                </template>
            </div>
            
            <div submit-error>
                <template type="amp-mustache">
                    Ошибка: {{message}}
                </template>
            </div>
        </amp-form>
        
        <!-- Analytics -->
        <amp-analytics>
            <script type="application/json">
            {
                "requests": {
                    "pageview": "/analytics?url=${canonicalUrl}&title=${title}"
                },
                "triggers": {
                    "trackPageview": {
                        "on": "visible",
                        "request": "pageview"
                    }
                }
            }
            </script>
        </amp-analytics>
    </main>
</body>
</html>