使用 CSS Regions 和 3D 轉換功能編寫可列印的書籍

Ilmari Heikkinen

那麼,那一天終於來臨了。你終於厭倦了長篇文字,並想尋找新的格式。優雅的東西。精簡。將長捲動畫面切成整齊的小矩形,並將這些矩形綁定在一起。我將這項發明稱為「書籍」。

有了 CSS 區域 (CanIUse,請前往 chrome://flags 並啟用 CSS 區域) 和 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 區域,將書籍文字流入書籍頁面。但我們需要先取得書籍內容。

<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 span 中的內容就會改為放入 .book-pages div 中。不過,這本書的內容相當貧乏。為了寫出更像書籍的書籍,我們必須展開探索。我們的旅程將會跨越 CSS 轉換的彩虹橋,前往 JavaScript 的機械錶王國。在機械仙子的大廳中,我們將施展史詩般的轉場魔法,並取得傳說中的三把鑰匙,用來控制上層介面。

彩虹橋的守護者會傳授我們時尚結構選取器的智慧,讓我們將 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 王國的千億齒輪大門。為了通過閘門,我們必須將平面書轉換為適當的書籍。為了讓書籍有厚度,我們在 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++;
        }
    };
})();

我們已取得「書籍」技術,可以撤離地表水晶塔,遠離那刺眼的光芒和 Achenar 的核火,Achenar 是地表 Nexus 的巨大藍色恆星。我們得意洋洋地回到家中,高舉書本,準備迎接必然會接踵而來的遊行和慶祝活動。

您可以在這個網頁上查看線上示例,並取得示例的完整原始碼。如果瀏覽器中沒有 CSS 區域,範例就會顯示為不完整。在這種情況下,您可以改用這個範例