Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

html - I am making a Calendar and facing a problem with the functionality in Javascript

I am using the arrows(present in the image) to either move to the previous or next month. I want to do this using the dates of the previous month and the next month (which are in gray in the image). I want this to run dynamically, but I can do it once only. Can anyone help me with this? (The image is in the link here ->Calendar)

Here is the code:

HTML:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8" />
    <title>Calendar</title>
    <link rel="stylesheet" href="./styles.css" />
    <link
      rel="stylesheet"
      href="https://use.fontawesome.com/releases/v5.15.1/css/all.css"
      integrity="sha384-vp86vTRFVJgpjF9jiIGPEEqYqlDwgyBgEF109VFjmqGmIY/Y4HV4d3Gp2irVfcrp"
      crossorigin="anonymous"
    />
  </head>
  <body>
    <div class="container">
      <div class="calendar">
        <div class="month">
          <i class="fas fa-chevron-left prev"></i>
          <div class="date">
            <h2></h2>
            <p></p>
          </div>
          <i class="fas fa-chevron-right next"></i>
        </div>
        <div class="days">
          <div>Sun</div>
          <div>Mon</div>
          <div>Tue</div>
          <div>Wed</div>
          <div>Thu</div>
          <div>Fri</div>
          <div>Sat</div>
        </div>
        <div class="dates"></div>
      </div>
      <div class="event">
        <h2></h2>
        <input
          type="text"
          name="newEvent"
          placeholder="Add an Event"
          autocomplete="off"
        />
      </div>
    </div>
  </body>
  <script src="calendar.js"></script>
</html>

Javascript:

const date = new Date();
const renderCalendar = () => {
  date.setDate(1);
  const month = date.toLocaleString("default", {
    month: "long",
    year: "numeric"
  });
  const daysofMonth = document.querySelector(".dates");
  const lastDay = new Date(
    date.getFullYear(),
    date.getMonth() + 1,
    0
  ).getDate();

  const prevmonthLastDay = new Date(
    date.getFullYear(),
    date.getMonth(),
    0
  ).getDate();
  const firstDayIndex = date.getDay();
  const lastDayIndex = new Date(
    date.getFullYear(),
    date.getMonth() + 1,
    0
  ).getDay();
  const nextDays = 7 - lastDayIndex - 1;

  document.querySelector(".date p").innerHTML = new Date().toDateString();
  document.querySelector(".date h2").innerHTML = month;

  let days = " ";
  for (let x = firstDayIndex; x > 0; x--) {
    days += `<div class="prev-date">${prevmonthLastDay - x + 1}</div>`;
  }

  for (let i = 1; i <= lastDay; i++) {
    i === new Date().getDate() && date.getMonth() === new Date().getMonth()
      ? (days += `<div class="today">${i}</div>`)
      : (days += `<div>${i}</div>`);
  }

  if (nextDays >= 1) {
    for (let j = 1; j <= nextDays; j++) {
      days += `<div class="next-date">${j}</div>`;
      daysofMonth.innerHTML = days;
    }
  } else {
    for (let k = 0; k <= nextDays; k++) {
      days += `<div class="invisible"></div>`;
      daysofMonth.innerHTML = days;
    }
  }

};

function prevMonth(){
    date.setMonth(date.getMonth() - 1);
     renderCalendar();
}

function nextMonth(){
renderCalendar();
    date.setMonth(date.getMonth() + 1);
     renderCalendar();
}



document.querySelector(".prev").addEventListener("click", prevMonth);

document.querySelector(".next").addEventListener("click", nextMonth);

document.querySelector(".date p").addEventListener("click", () => {
  date.setMonth(new Date().getMonth());
  date.setYear(new Date().getFullYear());
  renderCalendar();
});

document.querySelector(".dates").addEventListener("click", event => {
  const monthAndYear = document.querySelector(".date h2").innerHTML;
  event.target.innerHTML == new Date().getDate() &&
  date.getMonth() === new Date().getMonth()
    ? (document.querySelector(".event h2").innerHTML = `Today`)
    : (document.querySelector(".event h2").innerHTML = `<h2>${event.target
        .innerHTML +
        " " +
        monthAndYear}</h2>`);
});



renderCalendar();

  for (
    var i = 0;
    i < document.getElementsByClassName("next-date").length;
    i++
  ) {
console.log( this.document
      .getElementsByClassName("next-date")
      [i]);
    this.document
      .getElementsByClassName("next-date")
      [i].addEventListener("click",nextMonth);
  }

  for (
    var i = 0;
    i < document.getElementsByClassName("prev-date").length;
    i++
  ) {
    document
      .getElementsByClassName("prev-date")
      [i].addEventListener("click",prevMonth);
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...