CSS Regions と 3D 変換を使用してフリップ可能な書籍を作成する

Ilmari Heikkinen

いよいよその日がやってきました。あなたはついに長いテキストのスクロールに飽きてしまい、新しいフォーマットを探しています。エレガントな雰囲気。コンパクトなデザイン。長いスクロールを小さな長方形に切り分けて束縛します。私はこの発明を「本」と呼んでいます。

CSS リージョン(CanIUsechrome://flags に移動して CSS Regions を有効にする)と CSS 3D 変換の機能により、最新のブラウザで最先端の書籍テクノロジーを利用できるようになりました。必要なのは、数行の JavaScript と大量の CSS だけです。

まず、書籍の構造を定義します。書籍は複数のページで構成されており、ページは両面で構成されています。側面には書籍のコンテンツが表示されます。

<div class="book">
    <div> <!-- first page -->
    <div> <!-- front cover -->
        # My Fancy Book
    </div>
    <div> <!-- backside of cover -->
        # By Me I. Myself
        ## 2012 Bogus HTML Publishing Ltd
    </div>
    </div>
    <!-- content pages -->
    <div>
    <!-- front side of page -->
    <div class="book-pages"></div>
    <!-- back side of page -->
    <div class="book-pages"></div>
    </div>
    <div>
    <div class="book-pages"></div>
    <div class="book-pages"></div>
    </div>
    <div>
    <div class="book-pages"></div>
    <div class="book-pages"></div>
    </div>
</div>

CSS の Region を使用して、書籍のテキストを書籍のページに流し込みます。まず、本のテキストが必要です。

<span id="book-content">
    blah blah blah ...
</span>

本を作成したので、フローの CSS を定義しましょう。ベンダー プレフィックスのプレースホルダとして + 文字を使用しています。WebKit ブラウザの場合は -webkit-、Firefox の場合は -moz- などに置き換えてください。

#book-content {
    +flow-into: book-text-flow;
}
.book-pages {
    +flow-from: book-text-flow;
}

これで、#book-content スパンのコンテンツは .book-pages div に配置されます。ただ、この本はあまり良くない本です。本を読むため、冒険に出かけなければなりません。ここからは、CSS 変形のレインボー ブリッジを乗り越え、JavaScript の時計仕掛け王国へと変貌していくことでしょう。機械師の妖精の集落で、壮大な遷移魔法を解き放ち、世界のインターフェースを支配する伝説の 3 つの鍵を手に入れよう。

レインボー ブリッジの守り手は、スタイリッシュな構造セレクタの知恵を与えてくれました。それにより、HTML のブック構造をよりブック型に変えることができます。

html {
    width: 100%;
    height: 100%;
}
body {
    /* The entire body is clickable area. Let the visitor know that. */
    cursor: pointer;
    width: 100%;
    height: 100%;
    /* Set the perspective transform for the page so that our book looks 3D. */
    +perspective: 800px;
    /* Use 3D for body, the book itself and the page containers. */
    +transform-style: preserve-3d;
}
.book {
    +transform-style: preserve-3d;
    position: absolute;
}
/* Page containers, contain the two sides of the page as children. */
.book > div {
    +transform-style: preserve-3d;
    position: absolute;
}
/* Both sides of a page. These are flat inside the page container, so no preserve-3d. */
.book > div > div {
    /* Fake some lighting with a gradient. */
    background: +linear-gradient(-45deg, #ffffff 0%, #e5e5e5 100%);
    width: 600px;
    height: 400px;
    overflow: hidden;
    /* Pad the page text a bit. */
    padding: 30px;
    padding-bottom: 80px;
}
/* Front of a page */
.book > div > div:first-child {
    /* The front side of a page should be slightly above the back of the page. */
    +transform: translate3d(0px, 0px, 0.02px);
    /* Add some extra padding for the gutter. */
    padding-left: 40px;
    /* Stylish border in the gutter for visual effect. */
    border-left: 2px solid #000;
}
/* Back of a page */
.book > div > div:last-child {
    /* The back side of a page is flipped. */
    +transform: rotateY(180deg);
    padding-right: 40px;
    border-right: 2px solid #000;
}
/* Front cover of the book */
.book > div:first-child > div:first-child {
    /* The covers have a different color. */
    background: +linear-gradient(-45deg, #8c9ccc 0%, #080f40 100%);
    /* Put a border around the cover to make it cover the pages. */
    border: 2px solid #000;
    /* And center the cover. */
    margin-left: -1px;
    margin-top: -1px;
}
/* Back cover of the book */
.book > div:last-child > div:last-child {
    background: +linear-gradient(-45deg, #8c9ccc 0%, #080f40 100%);
    border: 2px solid #000;
    margin-left: -1px;
    margin-top: -1px;
}

HTML 用の紙の形をしたスタイルを作るなかで、JavaScript 界の 1 兆ギアの扉にたどり着きます。ゲートを通過するには、平らな本を適切な巻物に変える必要があります。書籍にボリュームを加えるために、各ページを z 軸で少しオフセットします。

(function() {
var books = document.querySelectorAll('.book');
for (var i = 0; i < books.length; i++) {
    var book = books[i];
    var pages = book.childNodes;
    for (var j = 0; j < pages.length; j++) {
    if (pages[j].tagName == "DIV") {
        setTransform(pages[j], 'translate3d(0px, 0px, ' + (-j) + 'px)');
    }
    }
}
})();

遷移マジックを使って妖精を魅了することは、呼び出しで最も難しいことではありません。それでも、このテスト結果により、本のページの回転がスムーズにアニメーション化されています。

.book > div {
    +transition: 1s ease-in-out;
}

最後に、ページが実際にめくるには、イベント自体を社会貢献活動にバインドする必要があります。

(function(){
    // Get all the pages.
    var pages = document.querySelectorAll('.book > div');
    var currentPage = 0;
    // Go to previous page when clicking on left side of window.
    // Go to the next page when clicking on the right side.
    window.onclick = function(ev) {
        if (ev.clientX < window.innerWidth/2) {
        previousPage();
        } else {
        nextPage();
        }
        ev.preventDefault();
    };
    var previousPage = function() {
        if (currentPage > 0) {
        currentPage--;
            // Rotate the page to closed position and move it to its place in the closed page stack.
        setTransform(pages[currentPage], 'translate3d(0px,0px,' + (-currentPage) + 'px) rotateY(0deg)');
        }
    };
    var nextPage = function() {
        if (currentPage < pages.length) {
            // Rotate the page to open position and move it to its place in the opened stack.
        setTransform(pages[currentPage], 'translate3d(0px,0px,' + currentPage + 'px) rotateY(-150deg)');
        currentPage++;
        }
    };
})();

「本」のテクノロジーを手に入れたため、地球上の水晶タワーから避難でき、目のくらむような反射と、地球のつながりを象徴する青い星、アチェナーの激しい核火事から解放されます。私たちは、私たちの敬意を込めてパレードや祝典が必然的に流れる準備を整え、勝ち誇った家に戻り、本を頭上に掲げてやってきます。

サンプルはこちらでご覧いただけます。サンプルのソース全体もご確認いただけます。ブラウザに CSS Regions がない場合、この例はかなりうまく機能しません。その場合は、こちらの例をお試しください。