Hey guys,
Ive been messing around with a little coding not the best at it. Kinda self taught, ive been a little stuck with a little problem.
Ive created a pop up to try drum up some subscribers, i got it to work. But only problem i am having now is that when i try click the (x) to close it. It wont work. Ive also applied a .js where you click outside the pop up that it will close But that wont work either.
Can someone please have a quick look and see what i done wrong? I would greatly appreciate it. I can upload the website its on a duplicate while testing.
This is the .css
/* Main popup styling */
#customNewsletter {
position: fixed !important;
top: 0 !important;
left: 0 !important;
width: 100% !important;
height: 100vh !important;
display: flex !important;
justify-content: center !important;
align-items: center !important;
z-index: 9999 !important;
}
/* Popup container styling */
.customNewsletter__container {
width: 90%;
max-width: 700px;
background-color: #181e40; /* Dark background */
border-radius: 15px;
box-shadow: 0 8px 30px rgba(255, 255, 255, 0.4); /* Light shadow */
padding: 40px;
border: 3px solid #fff;
text-align: center;
position: relative;
overflow-y: auto;
max-height: 50vh; /* Limit height to fit smaller screens */
}
/* Close button styling */
#customNewsletter__close {
position: absolute; /* Ensure it's positioned relative to a parent */
top: 15px;
right: 15px;
font-weight: bold;
border: none; /* No border for cleaner look */
background-color: transparent; /* Keep it transparent, but ensure visibility */
font-size: 2rem;
color: #fff; /* Visible color */
cursor: pointer;
z-index: 10; /* Ensure it's on top of other elements */
}
/* Logo styling */
.customNewsletter__logo {
display: flex;
justify-content: center; /* Center the logo */
align-items: center; /* Vertically center the logo within its container */
margin-bottom: 25px;
width: 100%; /* Make the container take full width */
}
.customNewsletter__logo img {
width: 65%; /* Scale image to full width */
max-width: none; /* Remove the max-width limit */
height: auto; /* Keep aspect ratio */
}
/* Text styling */
.customNewsletter__text {
margin-top: 20px;
padding: 20px 0;
font-weight: 600;
font-size: 1.9rem;
color: #ffffff; /* Light color for the text */
}
/* Questionnaire styling */
#question-box {
display: flex;
flex-direction: column;
gap: 25px;
margin-top: 20px;
margin-bottom: 40px;
}
.answer-button {
background-color: #ffffff; /* Light background */
color: #181e40; /* Dark text color */
border: none;
padding: 20px;
font-size: 1.8rem;
font-weight: 600;
border-radius: 10px;
cursor: pointer;
box-shadow: 0 4px 20px rgba(255, 255, 255, 0.15); /* Light shadow */
transition: background-color 0.3s ease, transform 0.2s ease;
}
.answer-button:hover {
background-color: #e0e4f2; /* Lighter background for hover */
transform: scale(1.05);
position: center;
}
/* Email Subscription Form styling */
#email-box, #confirmation-box {
margin-top: 20px;
}
.custom__newsletter-form {
width: 100%;
max-width: 500px;
}
.newsletter-form__field-wrapper {
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
}
.field {
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
}
.field__input {
width: 100%;
padding: 18px;
margin-bottom: 20px;
border: 2px solid #ffffff; /* Light border */
border-radius: 8px;
font-size: 1.2rem;
box-shadow: 0 2px 10px rgba(255, 255, 255, 0.1); /* Light shadow */
}
.newsletter-form__button {
padding: 15px 30px;
width: 100%;
background-color: #ffffff; /* Light background */
color: #181e40; /* Dark text color */
border: none;
border-radius: 8px;
font-size: 1.2rem;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.2s ease;
}
.newsletter-form__button:hover {
background-color: #e0e4f2; /* Lighter hover background */
transform: scale(1.05);
}
/* Confirmation message styling */
#confirmation-box {
text-align: center;
padding: 25px;
font-size: 1.4rem;
color: #ffffff; /* Light text */
}
/* Responsive Styles */
@media only screen and (max-width: 768px) {
.customNewsletter__container {
width: 95%;
max-width: 90%;
}
#customNewsletter__close {
font-size: 2.5rem;
}
.answer-button {
font-size: 1.6rem;
}
}
@media only screen and (min-width: 600px) {
.customNewsletter__container {
width: 80%;
padding: 30px;
}
}
@media only screen and (min-width: 900px) {
.customNewsletter__container {
max-width: 600px;
padding: 50px;
}
.customNewsletter__text {
font-size: 1.8rem;
}
.answer-button {
font-size: 1.5rem;
padding: 18px;
}
.newsletter-form__button {
width: auto;
padding: 15px 40px;
}
}
This is the .js
document.addEventListener("DOMContentLoaded", function () {
const popup = document.getElementById("customNewsletter");
const answerButtons = document.querySelectorAll(".answer-button");
const emailBox = document.getElementById("email-box");
const questionBox = document.getElementById("question-box");
const responseText = document.getElementById("response-text");
const confirmationBox = document.getElementById("confirmation-box");
const closePopup = document.getElementById("customNewsletter__close");
// Check for cookie before showing popup
if (!Cookies.get('popup_shown')) {
setTimeout(() => {
popup.style.display = "flex"; // Show the popup after a delay
}, 30000); // 30-second delay
}
// Handle answer button click
answerButtons.forEach(button => {
button.addEventListener("click", function () {
const response = button.getAttribute("data-response");
responseText.textContent = response;
questionBox.style.display = "none";
emailBox.style.display = "block";
});
});
// Handle email form submission
const form = document.querySelector('.custom__newsletter-form');
if (form) {
form.addEventListener("submit", function (event) {
event.preventDefault();
emailBox.style.display = "none";
confirmationBox.style.display = "block";
// Set the cookie to indicate the popup has been shown
Cookies.set('popup_shown', 'true', { expires: 30 }); // Set it for 30 days for testing
console.log("Popup shown cookie set"); // Debugging step
});
}
// Close the popup if close button exists
if (closePopup) {
closePopup.addEventListener("click", function (e) {
console.log("Close button clicked"); // Debugging step
popup.style.display = "none";
});
} else {
console.warn("Close button not found");
}
// Close the popup on outside click
if (popup) {
popup.addEventListener("click", function (event) {
if (event.target === popup) {
popup.style.display = "none";
}
});
}
});
.liquid.
.liquid
{{ "custom-subscriber-popup.css" | asset_url | stylesheet_tag }}
# {{ section.settings.heading }}
{{ section.settings.paragraph }}
What's your secret for looking PHRESH?
{% form 'customer', class: 'custom__newsletter-form' %}
{% endform %}
Thank you! A code will be emailed to you shortly.
{{ 'custom-subscriber-popup.js' | asset_url | script_tag }}
{% schema %}
{
"name": "Pop Up",
"settings": [
{
"type": "image_picker",
"id": "logo",
"label": "Logo",
"info": "Logo goes here"
},
{
"type": "text",
"id": "heading",
"label": "Heading",
"default": "Heading Here",
"placeholder": "Place Your Heading Here",
"info": "Header for your popup"
},
{
"type": "textarea",
"id": "paragraph",
"label": "Paragraph",
"default": "Tell your customer why they should subscribe.",
"placeholder": "Subscribe now to hear about great deals!",
"info": "Paragraph area for pop up"
}
],
"presets": [
{
"name": "Pop Up"
}
]
}
{% endschema %}
{% javascript %}
document.addEventListener("DOMContentLoaded", function () {
const popup = document.getElementById("customNewsletter");
const answerButtons = document.querySelectorAll(".answer-button");
const emailBox = document.getElementById("email-box");
const questionBox = document.getElementById("question-box");
const responseText = document.getElementById("response-text");
const confirmationBox = document.getElementById("confirmation-box");
const closePopup = document.getElementById("customNewsletter__close");
// Show the popup on page load
popup.style.display = "block";
// Handle answer button click
answerButtons.forEach(button => {
button.addEventListener("click", function () {
const response = button.getAttribute("data-response");
responseText.textContent = response;
questionBox.style.display = "none";
emailBox.style.display = "block";
});
});
// Handle email form submission
const form = document.querySelector('.custom__newsletter-form');
form.addEventListener("submit", function (event) {
event.preventDefault();
// AJAX can be used here to submit the form without reloading the page
emailBox.style.display = "none";
confirmationBox.style.display = "block";
// Optionally, set a cookie to prevent the popup from showing again
Cookies.set('popup_shown', 'true', { expires: 0.0833 }); // 2 hours
});
// Close the popup
closePopup.addEventListener("click", function () {
popup.style.display = "none";
});
});
{% endjavascript %}