简易web-slide

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="./index.css">
</head>
<body>
  <div class="container">
    <div class="slide" id="1">1</div>
    <div class="slide" id="2">2</div>
    <div class="slide" id="3">3</div>
    <div class="slide" id="4">4</div>
    <div class="slide" id="5">5</div>
    <div class="slide" id="6">6</div>
  </div>
  <script src="./a.js"></script>
</body>
</html>
.container {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
}
.slide {
  width: 100%;
  height: 100%;
  font-size: 50px;
  text-align: center;
}
@keyframes zoomIn {
  from {
    opacity: 0;
    -webkit-transform: scale3d(0.3, 0.3, 0.3);
    transform: scale3d(0.3, 0.3, 0.3);
  }

  50% {
    opacity: 1;
  }
}

.zoomIn {
  animation: zoomIn 0.5s;
}
let num = 1;
let total = 6;
function page(num) {
  window.location.hash = '#' + num;
  document.getElementById(num).classList.toggle('zoomIn');
  setTimeout(() => {
    document.getElementById(num).classList.toggle('zoomIn');
  }, 500);
}
function prev() {
  if (num <= 1) {
    return
  }
  num -= 1;
  page(num);
}
function next() {
  if (num >= total) {
    return
  }
  num += 1;
  page(num);
}
window.addEventListener('keydown', (e) => {
  // console.log(e);
  if (e.code === 'ArrowDown' || e.code === 'ArrowRight') {
    next();
  }
  if (e.code === 'ArrowUp' || e.code === 'ArrowLeft') {
    prev();
  }
});

猜你喜欢

转载自www.cnblogs.com/dkplus/p/8986894.html