简体   繁体   中英

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 . 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/ . 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. Currently, it is set to an invalid value (“relatedamp#aeohig”) value.
  2. Please make sure server endpoints implement the requirements for CORS security. See : https://www.ampproject.org/docs/fundamentals/amp-cors-requests#cors-security-in-amp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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