js+css3的簡短代碼實現(xiàn)的翻書效果

效果:


1620704098763047.gif

代碼:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta name="viewport"
        content="width=device-width, viewport-fit=cover,initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
    <title>翻書效果</title>
</head>
<body>
<span class="pager" onclick="handlePrev()" id="prev">上一頁</span><span class="pager" id="next" onclick="handleNext()">下一頁</span>
<div class="book" id="book">
    <div class="cover page">
        <div class="piece front">封面</div>
        <div class="piece back">封面反面</div>
    </div>
    <div class="page inner">
        <div class="piece front">第一頁正面</div>
        <div class="piece back">第一頁反面</div>
    </div>
    <div class="page inner">
        <div class="piece front">第二頁正面</div>
        <div class="piece back">第二頁反面</div>
    </div>
    <div class="page inner">
        <div class="piece front">第三頁正面</div>
        <div class="piece back">第三頁反面</div>
    </div>
    <div class="page inner">
        <div class="piece front">第四頁正面</div>
        <div class="piece back">第四頁反面</div>
    </div>
</div>
</body>
<style type="text/css">
    * {
        margin: 0;
        padding: 0;
    }
    html,body {
        width: 100%;
        height: 100%;
    }
    body {
        background: #000;
    }
    .pager {
        margin-left: 10px;
        cursor: pointer;
        user-select: none;
        display: inline-block;
        margin-top: 20px;
        color: #fff;
    }
    .pager~.pager {
        margin-left: 40px;
    }
    .book {
  perspective: 800px;
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        right: 0;
        margin: auto;
        width: 306px;
        height: 470px;
        user-select: none;
        transition: transform .3s linear;
    }
    .showPager {
        transform: translateX(10%);
    }
    .cover {
        position: absolute;
        left: 0;
        top:0;
        width: 306px;
        height: 470px;
        z-index: 3;
    }
    .page {
        width:100%;
        height: 100%;
        position: absolute;
        left: 0;
        top: 0;
        transition: transform .4s linear;

        transform-style: preserve-3d;
        
    }
    .page .front {
        overflow: hidden;
        border-top-right-radius: 18px;
        border-bottom-right-radius: 18px;
        border-bottom-left-radius: 8px;
    }
    .page .back {
        overflow: hidden;
        border-top-left-radius: 18px;
        border-bottom-left-radius: 18px;
        border-bottom-right-radius: 8px;
    }
    .page .piece {
        position:absolute;
        left: 0;
        top:0;
        width: 100%;
        height: 100%;
        box-sizing: border-box;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    .page .front {
        z-index: 2;
        backface-visibility: hidden;
-webkit-backface-visibility: hidden;
    }
    .page img {
        width: 100%;
        height: 100%;
    }
    .page .front {

    }
    .page .back {
        z-index: 1;
        transform: scaleX(-1);
        background:url(book-back.png) no-repeat left center;
        background-size: cover;
        /* background: #fff; */
    }
    .page .front {
        background:url(./front.png) no-repeat center top;
        background-size: cover;
    }
    .cover .front {
        background:url(./cover.png) no-repeat center top;
        background-size: cover;
    }
    .page.inner .front {
        background:url(./front.png) no-repeat center right;
        background-size: auto 100%;
    }
    .flip-left {
        transform-origin: left center;
    transform: rotateY(-180deg);
        z-index: 1;
    }
    .flip-right {
        z-index: 2;
        transform-origin: left center;
    transform: rotateY(0deg);
    }
</style>
<script>
    var animating = false;
    var index = -1;
    var maxIndex = document.querySelectorAll(".page").length-1;
    for(let i= 0; i <= maxIndex; i++){
        document.querySelectorAll(".page")[i].style.zIndex = maxIndex-i;
        document.querySelectorAll(".page")[i].setAttribute("origin-index", maxIndex - i);
    }
    function handlePrev(){  
        var tmpIndex = index-1;
        if(tmpIndex<-1){
            tmpIndex = -1 
        }
        handleFlipPage('right', tmpIndex)
    }
    function handleNext(){
        var tmpIndex = index+1;
        if(tmpIndex>maxIndex-1){
            tmpIndex = maxIndex-1;
        }
        handleFlipPage('left', tmpIndex)
    }
    function handleFlipPage(dir, tmpIndex){
        console.log(dir, tmpIndex);
        if(animating){
            return false;
        }
        animating = true
        index = tmpIndex
        if(dir == 'left'){
            document.querySelectorAll(".page")[index].classList.remove("flip-right")
            document.querySelectorAll(".page")[index].classList.add("flip-left")
            setTimeout(() => {
                document.querySelectorAll(".page")[index].style.zIndex = index;
            }, 200)
            if (index == 0){
                document.querySelector("#book").classList.add("showPager")
            }
        }else{
            document.querySelectorAll(".page")[index+1].classList.add("flip-right")
            document.querySelectorAll(".page")[index+1].classList.remove("flip-left")
            setTimeout(() => {
                document.querySelectorAll(".page")[index+1].style.zIndex = document.querySelectorAll(".page")[index + 1].getAttribute("origin-index")
            }, 200)
            if (index == -1) {
                document.querySelector("#book").classList.remove("showPager")
            }
        }

        setTimeout(() => {
            animating = false;
        }, 410)
    }
</script>
</html>

素材:


front.png
book-back.png
cover.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末咖祭,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌葵孤,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件压语,死亡現(xiàn)場離奇詭異榄鉴,居然都是意外死亡,警方通過查閱死者的電腦和手機渡贾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進店門逗宜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人空骚,你說我怎么就攤上這事纺讲。” “怎么了囤屹?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵熬甚,是天一觀的道長。 經(jīng)常有香客問我肋坚,道長乡括,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任智厌,我火速辦了婚禮诲泌,結果婚禮上,老公的妹妹穿的比我還像新娘铣鹏。我一直安慰自己敷扫,他們只是感情好,可當我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布诚卸。 她就那樣靜靜地躺著葵第,像睡著了一般绘迁。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上卒密,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天脊髓,我揣著相機與錄音,去河邊找鬼栅受。 笑死,一個胖子當著我的面吹牛恭朗,可吹牛的內容都是我干的屏镊。 我是一名探鬼主播,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼痰腮,長吁一口氣:“原來是場噩夢啊……” “哼而芥!你這毒婦竟也來了?” 一聲冷哼從身側響起膀值,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤棍丐,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后沧踏,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體歌逢,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年翘狱,在試婚紗的時候發(fā)現(xiàn)自己被綠了秘案。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡潦匈,死狀恐怖阱高,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情茬缩,我是刑警寧澤赤惊,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站凰锡,受9級特大地震影響未舟,放射性物質發(fā)生泄漏。R本人自食惡果不足惜掂为,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一处面、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧菩掏,春花似錦魂角、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽访忿。三九已至,卻和暖如春斯稳,著一層夾襖步出監(jiān)牢的瞬間海铆,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工挣惰, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留卧斟,地道東北人。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓憎茂,卻偏偏與公主長得像珍语,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子竖幔,可洞房花燭夜當晚...
    茶點故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內容