简体   繁体   English

Google AMP显示更多按钮滚动列表到页面顶部。 怎么预防呢?

[英]Google AMP show more button scrolls list to top of page. How to prevent it?

I have this developed this page using google amp list and show more button component https://www.newsapp.io/ampproject . 我已经使用Google放大器列表开发了此页面,并显示了更多按钮组件https://www.newsapp.io/ampproject The problem I am facing is that after show more button fetches new data and shows the page moves to the top (first item), whereas I want it to remain at the original position. 我面临的问题是,在显示更多按钮之后获取新数据并显示页面移至顶部(第一项),而我希望它保持在原始位置。 I have used exactly the same code as given in the docs: https://ampbyexample.com/advanced/show_more_button/ . 我已经使用了与文档中给出的完全相同的代码: https : //ampbyexample.com/advanced/show_more_button/ Here is my list code: - 这是我的清单代码:-

        <amp-state id="productsState" src="https://www.newsapp.io/amp">
        </amp-state>

        <!-- ... while the `amp-state` with id `product` is just used to implement the show-more logic where we are allowing the user to click 3 times. -->
        <amp-state id="product">
            <script type="application/json">
                {
                    "moreItemsPageIndex": 0,
                    "hasMorePages": true
                }
            </script>
        </amp-state>


        <!-- We bind the `src` attribute of the `amp-list` to the `amp-state` object containing the products
            from that component as a `src`. We also dynamically change the height of the amp-list based on
            the number of items (each item has a height of `24px`). -->
        <amp-list src="https://www.newsapp.io/amp"
                  [src]="productsState.items"
                  width="auto"
                  height="600"
                  [height]="productsState.items.length * 10"
                  class="m1">
            <template type="amp-mustache"><div class="card mt1">
                    <amp-img src="{{approved_image}}" width="5" height="2" layout="responsive" alt="The final spritzer" class="mb1"></amp-img>

                    <h3 class="mb1 px3"><a href="">{{approved_title}}</a> </h3>

                    <i class="px3">{{timeago}} ago</i>

                    <ul class="ampstart-social-follow list-reset flex  flex-wrap m0 mb1 px3">
                        <li>
                            <a href="http://www.facebook.com/share.php?u={{name}}" target="_blank" class="inline-block p1" aria-label="Link to AMP HTML Twitter"><i class="fab fa-facebook-square"></i></a>
                        </li>
                        <li>
                            <a href="https://twitter.com/share?url={{name}}&text={{name}}" target="_blank" class="inline-block p1" aria-label="Link to AMP HTML Twitter"><i class="fab fa-twitter-square"></i></a>
                        </li>
                        <li>
                            <a href="https://plus.google.com/share?url={{name}}" target="_blank" class="inline-block p1" aria-label="Link to AMP HTML Twitter"><i class="fab fa-linkedin"></i></a>
                        </li>
                        <li>
                            <a href="https://www.linkedin.com/shareArticle?mini=true&url={{name}}" target="_blank" class="inline-block p1" aria-label="Link to AMP HTML Twitter"><i class="fab fa-google-plus-square"></i></a>
                        </li>
                        <li>
                            <a href="whatsapp://send?text={{name}}" target="_blank" class="inline-block p1" aria-label="Link to AMP HTML Twitter"><i class="fab fa-whatsapp-square"></i></a>
                        </li>
                    </ul>



                    <p class="mb1 px3">{{approved_description}} </p>

                    <div class="px3">
                        <button class="ampstart-btn ampstart-btn-secondary mb1 px3">
                            Read more
                        </button>
                    </div>
                </div>
            </template>
        </amp-list>

        <!-- The show more button is implemented via a form which triggers a page update on the `submit-success` event.
            We are then merging the form results into the items already loaded by the `amp-state` using the `concat` function. -->
        <form method="GET"
              action="relatedamp#aeohig"
              action-xhr="relatedamp"
              target="_top"
              on="submit-success: AMP.setState({
          productsState: {
            items: productsState.items.concat(event.response.items)
          },
          product: {
            moreItemsPageIndex: product.moreItemsPageIndex + 1,
            hasMorePages: event.response.hasMorePages
          }
        });">
            <input type="hidden" name="moreItemsPageIndex" value="0" [value]="product.moreItemsPageIndex">
            <input type="submit"
                   value="Show more"
                   class="ampstart-btn caps m1 show"
                   [class] = "(product.hasMorePages == false ? 'hide' : 'ampstart-btn caps m1 mb3 show')">
        </form>

The behavior is not reproducible. 该行为是不可复制的。 However, I noticed console errors which is blocking the form submit action. 但是,我注意到控制台错误阻止了表单提交操作。 I recommend fixing the below console errors first. 我建议先修复以下控制台错误。

  1. Include right endpoint in “action” attribute. 在“操作”属性中包括右端点。 The value must be an https URL (absolute or relative) and must not be a link to a CDN. 该值必须是https URL(绝对或相对),并且不能是指向CDN的链接。 Currently, it is set to an invalid value (“relatedamp#aeohig”) value. 当前,它被设置为无效值(“ relatedamp#aeohig”)值。
  2. Please make sure server endpoints implement the requirements for CORS security. 请确保服务器端点实现了CORS安全性要求。 See : https://www.ampproject.org/docs/fundamentals/amp-cors-requests#cors-security-in-amp 参见: https : //www.ampproject.org/docs/fundamentals/amp-cors-requests#cors-security-in-amp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM