Skip to content

Instantly share code, notes, and snippets.

@yongzhihuang
Last active May 30, 2019 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yongzhihuang/ad3e4b02c5216009d3c1bdcdeb8fd391 to your computer and use it in GitHub Desktop.
Save yongzhihuang/ad3e4b02c5216009d3c1bdcdeb8fd391 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<title>TNY Growler</title>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0,width=device-width">
<style>
@font-face {
font-family: "Graphik Web";
font-stretch: normal;
font-style: normal;
font-weight: 500;
src: url('/fonts/Graphik-Medium.eot');
src: url('/fonts/Graphik-Medium.eot?#iefix') format('embedded-opentype'), url('/fonts/Graphik-Medium.woff2') format('woff2'), url('/fonts/Graphik-Medium.woff') format('woff');
}
@font-face {
font-family: "Adobe Caslon";
font-style: normal;
font-weight: normal;
src: url('/fonts/AdobeCaslonPro-Regular.woff2') format('woff2'), url('/fonts/AdobeCaslonPro-Regular.woff') format('woff');
}
@font-face {
font-family: "Adobe Caslon Italic";
font-style: normal;
font-weight: normal;
src: url('/fonts/AdobeCaslonPro-Italic.woff2') format('woff2'), url('/fonts/AdobeCaslonPro-Italic.woff') format('woff');
}
body,
html {
font-family: "Graphik Web", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
font-weight: 500;
line-height: 1.57;
height: 100%;
width: 100%;
padding: 0;
margin: 0;
-webkit-font-smoothing: antialiased;
}
.sales-copy em {
font-style: normal;
font-family: "Adobe Caslon Italic", Georgia, "Times New Roman", Times, serif;
}
#growler-wrapper {
background: #333;
bottom: 0;
color: #fff;
height: 50px;
left: 0;
min-width: 320px !important;
position: absolute;
width: 100%;
z-index: 1;
transition: height 0.5s ease-in-out, transform 0.5s ease-in-out;
transform-origin: bottom center;
overflow: hidden;
}
#growler-wrapper.expanded {
height: 240px;
}
#growler-wrapper:not(.expanded) .expanded-msg {
display: none;
}
#growler-wrapper.slide-in {
transform: translateY(100%);
}
.collapsed-msg {
display: table;
height: 50px;
width: 100%;
}
.collapsed-msg > span {
display: table-cell;
padding: 0 148px;
text-align: center;
vertical-align: middle;
}
.expanded-msg {
background: url('/images/CM/growler/group-7@2x.png') 177px 8px no-repeat;
background-size: 163px 142px;
font-family: "Adobe Caslon", Georgia, "Times New Roman", Times, serif;
font-size: 17px;
font-weight: normal;
height: 165px;
line-height: 1.5;
padding: 10px 34px 0 392px;
}
#cta {
top: 0;
color: #fff;
height: 50px;
font-size: 13px;
line-height: 50px;
position: absolute;
right: 30px;
z-index: 999;
}
#cta::after {
background-size: 100%;
background: url('/images/svg/arrow-white.svg') top left no-repeat;
content: '';
display: inline-block;
height: 10px;
margin-left: 15px;
transform-origin: center;
transition: transform 0.5s ease-in-out;
vertical-align: baseline;
width: 18px;
}
#growler-wrapper.expanded #cta::after {
transform: rotate(180deg);
}
#growler-wrapper.expanded #cta > a {
display: none;
}
.expanded-msg > span:not(.header) {
display: inline-block;
margin-right: 95px;
padding-bottom: 1.3em;
}
.header {
display: block;
font-weight: normal;
font-family: "Adobe Caslon", Georgia, "Times New Roman", Times, serif;
font-size: 40px;
line-height: 1.13;
margin-bottom: 5px;
}
.strike-through {
text-decoration: line-through;
}
.expanded-msg > span.signin-copy {
font-family: "Graphik Web", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 13px;
line-height: 1.6;
display: block;
}
.collapsed-msg a.subscribe-cta,
.expanded-msg .signin-copy a,
#cta a {
color: #fff;
text-decoration: underline;
}
.expanded-msg > a.subscribe-cta {
background: #087cc3;
color: #fff;
display: inline-block;
font-family: "Graphik Web", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 13px;
height: 48px;
line-height: 48px;
text-align: center;
text-decoration: none;
width: 255px;
border-radius: 2px;
position: relative;
bottom: 1.3em;
}
.hide {
display: none;
}
.hidden-cta::after {
display: none !important;
}
.close {
height: 24px;
width: auto;
background: none;
border: none;
position: relative;
vertical-align: middle;
top: 5px;
margin-left: 12px;
}
.close::before {
background-color: #dddddd;
content: ' ';
height: 24px;
position: absolute;
top: 0;
width: 1px;
transform: rotate(45deg);
}
.close::after {
background-color: #dddddd;
content: ' ';
height: 24px;
position: absolute;
top: 0;
width: 1px;
transform: rotate(-45deg);
}
.hide-with-animation {
visibility: hidden;
opacity: 0;
transition: visibility 1s, opacity 0.5s linear !important;
}
@media screen and (max-width: 768px) {
.collapsed-msg > span {
padding: 0 20px;
}
.expanded-msg {
padding: 0 5px 0 300px;
background-position: 96px 8px;
font-size: 19px;
}
#cta {
right: 15px;
}
#cta span {
display: none;
}
#cta::before {
display: inline-block;
content: '';
width: 1px;
height: 22px;
background: #737373;
vertical-align: middle;
}
#cta::after {
width: 14px;
height: 8px;
vertical-align: middle;
}
.expanded-msg > span:not(.header) {
padding-bottom: 10px;
}
.header {
font-size: 24px;
}
.expanded-msg > a.subscribe-cta {
bottom: 0;
}
.expanded-msg > .signin-copy {
padding-top: 10px;
padding-bottom: 0;
}
}
@media screen and (max-width: 600px) {
body, html {
font-size: 13px;
}
#growler-wrapper {
height: 70px;
}
#growler-wrapper.expanded {
height: 300px;
}
#growler-wrapper .collapsed-msg > span {
text-align: left;
}
.collapsed-msg {
height: 70px;
}
.collapsed-msg > span {
padding: 0 60px 0 15px;
}
.expanded-msg {
background: none;
padding: 12px 32px 42px 32px;
height: 176px;
text-align: center;
}
.expanded-msg > span:not(.header) {
margin: 10px auto auto auto;
padding-bottom: 10px;
}
.expanded-msg > a.subscribe-cta {
display: block;
margin: auto;
}
#cta {
height: 70px;
}
#cta::before {
height: 42px;
}
#cta::before,
#cta::after {
margin-top: 1em;
}
}
</style>
</head>
<body>
<div id="growler-wrapper">
<div class="collapsed-msg">
<span>Become a <em>New Yorker</em> subscriber. <br /><span class="save-text"> Get 12 weeks for $12.</span>
<a href="%%CLICK_URL_UNESC%%%%DEST_URL%%" target="_blank" class="subscribe-cta"> Subscribe now.</a>
</span>
</div>
<div class="expanded-msg">
<span class="header">Go beyond the headlines.</span>
<span class="sales-copy">Get <em>The New Yorker</em> for $1 a week, plus a free tote.</span>
<a href="%%CLICK_URL_UNESC%%%%DEST_URL%%" target="_blank" class="subscribe-cta">Subscribe Now</a>
<span class="signin-copy">Already have an account? <a href="javascript:void(0);return;" target="_top">Sign in</a>.</span>
</div>
<div id="cta">
<span>Collapse</span>
<button class="close"></button>
</div>
</div>
<script>
(function () {
'use strict';
var growlerFrame = window.frameElement;
var growler = document.getElementById('growler-wrapper');
var collapsedMsgCta = growler.querySelector('.collapsed-msg .subscribe-cta');
var saveText = growler.querySelector('.save-text');
var signInCta = growler.querySelector('.signin-copy a');
var cta = document.getElementById('cta');
var ctaMsg = cta.querySelector('span');
var growlerCloseBtn = document.querySelector('.close');
// Constants
var INIT = {
load: 'load-init',
remove: 'remove-init',
slideInOffset: 0.2
};
var CTA_MESSAGE = {
collapsed: ctaMsg.innerHTML,
expanded: 'Collapse'
};
var GROWLER_HEIGHT = {
hidden: '0px',
collapsed: '70px',
expanded: '320px',
waitTime: 500
};
var DISMISSABLE_PATHS = ['crossword'];
var GROWLER_HIDE_DEPTH = 70;
// Methods
var toggle = function (e, init) {
e = e || undefined;
init = init || undefined;
if (e) {
e.cancelBubble = true;
}
if (!growler.classList.contains('expanded')) {
growler.classList.add('expanded');
ctaMsg.innerHTML = CTA_MESSAGE.expanded;
} else if (growler.classList.contains('expanded')) {
growler.classList.remove('expanded');
ctaMsg.innerHTML = CTA_MESSAGE.collapsed;
}
msgToggle();
if (!init) {
frameToggle();
}
};
var msgToggle = function () {
if (growler.classList.contains('expanded')) {
collapsedMsgCta.classList.add('hide');
saveText.classList.add('hide');
} else {
collapsedMsgCta.classList.remove('hide');
saveText.classList.remove('hide');
}
};
var frameToggle = function (init) {
if (!growlerFrame) return;
init = init || undefined;
var gfParent = growlerFrame.parentElement;
if (init) {
if (init === INIT.load) {
// Start hidden
growlerFrame.style.setProperty('height', GROWLER_HEIGHT.hidden, 'important');
gfParent.style.setProperty('height', GROWLER_HEIGHT.hidden, 'important');
growlerFrame.classList.add('init');
growler.classList.add('slide-in');
toggle(null, INIT.load);
} else if (init === INIT.remove) {
growlerFrame.style.setProperty('height', GROWLER_HEIGHT.expanded, 'important');
gfParent.style.setProperty('height', GROWLER_HEIGHT.expanded, 'important');
growlerFrame.classList.remove('init');
growler.classList.remove('slide-in');
}
} else if (growler.classList.contains('expanded')) {
growlerFrame.style.setProperty('height', GROWLER_HEIGHT.expanded, 'important');
gfParent.style.setProperty('height', GROWLER_HEIGHT.expanded, 'important');
} else {
// Wait for animation to finish
var cllpsTime = setTimeout(function () {
growlerFrame.style.setProperty('height', GROWLER_HEIGHT.collapsed, 'important');
gfParent.style.setProperty('height', GROWLER_HEIGHT.collapsed, 'important');
clearTimeout(cllpsTime);
}, GROWLER_HEIGHT.waitTime);
}
};
var linkNoBubble = function (e) {
e.cancelBubble = true;
};
var scrollMonitor = function (e) {
var shouldSlideIn = window.top.scrollY >= Math.floor(window.top.innerHeight * INIT.slideInOffset);
if (growlerFrame.classList.contains('init') && shouldSlideIn) {
frameToggle(INIT.remove);
window.top.removeEventListener('scroll', scrollMonitor);
}
};
function shouldUseDismissableGrowler() {
var locationPath = window.location.href.split('/');
return locationPath[3] && DISMISSABLE_PATHS.indexOf(locationPath[3]) !== -1;
}
function hideGrowler() {
growler.classList.add("hide-with-animation");
}
/* Hide Growler at X article depth */
function hideGrowlerAtArticleDepth(e) {
var scrollPercent = getArticleBodyScrollPercent();
if (scrollPercent >= GROWLER_HIDE_DEPTH) {
hideGrowler();
window.top.removeEventListener('scroll', hideGrowlerAtArticleDepth);
}
}
function getArticleBodyScrollPercent() {
var documentElement = window.parent.document.documentElement;
var body = window.parent.document.body;
var articleBody = window.top.document.querySelector('#articleBody');
if (articleBody && articleBody.scrollHeight > documentElement.clientHeight) {
// Distance in pxs between the article body and the body element
var articleBodyOffsetTop = Math.round(articleBody.getBoundingClientRect().top + documentElement.scrollTop);
return (documentElement.scrollTop - articleBodyOffsetTop || body.scrollTop - articleBodyOffsetTop) / (articleBody.scrollHeight - documentElement.clientHeight) * 100;
}
return null;
}
// Bind Events
if (shouldUseDismissableGrowler()) {
cta.classList.add('hidden-cta');
cta.addEventListener('click', function() {
growler.style.display = "none";
});
} else {
cta.addEventListener('click', toggle);
if (growlerCloseBtn) {
growlerCloseBtn.style.display = 'none';
}
}
growler.addEventListener('click', toggle);
growler.querySelectorAll('a').forEach(function (link) {
link.addEventListener('click', linkNoBubble);
});
signInCta.addEventListener('click', function (e) {
e.preventDefault();
window.top.location.href = `https://account.newyorker.com/?retURL=${window.top.location.href}&reload=true&utm=AMS_NYR_MOBILE_PAYWALL_METER_ARTICLE_1_SignIn_PanelB`;
});
window.top.addEventListener('scroll', scrollMonitor);
window.top.addEventListener('scroll', hideGrowlerAtArticleDepth);
frameToggle(INIT.load);
console.log('***GROWLER_WORKING_SLIDE_IN_ANIMATION***');
if (!growlerFrame) console.warn('GROWLER WARNING: Growler does not have access to parent frame.');
// ANALYTICS
try {
window.top.dataLayer.push({'event': 'growler impression','growler-barrier-text': 'You have 3 free articles left this month.'});
} catch (e) {
console.error('Error in growler ad tracking calls', e);
}
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment