问题描述
我被要求在已经访问过的网站上放置退出意图弹出窗口.完全以AMP-HTML格式编写(甚至适用于台式机用户).
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).
通过监听mouseleave事件,这在标准JS中非常容易实现.
This is fairly easy to implement in standard JS by listening to the mouseleave event.
amp-script兼容性表表示onmouseleave尚不适用于自定义脚本.
The amp-script compatibility table says that onmouseleave is not available yet for custom scripts.
请明确说明:退出意图弹出窗口"是页内横幅,请勿打扰导航或关闭选项卡,这使它们比.另外,这不是 amp-ad-exit . Amp-position-observer 仅适用于页面滚动,而不适用于光标位置.
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.
金达出主意.AMP中是否可能有退出意图弹出窗口?
Kinda out of ideas. Is an Exit Intent Popup possible in AMP?
推荐答案
好问题.但是您必须显示您的代码,以便其他人不必为您完成工作.
Good question. But you had to show your code so that others wouldn't have to do the work for you.
amp-script兼容性表显示onmouseleave不是自定义脚本尚未提供.
The amp-script compatibility table says that onmouseleave is notavailable yet for custom scripts.
您的问题是您正在引用jQuery示例.忘了jQuery,一切都会好起来的.原生JavaScript具有类似"mouseleave"的事件,并且工作正常
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
我的解决方案,我使用了amp-script组件:
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>
此代码不适用于stackoverflow,但相信这是一个可行的解决方案,您可以查看演示此处
This code does not work on stackoverflow, but believe that this is a working solution, you can view the demo here
唯一的警告是我在 amp-script
中编写了 layout =" flex-item"
.这是有原因的,否则将需要用户采取行动.如果您对 layout ="......"
有任何疑问,请在此处阅读:
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/组件/amp-script/?format = websites#user-gestures
这篇关于退出AMP中的Intent Popup?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!