Часы JS + CSS - пример html js css



Книга Часы JS + CSS

Часы JS + CSS (HTML код)


<div class="container">
		<div class="clock">
			<div class="clock__item clock__year">
				<div class="clock__year--item clock__year--01"></div>
				<div class="clock__year--item clock__year--02"></div>
				<div class="clock__year--item clock__year--03"></div>
				<div class="clock__year--item clock__year--04"></div>
				<div class="clock__year--item clock__year--05"></div>
				<div class="clock__year--item clock__year--06"></div>
				<div class="clock__year--item clock__year--07"></div>
				<div class="clock__year--item clock__year--08"></div>
			</div>
			<div class="clock__item clock__month">
				<div class="clock__month--item clock__month--01"></div>
				<div class="clock__month--item clock__month--02"></div>
				<div class="clock__month--item clock__month--03"></div>
				<div class="clock__month--item clock__month--04"></div>
				<div class="clock__month--item clock__month--05"></div>
				<div class="clock__month--item clock__month--06"></div>
			</div>
			<div class="clock__item clock__day-numbers">
				<div class="clock__day-numbers--item clock__day-numbers--01"></div>
				<div class="clock__day-numbers--item clock__day-numbers--02"></div>
				<div class="clock__day-numbers--item clock__day-numbers--03"></div>
				<div class="clock__day-numbers--item clock__day-numbers--04"></div>
			</div>
			<div class="clock__item clock__day-letters">
				<div class="clock__day-letters--item clock__day-letters--01"></div>
				<div class="clock__day-letters--item clock__day-letters--02"></div>
				<div class="clock__day-letters--item clock__day-letters--03"></div>
				<div class="clock__day-letters--item clock__day-letters--04"></div>
				<div class="clock__day-letters--item clock__day-letters--05"></div>
				<div class="clock__day-letters--item clock__day-letters--06"></div>
			</div>
			<div class="clock__item clock__analog">
					<div class="clock__analog--hours"></div>
					<div class="clock__analog--minutes"></div>
					<div class="clock__analog--seconds"></div>
				</div>
	</div>
</div>

Часы JS + CSS (CSS код)



@import url("https://fonts.googleapis.com/css2?family=Overpass:wght@900&display=swap");
*::after,
*::before,
* {
  margin: 0;
  padding: 0;
  box-sizing: inherit;
}

body {
  box-sizing: border-box;
  background-color: #E0E0E0;
  font-family: "Overpass", sans-serif;
}

.container {
  width: 100%;
}

.clock {
  width: 30rem;
  height: 30rem;
  margin: 5rem auto;
  padding: 1rem;
  background-color: #333;
  border: 2rem solid white;
  border-radius: 25%;
  box-shadow: 2px 0px 10px 2px #b3b3b3, 0px 0px 8px 0px black inset;
  display: grid;
  grid-template-rows: fit-content(100%);
  grid-template-columns: fit-content(100%);
  align-items: start;
  justify-content: space-evenly;
  grid-gap: 1rem;
  grid-template-areas: ". year year year year ." "month month month num num num" "analog analog analog num num num" "analog analog analog day day day";
}
.clock__item {
  border: 1px solid black;
  box-shadow: -8px 0px 8px 0px black inset;
}
.clock__year {
  grid-area: year;
  font-size: 3.2rem;
  display: grid;
  justify-content: center;
  align-items: center;
  color: white;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(2, auto-fit);
  grid-template-areas: "one two three four" "one two three four";
}
.clock__year--item {
  border: 1px solid rgba(0, 0, 0, 0.5);
  text-align: center;
}
.clock__year--01 {
  grid-area: one;
}
.clock__year--02 {
  grid-area: two;
}
.clock__year--03 {
  grid-area: three;
}
.clock__year--04 {
  grid-area: four;
}
.clock__year--05 {
  grid-area: one;
}
.clock__year--06 {
  grid-area: two;
}
.clock__year--07 {
  grid-area: three;
}
.clock__year--08 {
  grid-area: four;
}
.clock__month {
  grid-area: month;
  font-size: 3.2rem;
  display: grid;
  justify-content: center;
  align-items: center;
  color: white;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, auto-fit);
  grid-template-areas: "one two three" "one two three";
}
.clock__month--item {
  border: 1px solid rgba(0, 0, 0, 0.5);
  text-align: center;
}
.clock__month--01 {
  grid-area: one;
}
.clock__month--02 {
  grid-area: two;
}
.clock__month--03 {
  grid-area: three;
}
.clock__month--04 {
  grid-area: one;
}
.clock__month--05 {
  grid-area: two;
}
.clock__month--06 {
  grid-area: three;
}
.clock__day-numbers {
  grid-area: num;
  font-size: 5.8rem;
  display: grid;
  justify-content: center;
  align-items: center;
  color: white;
  justify-content: flex-end;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, auto-fit);
  grid-template-areas: "one two" "one two";
}
.clock__day-numbers--item {
  border: 1px solid rgba(0, 0, 0, 0.5);
  text-align: center;
}
.clock__day-numbers--01 {
  grid-area: one;
}
.clock__day-numbers--02 {
  grid-area: two;
}
.clock__day-numbers--03 {
  grid-area: one;
}
.clock__day-numbers--04 {
  grid-area: two;
}
.clock__day-letters {
  grid-area: day;
  font-size: 3rem;
  display: grid;
  justify-content: center;
  align-items: center;
  color: white;
  transform: translate(-1rem, -2rem);
  width: 110%;
  background-color: green;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, auto-fit);
  grid-template-areas: "one two three" "one two three";
}
.clock__day-letters--item {
  border: 1px solid rgba(0, 0, 0, 0.5);
  text-align: center;
}
.clock__day-letters--01 {
  grid-area: one;
}
.clock__day-letters--02 {
  grid-area: two;
}
.clock__day-letters--03 {
  grid-area: three;
}
.clock__day-letters--04 {
  grid-area: one;
}
.clock__day-letters--05 {
  grid-area: two;
}
.clock__day-letters--06 {
  grid-area: three;
}
.clock__analog {
  grid-area: analog;
  width: 10rem;
  height: 10rem;
  border-radius: 50%;
  position: relative;
}
.clock__analog--hours, .clock__analog--minutes, .clock__analog--seconds {
  position: absolute;
  top: 50%;
  transform: rotate(90deg);
  transform-origin: 100%;
  box-shadow: 2px 0px 5px 0px black;
}
.clock__analog--hours {
  width: 40%;
  left: 10%;
  height: 5px;
  background-color: white;
  border-radius: 2.5rem;
}
.clock__analog--minutes {
  width: 45%;
  left: 5%;
  height: 5px;
  background-color: white;
  border-radius: 2.5rem;
}
.clock__analog--seconds {
  width: 45%;
  left: 5%;
  height: 3px;
  background-color: red;
  border-radius: 2.5rem;
}


