Pular para o conteúdo principal

Tutorial: Construindo um calendário dinâmico com HTML, CSS e JavaScript

Calendário dinâmico

Calendário Dinâmico:

Aprenda a construir um calendário dinâmico com este tutorial detalhado.

Este projeto consiste em um calendário dinâmico muito lindo e funcional. 

Neste tutorial você vai aprender a fazer um Calendário dinâmico usando apenas HTML, CSS e JavaScript. 

Veja o Calendário dinâmico em funcionamento neste link Calendário dinâmico.

Veja o vídeo no YouTube:

Começando o Tutorial: 

1° Passo: Abra seu VS Code,  ou qualquer IDE da sua preferência e crie três pastas com os nomes... index.html, style.css e script.js.

Logo em seguida copie o código html abaixo e cole na pasta "index.html".



<!DOCTYPE html>

<html lang="pt-br" dir="ltr">

  <head>

    <meta charset="utf-8">

    <title>Calendário Dinâmico</title>

    <link rel="stylesheet" href="style.css">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200">

    <script src="script.js" defer></script>

  </head>

  <body>

    <div class="wrapper">

      <header>

        <p class="current-date"></p>

        <div class="icons">

          <span id="prev" class="material-symbols-rounded">chevron_left</span>

          <span id="next" class="material-symbols-rounded">chevron_right</span>

        </div>

      </header>

      <div class="calendar">

        <ul class="weeks">

          <li>Dom</li>

          <li>Seg</li>

          <li>Ter</li>

          <li>Qua</li>

          <li>Qui</li>

          <li>Sex</li>

          <li>Sab</li>

        </ul>

        <ul class="days"></ul>

      </div>

    </div>

    

  </body>

</html>

        

2° Passo: Copie o código abaixo e cole na pasta "style.css".



@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap');

*{

  margin: 0;

  padding: 0;

  box-sizing: border-box;

  font-family: 'Poppins', sans-serif;

}

body{

  display: flex;

  align-items: center;

  padding: 0 10px;

  justify-content: center;

  min-height: 100vh;

  background: #FFDEAD;

}

.wrapper{

  width: 450px;

  background: #fff;

  border-radius: 10px;

  box-shadow: 0 15px 40px rgba(0,0,0,0.12);

}

.wrapper header{

  display: flex;

  align-items: center;

  padding: 25px 30px 10px;

  justify-content: space-between;

}

header .icons{

  display: flex;

}

header .icons span{

  height: 38px;

  width: 38px;

  margin: 0 1px;

  cursor: pointer;

  color: #878787;

  text-align: center;

  line-height: 38px;

  font-size: 1.9rem;

  user-select: none;

  border-radius: 50%;

}

.icons span:last-child{

  margin-right: -10px;

}

header .icons span:hover{

  background: #f2f2f2;

}

header .current-date{

  font-size: 1.45rem;

  font-weight: 500;

}

.calendar{

  padding: 20px;

}

.calendar ul{

  display: flex;

  flex-wrap: wrap;

  list-style: none;

  text-align: center;

}

.calendar .days{

  margin-bottom: 20px;

}

.calendar li{

  color: #333;

  width: calc(100% / 7);

  font-size: 1.07rem;

}

.calendar .weeks li{

  font-weight: 500;

  cursor: default;

}

.calendar .days li{

  z-index: 1;

  cursor: pointer;

  position: relative;

  margin-top: 30px;

}

.days li.inactive{

  color: #aaa;

}

.days li.active{

  color: #fff;

}

.days li::before{

  position: absolute;

  content: "";

  left: 50%;

  top: 50%;

  height: 40px;

  width: 40px;

  z-index: -1;

  border-radius: 50%;

  transform: translate(-50%, -50%);

}

.days li.active::before{

  background: #FFDEAD;

}

.days li:not(.active):hover::before{

  background: #f2f2f2;

.className {

 

}

        

3° Passo: Copie o código abaixo e cole na pasta "script.js".



const daysTag = document.querySelector(".days"),

currentDate = document.querySelector(".current-date"),

prevNextIcon = document.querySelectorAll(".icons span");

// getting new date, current year and month

let date = new Date(),

currYear = date.getFullYear(),

currMonth = date.getMonth();

// storing full name of all months in array

const months = ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho",

              "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"];

const renderCalendar = () => {

    let firstDayofMonth = new Date(currYear, currMonth, 1).getDay(), // getting first day of month

    lastDateofMonth = new Date(currYear, currMonth + 1, 0).getDate(), // getting last date of month

    lastDayofMonth = new Date(currYear, currMonth, lastDateofMonth).getDay(), // getting last day of month

    lastDateofLastMonth = new Date(currYear, currMonth, 0).getDate(); // getting last date of previous month

    let liTag = "";

    for (let i = firstDayofMonth; i > 0; i--) { // creating li of previous month last days

        liTag += `${lastDateofLastMonth - i + 1}`;

    }

    for (let i = 1; i <= lastDateofMonth; i++) { // creating li of all days of current month

        // adding active class to li if the current day, month, and year matched

        let isToday = i === date.getDate() && currMonth === new Date().getMonth() 

                     && currYear === new Date().getFullYear() ? "active" : "";

        liTag += `${i}`;

    }

    for (let i = lastDayofMonth; i < 6; i++) { // creating li of next month first days

        liTag += `${i - lastDayofMonth + 1}`

    }

    currentDate.innerText = `${months[currMonth]} ${currYear}`; // passing current mon and yr as currentDate text

    daysTag.innerHTML = liTag;

}

renderCalendar();

prevNextIcon.forEach(icon => { // getting prev and next icons

    icon.addEventListener("click", () => { // adding click event on both icons

        // if clicked icon is previous icon then decrement current month by 1 else increment it by 1

        currMonth = icon.id === "prev" ? currMonth - 1 : currMonth + 1;

        if(currMonth < 0 || currMonth > 11) { // if current month is less than 0 or greater than 11

            // creating a new date of current year & month and pass it as date value

            date = new Date(currYear, currMonth, new Date().getDate());

            currYear = date.getFullYear(); // updating current year with new date year

            currMonth = date.getMonth(); // updating current month with new date month

        } else {

            date = new Date(); // pass the current date as date value

        }

        renderCalendar(); // calling renderCalendar function

    });

});

        
4° Passo: Testar o Projeto! 
Agora, com os três arquivos (index.html, style.css, e script.js) criados, você pode abrir o index.html no seu navegador e ver seu projeto funcionando perfeitamente!

Comentários

Mais vistas

Tutorial: Criando uma Animação de Roleta de Prêmios usando HTML, CSS e JavaScript

Animação Roleta de Prêmios: Descubra como criar uma animada roleta de prêmios com este tutorial detalhado. Este projeto consiste em uma Animação,  uma roleta

Autocode: Significado e Funcionalidade

O Que é Autocode? O Autocode é amplamente reconhecido como uma das primeiras linguagens de programação de computadores e um marco histórico no desenvolvimento

Os Melhores Sites para Programar de Graça: Aprenda e Pratique Online

A programação é uma habilidade indispensável no mercado atual, seja para desenvolver websites, criar aplicações ou trabalhar com dados. Felizmente, existem muitas plataformas gratuitas
Este site utiliza cookies para garantir que você obtenha a melhor experiência em nosso site. Saiba mais