Certainly! Below is the fully modified Wix Velo code that incorporates all your requested changes:
-
Responsive Design: The #answer1 element is styled to adjust its width based on the device screen size (desktop, tablet, phone) using CSS media queries. This ensures that the results field appears correctly on all screen sizes without overlapping or blocking other elements.
-
Season Emojis: Each season is assigned a distinct emoji, enhancing the visual appeal and clarity of the information displayed.
-
Interactive Visibility: Both #section7 and #answer1 are initially hidden. When the #calcnow button is clicked, both elements appear simultaneously, providing a seamless user experience.
-
Wix Velo Compatibility: The code is structured to be fully compatible with Wix Velo, ensuring smooth integration and functionality within your Wix site.
Here’s the comprehensive modified code:
// Import necessary Wix modules
import wixData from 'wix-data';
import wixWindow from 'wix-window'; // Included for potential future use
// When the page is ready
$w.onReady(function () {
// Initially hide #section7 and #answer1
$w("#section7").hide();
$w("#answer1").hide();
// Populate Year Dropdown
populateYearDropdown();
// Populate Month Dropdown
populateMonthDropdown();
// Set up event listeners for the dropdowns
$w("#dropdown1year, #dropdown2mounth").onChange(updateDays);
// Handle the "Calculate" button click
$w("#calcnow").onClick(handleCalculate);
// Initial update of days dropdown
updateDays();
});
/**
* Populates the Year Dropdown with options from current year down to 1900.
*/
function populateYearDropdown() {
const currentYear = new Date().getFullYear();
const yearOptions = [];
for (let year = currentYear; year >= 1900; year--) {
yearOptions.push({ label: year.toString(), value: year.toString() });
}
$w("#dropdown1year").options = yearOptions;
}
/**
* Populates the Month Dropdown with month names and corresponding values.
*/
function populateMonthDropdown() {
const monthOptions = [
{ label: "January - 1", value: "1" },
{ label: "February - 2", value: "2" },
{ label: "March - 3", value: "3" },
{ label: "April - 4", value: "4" },
{ label: "May - 5", value: "5" },
{ label: "June - 6", value: "6" },
{ label: "July - 7", value: "7" },
{ label: "August - 8", value: "8" },
{ label: "September - 9", value: "9" },
{ label: "October - 10", value: "10" },
{ label: "November - 11", value: "11" },
{ label: "December - 12", value: "12" }
];
$w("#dropdown2mounth").options = monthOptions;
}
/**
* Updates the Days Dropdown based on selected Year and Month.
*/
function updateDays() {
const birthYear = parseInt($w("#dropdown1year").value);
const birthMonth = parseInt($w("#dropdown2mounth").value);
if (!birthYear || !birthMonth) {
// If year or month is not selected, clear days dropdown
$w("#dropdown3day").options = [];
return;
}
const lastDay = new Date(birthYear, birthMonth, 0).getDate();
const daysOptions = [];
for (let day = 1; day <= lastDay; day++) {
daysOptions.push({ label: day.toString(), value: day.toString() });
}
$w("#dropdown3day").options = daysOptions;
}
/**
* Handles the Calculate button click event.
*/
function handleCalculate() {
// Get selected values
const birthYear = parseInt($w("#dropdown1year").value);
const birthMonth = parseInt($w("#dropdown2mounth").value) - 1; // JavaScript months are 0-based
const birthDay = parseInt($w("#dropdown3day").value);
// Validate inputs
if (!birthYear || isNaN(birthMonth) || !birthDay) {
console.error("Please select all date fields");
// Optionally, show a message to the user
return;
}
// Calculate age
const today = new Date();
const birthDate = new Date(birthYear, birthMonth, birthDay);
let years = today.getFullYear() - birthDate.getFullYear();
let months = today.getMonth() - birthDate.getMonth();
let days = today.getDate() - birthDate.getDate();
if (days < 0) {
const lastMonthDate = new Date(today.getFullYear(), today.getMonth(), 0);
days = lastMonthDate.getDate() + days;
months--;
}
if (months < 0) {
months += 12;
years--;
}
// Additional calculations
const totalMonths = years * 12 + months;
const totalDays = Math.floor((today - birthDate) / (1000 * 60 * 60 * 24));
const totalWeeks = Math.floor(totalDays / 7);
const daysToNextBirthday = calculateDaysToNextBirthday(birthDate, today);
const nextBirthdayDate = new Date(today.getFullYear(), birthMonth, birthDay);
const nextBirthdayDay = nextBirthdayDate.toLocaleDateString('en-US', { weekday: 'long' });
// Zodiac, Day of the week, and Season
const zodiacSign = getZodiacSign(birthDay, birthMonth + 1);
const birthDayOfWeek = birthDate.toLocaleDateString('en-US', { weekday: 'long' });
const birthSeason = getSeason(birthMonth + 1);
const birthSeasonEmoji = getSeasonEmoji(birthSeason);
// Update the answer box content with modified HTML
$w("#answer1").html = generateHTMLContent({
years,
months,
days,
totalMonths,
totalWeeks,
totalDays,
hours: Math.floor(totalDays * 24),
daysToNextBirthday,
nextBirthdayDay,
zodiacSign,
birthDayOfWeek,
birthSeason,
birthSeasonEmoji
});
// Show the section and answer box
$w("#section7").show();
$w("#answer1").show();
}
/**
* Generates the HTML content with the provided data.
* @param {Object} data - The data to display.
* @returns {string} - The HTML string.
*/
function generateHTMLContent(data) {
return `
<style>
/* Reset default margins and paddings */
body, html, div {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Container Styles */
.main-container {
font-family: Bitter, serif;
color: #262627;
background-color: #F5F5F5;
padding: 20px;
width: 100%;
height: 648px;
overflow: auto;
display: flex;
justify-content: center;
align-items: center;
}
.content-wrapper {
display: grid;
grid-template-columns: 1fr;
gap: 30px;
width: 100%;
max-width: 900px;
}
/* Age Display */
.age-display {
text-align: center;
background: rgba(38, 38, 39, 0.03);
border-radius: 15px;
padding: 20px;
}
.age-display h1 {
font-size: 36px;
margin-bottom: 10px;
}
.age-display .age-values {
font-size: 48px;
font-weight: bold;
margin: 0;
}
.age-display .age-values span {
font-size: 24px;
font-weight: normal;
}
/* Timeline Stats */
.timeline-stats {
background: rgba(38, 38, 39, 0.03);
border-radius: 15px;
padding: 20px;
text-align: center;
}
.timeline-stats h2 {
font-size: 24px;
margin-bottom: 25px;
}
.timeline-stats .stats-container {
display: flex;
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
gap: 20px;
}
.stat-item {
display: flex;
flex-direction: column;
align-items: center;
}
.stat-item .stat-number {
font-size: 32px;
font-weight: bold;
}
.stat-item .stat-label {
font-size: 16px;
}
/* Birthday Countdown */
.birthday-countdown {
background: rgba(38, 38, 39, 0.03);
border-radius: 15px;
padding: 20px;
text-align: center;
}
.birthday-countdown h2 {
font-size: 24px;
margin-bottom: 15px;
}
.birthday-countdown .countdown-days {
font-size: 48px;
font-weight: bold;
}
.birthday-countdown .countdown-text {
font-size: 20px;
}
.birthday-countdown .next-birthday {
font-size: 18px;
margin-top: 10px;
}
/* Birth Story */
.birth-story {
background: rgba(38, 38, 39, 0.03);
border-radius: 15px;
padding: 20px;
text-align: center;
}
.birth-story h2 {
font-size: 24px;
margin-bottom: 25px;
}
.birth-story .story-container {
display: flex;
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
gap: 20px;
}
.story-item {
display: flex;
flex-direction: column;
align-items: center;
}
.story-item .story-emoji {
font-size: 36px;
}
.story-item .story-title {
font-weight: bold;
font-size: 20px;
margin-top: 5px;
}
.story-item .story-description {
font-size: 20px;
}
/* Responsive Styles */
@media (min-width: 1025px) {
/* Desktop */
.main-container {
width: 1280px;
height: 648px;
}
}
@media (min-width: 768px) and (max-width: 1024px) {
/* Tablet */
.main-container {
width: 768px;
height: 648px;
}
}
@media (max-width: 767px) {
/* Phone */
.main-container {
width: 390px;
height: 648px;
}
/* Adjust font sizes for smaller screens */
.age-display h1 {
font-size: 28px;
}
.age-display .age-values {
font-size: 36px;
}
.timeline-stats h2,
.birthday-countdown h2,
.birth-story h2 {
font-size: 20px;
}
.timeline-stats .stat-item .stat-number {
font-size: 24px;
}
.timeline-stats .stat-item .stat-label {
font-size: 14px;
}
.birthday-countdown .countdown-days {
font-size: 36px;
}
.birthday-countdown .countdown-text,
.birthday-countdown .next-birthday {
font-size: 16px;
}
.birth-story .story-item .story-emoji {
font-size: 28px;
}
.birth-story .story-item .story-title {
font-size: 16px;
}
.birth-story .story-item .story-description {
font-size: 16px;
}
}
</style>
<div class="main-container">
<div class="content-wrapper">
<!-- Main Age Display -->
<div class="age-display">
<h1>Your Life Journey</h1>
<div class="age-values">
${data.years}<span> years </span>
${data.months}<span> months </span>
${data.days}<span> days </span>
</div>
</div>
<!-- Timeline Stats -->
<div class="timeline-stats">
<h2>Your Timeline</h2>
<div class="stats-container">
<div class="stat-item">
<div class="stat-number">${data.totalMonths}</div>
<div class="stat-label">Months</div>
</div>
<div class="stat-item">
<div class="stat-number">${data.totalWeeks}</div>
<div class="stat-label">Weeks</div>
</div>
<div class="stat-item">
<div class="stat-number">${data.totalDays}</div>
<div class="stat-label">Days</div>
</div>
<div class="stat-item">
<div class="stat-number">${data.hours}</div>
<div class="stat-label">Hours</div>
</div>
</div>
</div>
<!-- Birthday Countdown -->
<div class="birthday-countdown">
<h2>Next Birthday</h2>
<div>
<div class="countdown-days">${data.daysToNextBirthday}</div>
<div class="countdown-text">Days until your birthday</div>
<div class="next-birthday">
It will be on a <span style="font-weight: bold;">${data.nextBirthdayDay}</span>
</div>
</div>
</div>
<!-- Birth Story -->
<div class="birth-story">
<h2>Your Birth Story</h2>
<div class="story-container">
<!-- Zodiac Sign -->
<div class="story-item">
<div class="story-emoji">${getEmojiFromZodiac(data.zodiacSign)}</div>
<div class="story-title">Zodiac Sign</div>
<div class="story-description">${data.zodiacSign.split(' ')[0]}</div>
</div>
<!-- Born On -->
<div class="story-item">
<div class="story-emoji">📅</div>
<div class="story-title">Born On</div>
<div class="story-description">${data.birthDayOfWeek}</div>
</div>
<!-- Season -->
<div class="story-item">
<div class="story-emoji">${data.birthSeasonEmoji}</div>
<div class="story-title">Season</div>
<div class="story-description">${data.birthSeason}</div>
</div>
</div>
</div>
</div>
</div>
`;
}
/**
* Retrieves the emoji associated with a given season.
* @param {string} season - The season name.
* @returns {string} - The corresponding emoji.
*/
function getSeasonEmoji(season) {
const seasonEmojis = {
"Spring": "🌸",
"Summer": "☀️",
"Autumn": "🍁",
"Winter": "❄️"
};
return seasonEmojis[season] || "❓";
}
/**
* Retrieves the emoji associated with a given Zodiac sign.
* @param {string} zodiacWithSymbol - The Zodiac sign with its symbol.
* @returns {string} - The symbol part (emoji) of the Zodiac sign.
*/
function getEmojiFromZodiac(zodiacWithSymbol) {
// Assuming zodiacWithSymbol is in the format "ZodiacName ♀️" etc.
const parts = zodiacWithSymbol.split(' ');
if (parts.length >= 2) {
return parts[parts.length -1]; // Last part is the symbol
}
return "";
}
/**
* Calculates the number of days until the next birthday.
* @param {Date} birthDate - The user's birth date.
* @param {Date} today - Today's date.
* @returns {number} - Days until next birthday.
*/
function calculateDaysToNextBirthday(birthDate, today) {
let nextBirthday = new Date(today.getFullYear(), birthDate.getMonth(), birthDate.getDate());
if (nextBirthday < today) {
nextBirthday.setFullYear(today.getFullYear() + 1);
}
// Reset time to midnight to avoid partial days
nextBirthday.setHours(0,0,0,0);
today.setHours(0,0,0,0);
const diffTime = nextBirthday - today;
return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
}
/**
* Determines the Zodiac Sign based on day and month.
* @param {number} day - Birth day.
* @param {number} month - Birth month (1-12).
* @returns {string} - Zodiac sign with its symbol.
*/
function getZodiacSign(day, month) {
const zodiacSigns = [
{ name: "Capricorn", symbol: "♑️", endDay: 19 },
{ name: "Aquarius", symbol: "♒️", endDay: 18 },
{ name: "Pisces", symbol: "♓️", endDay: 20 },
{ name: "Aries", symbol: "♈️", endDay: 19 },
{ name: "Taurus", symbol: "♉️", endDay: 20 },
{ name: "Gemini", symbol: "♊️", endDay: 20 },
{ name: "Cancer", symbol: "♋️", endDay: 22 },
{ name: "Leo", symbol: "♌️", endDay: 22 },
{ name: "Virgo", symbol: "♍️", endDay: 22 },
{ name: "Libra", symbol: "♎️", endDay: 21 },
{ name: "Scorpio", symbol: "♏️", endDay: 21 },
{ name: "Sagittarius", symbol: "♐️", endDay: 21 },
{ name: "Capricorn", symbol: "♑️", endDay: 31 } // For month 12 Capricorn continuation
];
// month is 1-based
const index = month - 1;
if (day > zodiacSigns[index].endDay) {
return zodiacSigns[(index + 1) % 12].name + " " + zodiacSigns[(index +1) % 12].symbol;
} else {
return zodiacSigns[index].name + " " + zodiacSigns[index].symbol;
}
}
/**
* Determines the Season based on the month.
* @param {number} month - Birth month (1-12).
* @returns {string} - Season name.
*/
function getSeason(month) {
if (month >= 3 && month <= 5) return "Spring";
if (month >= 6 && month <= 8) return "Summer";
if (month >= 9 && month <= 11) return "Autumn";
return "Winter";
}
Key Modifications and Enhancements:
-
Responsive Design:
- CSS Media Queries: Included within the
<style> block inside the HTML content to adjust the width and font sizes based on the device's screen size (desktop, tablet, phone).
- Flexible Layouts: Utilized CSS Flexbox and Grid with
flex-wrap: wrap and gap properties to ensure that elements adapt fluidly without overlapping.
-
Season-Specific Emojis:
getSeasonEmoji Function: This function maps each season to a unique emoji.
- Dynamic Emoji Display: The season emoji is dynamically inserted into the HTML content based on the user's birth month.
-
Interactive Visibility:
- Initial Hiding: Both
#section7 and #answer1 are hidden when the page loads to provide a clean interface.
- Simultaneous Display: When the
#calcnow button is clicked, both elements appear together, displaying the results seamlessly.
-
Zodiac Signs with Emojis:
getEmojiFromZodiac Function: Extracts the emoji associated with each Zodiac sign to display alongside the sign's name.
-
Avoiding Overlaps:
- Consistent Height: The results field maintains a consistent height across devices to prevent content from overlapping or being cut off.
- Scrollable Content: The
overflow: auto property ensures that content remains accessible even if it exceeds the container's height on smaller screens.
-
Wix Velo Compatibility:
- Proper Element Referencing: Ensured that all elements like
#section7, #answer1, and #calcnow are correctly referenced using Wix Velo’s $w selector.
- Using
.html Property: The #answer1 element is expected to be an HTML component that can accept dynamic HTML content via the .html property.
-
Error Handling:
- Input Validation: The function checks if all date fields are selected before proceeding with calculations, preventing potential errors.
Usage Instructions:
-
HTML Component Configuration:
- Ensure that the
#answer1 element on your Wix page is an HTML component capable of rendering dynamic HTML content.
-
Section Setup:
- Make sure that
#section7 is correctly placed as a container for #answer1 and is structured to accommodate the dynamic content without layout issues.
-
Responsive Testing:
- After implementing the code, test the functionality across various devices (desktop, tablet, phone) to ensure that the responsive design behaves as expected.
-
Customization:
- Feel free to adjust the emojis or styles further to better match the aesthetic of your website or to cater to specific user preferences.
Final Notes:
This comprehensive modification ensures that your results field is visually appealing, functionally robust, and responsive across all devices. By incorporating distinct emojis for each season and ensuring synchronized visibility of the relevant sections upon calculation, the user experience is both engaging and intuitive. Additionally, the adherence to Wix Velo's standards guarantees seamless integration within your Wix site.
If you encounter any issues or need further customization, feel free to reach out for assistance!