Exit Intent Popup in AMP?

不羁岁月 提交于 2021-02-11 14:12:41

问题


I've been asked to put an Exit Intent Popup on a site which has been written entirely as AMP-HTML (even for desktop users).

This is fairly easy to implement in standard JS by listening to the mouseleave event.

The amp-script compatibility table says that onmouseleave is not available yet for custom scripts.

Just to be clear: Exit Intent Popups are in-page banners and do not disturb navigation or closing the tab, which make them friendlier than beforeunload messages. Also, this is not an amp-ad-exit. Amp-position-observer only works on page scroll, not cursor position.

Kinda out of ideas. Is an Exit Intent Popup possible in AMP?


回答1:


Good question. But you had to show your code so that others wouldn't have to do the work for you.

This is fairly easy to implement in standard JS by listening to the mouseleave event.

The amp-script compatibility table says that onmouseleave is not available yet for custom scripts.

Your problem is that you are referring to the jQuery example. Forget about jQuery and everything will be fine. Native JavaScript has an event like 'mouseleave' and it works fine

My solution, i used the amp-script component:

<!DOCTYPE html>
<html amp lang="en">
  <head>
    <meta charset="utf-8" />
    <script async src="https://cdn.ampproject.org/v0.js"></script>
    <script async custom-element="amp-script" src="https://cdn.ampproject.org/v0/amp-script-0.1.js"></script>
    <title>Hello, AMPs</title>
    <link rel="canonical" href="https://amp.dev/documentation/guides-and-tutorials/start/create/basic_markup/" />
    <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1" />
    <meta name="amp-script-src" content="sha384-joKetGNlB_ui7udIDhN5ersYPhzqRPNjCP-JO6E6VFBsjVgAHe7j-ijiMvr5gG2F" />
    <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>
    <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>
      .header {
        background-color: #56bcf9;
        height: 50px;
      }

      .main-content {
        max-width: 1200px;
        margin: 0 auto;
      }

      p {
        line-height: 1.8;
      }

      .modal {
        box-shadow: 0 30px 90px -20px rgba(0, 0, 0, 0.3), 0 0 1px 1px rgba(0, 0, 0, 0.05);
        background-color: #fff;
        display: none;
        height: 100px;
        left: 50%;
        position: fixed;
        padding: 15px;
        top: 50%;
        transform: translate(-50%, -50%);
        width: 200px;
      }

      .modal.show {
        display: block;
      }
    </style>
  </head>

  <body>
    <header class="header">Header</header>

    <amp-script script="hello-world" layout="flex-item">
      <div class="main-content" id="mainContent">
        <h1>Main-content</h1>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
        <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>
      </div>

      <div class="modal" id="modal">Modal text</div>
    </amp-script>

    <script id="hello-world" type="text/plain" target="amp-script">
      function showModal(){
        const modal = document.querySelector('#modal');
        modal.classList.add('show');
      }

      const mainContent = document.querySelector('#mainContent');
      mainContent.addEventListener('mouseleave', showModal);
    </script>
  </body>
</html>

This code does not work on stackoverflow, but believe that this is a working solution, you can view the demo here

The only caveat is that I wrote layout="flex-item" in amp-script. There are reasons for this, otherwise user-action would be required. Read it here if you have any problems with layout="...":

  • https://github.com/ampproject/amphtml/issues/26511#issuecomment-601937159
  • https://amp.dev/documentation/components/amp-script/?format=websites#user-gestures


来源:https://stackoverflow.com/questions/62541764/exit-intent-popup-in-amp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!