Часы JS + CSS (JS код)



const secondsDiv = document.querySelector('.clock__analog--seconds');
const minutesDiv = document.querySelector('.clock__analog--minutes');
const hoursDiv = document.querySelector('.clock__analog--hours');
const yearDiv01 = document.querySelector('.clock__year--01');
const yearDiv02 = document.querySelector('.clock__year--02');
const yearDiv03 = document.querySelector('.clock__year--03');
const yearDiv04 = document.querySelector('.clock__year--04');
const monthDiv01 = document.querySelector('.clock__month--01');
const monthDiv02 = document.querySelector('.clock__month--02');
const monthDiv03 = document.querySelector('.clock__month--03');
const dayLettersDiv01 = document.querySelector('.clock__day-letters--01');
const dayLettersDiv02 = document.querySelector('.clock__day-letters--02');
const dayLettersDiv03 = document.querySelector('.clock__day-letters--03');
const dayNumDiv01 = document.querySelector('.clock__day-numbers--01');
const dayNumDiv02 = document.querySelector('.clock__day-numbers--02');

function setDate() {
  const now = new Date();
  const seconds = now.getSeconds();
  const secondsDegrees = seconds * 360 / 60 + 90;
  secondsDiv.style.transform = `rotate(${secondsDegrees}deg)`;

  const minutes = now.getMinutes();
  const minutesDegrees = minutes * 360 / 60 + 90;
  minutesDiv.style.transform = `rotate(${minutesDegrees}deg)`;

  const hours = now.getHours();
  const hoursDegrees = hours * 360 / 12 + 90;
  hoursDiv.style.transform = `rotate(${hoursDegrees}deg)`;

  const year = now.getFullYear();
  yearDiv01.innerText = (year + '')[0];
  yearDiv02.innerText = (year + '')[1];
  yearDiv03.innerText = (year + '')[2];
  yearDiv04.innerText = (year + '')[3];


  const month = new Intl.DateTimeFormat('fr-FR', { month: 'short' }).format(now).split('.')[0].toUpperCase();
  monthDiv01.innerText = month[0];
  monthDiv02.innerText = month[1];
  monthDiv03.innerText = month[2];

  const dayLetters = new Intl.DateTimeFormat('fr-FR', { weekday: 'short' }).format(now).split('.')[0].toUpperCase();
  dayLettersDiv01.innerText = dayLetters[0];
  dayLettersDiv02.innerText = dayLetters[1];
  dayLettersDiv03.innerText = dayLetters[2];

  const dayNum = now.getUTCDate();
  if (dayNum.toString().length == 1) {
    dayNumDiv01.innerText = '0';
    dayNumDiv02.innerText = dayNum + '';
  } else {
    dayNumDiv01.innerText = (dayNum + '')[0];
    dayNumDiv02.innerText = (dayNum + '')[1];
  }
}

setInterval(setDate, 1000);
//# sourceURL=pen.js
    

Часы JS + CSS (Результат кода)

85   0  
    Ничего не найдено.

Добавить ответ:
Отменить.