如何在AMP页面中添加自定义CSS和脚本

时间:2018-11-23 09:49:49

标签: html asp.net asp.net-mvc amp-html

    <!doctype html>
<html ⚡>
  <head>
    <meta charset="utf-8">
    <title>Sample document</title>
    <link rel="canonical" href="AMPstyle.html">
    <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
    <style amp-custom>
   @import "css/style1.css"; 
  {% include "css/style2.css" %}
    </style> 

<script type="application/ld+json" src="../Scripts/appjs/StaticCampaigns.js"></script>
    <script type="application/ld+json">
    {
      "@context": "http://schema.org",
      "@type": "NewsArticle",
      "headline": "Article headline",
      "image": [
        "thumbnail1.jpg"
      ],
      "datePublished": "2015-02-05T08:00:00+08:00"
    }
    </script>
    <script async custom-element="amp-carousel" src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js"></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>
    <script async src="https://cdn.ampproject.org/v0.js"></script>
    <script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
       <!-- <link rel="stylesheet" type="text/css" media="all" href="css/initcarousel.css" /> -->

  </head>
  <body>
<h>AMP Pages Example.</h>
</body>
</html>

添加CSS和脚本后,我得到了无效的AMP页面。我无法添加样式表和CSS无法正常工作。自定义CSS和脚本不起作用。请建议如何在AMP页面中添加自定义CSS(@import“ css / style1.css”;)和脚本(src =“ ../ Scripts / appjs / StaticCampaigns.js”)。

1 个答案:

答案 0 :(得分:2)

AMP文档要求所有CSS都必须在页面标题中内联,并要求您使用样式标签上的amp-custom属性。看来您正在尝试在页眉中添加@import语句,即使该语句不是AMP也不是有效的。我建议先编译它,然后将内容复制到样式标签中。请记住,还有50,000个字节size limit

例如:

<style amp-custom>
  h1 {
   color: red
  }
</style>

对于脚本,您不能在AMP文档中包含创作的JavaScript。您需要寻找一个等效的AMP component