利用css3的3d效果,实现一个简单的翻页器:

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>3d翻页动画</title>
</head>
<style>
    .box {
        position: relative;
        margin: 100px auto;
        width: 160px;
        height: 200px;
        background: #f0ddca;
        transform: perspective(600px);              /*开启透视*/
        transform-style: preserve-3d;               /*保持子元素的3d效果*/
        box-shadow: 2px 5px 6px #666666;
    }
     .page {
         position: absolute;
         width: 100%;
         height: 100%;
         transition: all .8s ease-in-out;
         transform-origin: left;
         transform-style: preserve-3d;          /*保持子元素的3d效果*/

     }
    .next_page {
        transform: rotateY(-180deg);
    }
    .pre_page {
        transform: rotateY(0deg);
    }
    .box .page .front,.back {
        width: 100%;
        height: 100%;
        position: absolute;
        backface-visibility: hidden;            /*元素背面朝屏幕时背景不可见*/

    }
    .box .page .front {
        background: #0a60bd;
    }
    .box .page .back {
        background: #0fbf60;
        transform: rotateY(180deg);
    }
</style>
<script>
  window.onload = () => {
    const next = document.querySelector(".next");
    const pre = document.querySelector(".pre");
    const pages = document.querySelectorAll(".page")
    let index = 0;
    next.addEventListener('click', () => {
      if(index <2) {
        pages[index].classList.remove("pre_page")
        pages[index].classList.add("next_page")
        index ++
      }
    })
    pre.addEventListener('click', () => {
      if(index >0) {
        pages[index -1].classList.add("pre_page")
        pages[index -1].classList.remove("next_page")
        index --
      }
    })
  }
</script>
<body>
    <div class="box">
        <div class="page">
            <div class="front"></div>
            <div class="back"></div>
        </div>
        <div class="page">
            <div class="front"></div>
            <div class="back"></div>
        </div>
        <div class="page">
            <div class="front"></div>
            <div class="back"></div>
        </div>
    </div>
    <button class="pre">上一页</button>
    <button class="next">下一页</button>
</body>
</html>
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