วันดังกล่าวมาถึงแล้ว ในที่สุดคุณก็เบื่อกับการเลื่อนอ่านข้อความยาวๆ และกำลังมองหารูปแบบใหม่ อะไรที่ดูหรูหรา อุปกรณ์แบบกะทัดรัด อุปกรณ์ที่ใช้ตัดภาพสไลด์ยาวๆ ให้เป็นรูปสี่เหลี่ยมผืนผ้าเล็กๆ เรียบร้อยและนำมารวมกัน เราเรียกสิ่งประดิษฐ์นี้ว่า "สมุด"
เทคโนโลยีหนังสือล้ำสมัยพร้อมใช้งานในเบราว์เซอร์สมัยใหม่แล้วด้วยความสามารถของภูมิภาค CSS (CanIUse ให้ไปที่ chrome://flags
แล้วเปิดใช้ภูมิภาค CSS) และการเปลี่ยนรูปแบบ 3 มิติของ CSS คุณใช้ JavaScript เพียงไม่กี่บรรทัดและ CSS จำนวนมากก็เพียงพอแล้ว
มาเริ่มกันด้วยการกำหนดโครงสร้างหนังสือ หนังสือประกอบด้วยหน้าต่างๆ และหน้าแต่ละหน้ามี 2 ด้าน ด้านข้างมีเนื้อหาของหนังสือ
<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, -moz-
สำหรับ Firefox และอื่นๆ
#book-content {
+flow-into: book-text-flow;
}
.book-pages {
+flow-from: book-text-flow;
}
ตอนนี้เนื้อหาจากช่วง #book-content จะเข้าไปอยู่ใน div .book-pages แทน แต่หนังสือเล่มนี้ไม่ค่อยดี หากต้องการหนังสือที่เหมาะกับการอ่านมากกว่า เราจะต้องออกเดินทางค้นหา การเดินทางของเราจะนำคุณข้ามสะพานสายรุ้งของการเปลี่ยนรูปแบบ 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 เราต้องเปลี่ยนสมุดภาพแบบแบนเป็นสมุดภาพแบบเล่มเพื่อผ่านด่าน เราปรับแต่ละหน้าให้เอียงเล็กน้อยในแนว 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 ซึ่งเป็นดาวสีฟ้าอันยิ่งใหญ่ของจุดเชื่อมต่อบนโลก เรากลับถึงบ้านอย่างสมชัย ชูหนังสือขึ้นเหนือหัว เตรียมพร้อมรับขบวนพาเหรดและการเฉลิมฉลองอันยิ่งใหญ่ในเกียรติของเรา
คุณดูตัวอย่างออนไลน์ได้ที่นี่และดูแหล่งที่มาแบบเต็มของตัวอย่าง หากไม่มีภูมิภาค CSS ในเบราว์เซอร์ ตัวอย่างจะดูไม่สมบูรณ์ ในกรณีนี้ ให้ลองใช้ตัวอย่างนี้แทน