退出 AMP 中的意图弹出窗口?

Exit Intent Popup in AMP?

有人要求我在一个完全以 AMP-HTML 编写的网站上放置一个 Exit Intent Popup(即使对于桌面用户也是如此)。

listening to the mouseleave event.

在标准 JS 中实现这一点相当容易

amp-script compatibility table 说 onmouseleave 还不能用于自定义脚本。

明确一点:退出意图弹出窗口是页内横幅,不会干扰导航或关闭选项卡,这比 beforeunload messages. Also, this is not an amp-ad-exit. Amp-position-observer 更友好在页面滚动上,而不是光标位置。

有点没思路。 AMP 中是否可以使用退出意图弹出窗口?

好问题。但是您必须展示您的代码,这样其他人就不必为您完成这项工作。

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.

您的问题是您指的是 jQuery 示例。忘记 jQuery,一切都会好起来的。本机 JavaScript 有类似 'mouseleave' 的事件,并且运行良好

我的解决方案,我使用了 amp-script 组件:

<!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>

此代码不适用于Whosebug,但相信这是一个可行的解决方案,您可以查看演示here

唯一需要注意的是我在 amp-script 中写了 layout="flex-item"。这是有原因的,否则将需要用户操作。如果您对 layout="...":

有任何疑问,请阅读此处