2017-06-13 33 views
0

我試圖用amp-live-list顯示一個項目。我需要只顯示一個項目並每10秒更新一次。用amp-live-list更新單個和同一個項目

我現在的解決方案只顯示<div items>的第一個子元素,但不是很有信心,這是一個正確的方法。

這裏是我創建的小提琴,雖然你不能看到amp-live-list如何工作,因爲小提琴會自動更新html源碼。但我希望你能看到代碼,以便更容易理解我想要做的事情。謝謝!

Codepen

<!doctype html> 
 
<html amp lang="en"> 
 
    <head> 
 
    <meta charset="utf-8"> 
 
    <script async src="https://cdn.ampproject.org/v0.js"></script> 
 
    <title>Hello, AMPs</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-custom> 
 
    body{font-family: arial, helvetica, sans-serif;} 
 
    pre{background: #FFF8DC;padding: 10px} 
 
    .amp-live-list-item{justify-content:center;display:flex;} 
 
.blog{padding:0;background:#fff;width:300px;} 
 
.blog p{padding:8px;margin:0;} 
 
.match{background:#fff;min-width:0%;} 
 
.side{display:flex;align-items:center;justify-content:space-around;} 
 
.content{font-size:2rem;padding:8px;} 
 
.flag{padding-top:8px;padding-bottom:8px;} 
 
.amp-live-list-item{display:none;} 
 
.amp-live-list-item:first-child{display:flex;} 
 
    </style> 
 
    <script type="application/ld+json"> 
 
     { 
 
     "@context": "http://schema.org", 
 
     "@type": "NewsArticle", 
 
     "headline": "Open-source framework for publishing content", 
 
     "datePublished": "2015-10-07T12:02:41Z", 
 
     "image": [ 
 
      "logo.jpg" 
 
     ] 
 
     } 
 
    </script> 
 
    <script async custom-element="amp-carousel" src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js"></script> 
 
    
 
     <script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script> 
 
    
 
    <script async custom-element="amp-live-list" src="https://cdn.ampproject.org/v0/amp-live-list-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> 
 
    </head> 
 
    <body> 
 
    
 
    <p>I want to show only one element below. I hide the other two elements with css as below to show the most updated content.</p> 
 
    <pre>.amp-live-list-item{display:none;} 
 
.amp-live-list-item:first-child{display:flex;} 
 
</pre> 
 
    <br> 
 
    <p>Not sure whether this is a proper way to do.</p> 
 
    
 
    <hr> 
 

 
    <amp-live-list layout="container" 
 
    data-poll-interval="10000" 
 
    data-max-items-per-page="2" 
 
    id="amp-live-list-time"> 
 
     <button update 
 
     on="tap:amp-live-list-time.update" 
 
     class="ampstart-btn ml1 caps">Update</button> 
 
    <div items> 
 

 
<div id="item3" 
 
     data-update-time="20170610" 
 
     data-sort-time="20170610"> 
 
     <div class="card blog"> 
 
      <a href=""> 
 
      <p>Night</p> 
 
      </a> 
 
     </div> 
 
     </div> 
 

 

 
    <div id="item2" 
 
     data-update-time="20170609" 
 
     data-sort-time="20170609"> 
 
     <div class="card blog"> 
 
      <a href=""> 
 
      <p>Afternoon</p> 
 
      </a> 
 
     </div> 
 
     </div> 
 

 

 
     <div id="item1" 
 
     data-update-time="20170608" 
 
     data-sort-time="20170608"> 
 
     <div class="card blog"> 
 
      <a href=""> 
 
      <p>Morning</p> 
 
      </a> 
 
     </div> 
 
     </div> 
 

 

 
    </div> 
 

 

 
    </amp-live-list> 
 
    
 
</body> 
 
</html>

回答