當年騰訊微博客戶端有個很有意思的交互:用手指按住卡片的一角往下撕,紙片會隨著手指的移動翻捲,露出下面的內容。網頁上要模擬它,關鍵是把「撕」這個動作拆成三件事:跟手的變形、翻捲的層疊、以及超過閾值後的翻頁完成。
touchmove(PC 端用 mousedown + mousemove),根據指針位移計算折疊線的位置與角度,用 CSS transform 的 rotate、skew、scale 組合模擬紙張翹起。<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>模擬撕紙翻頁效果</title>
<style type="text/css">
body { margin:0; padding:0; font-size:14px; line-height:22px;
background:#e8e4d9; }
.book { position:relative; width:300px; height:400px; margin:40px auto; }
/* 底層:下一頁內容 */
.page_next {
position:absolute; top:0; left:0;
width:300px; height:400px; overflow:hidden;
background:#f2eee3;
}
/* 上層:當前頁,跟手變形 */
.page_cur {
position:absolute; top:0; left:0;
width:300px; height:400px; overflow:hidden;
background:#fffdf7; cursor:grab;
transform-origin: left bottom;
box-shadow: 2px 2px 8px rgba(0,0,0,.2);
-webkit-user-select:none; user-select:none;
}
/* 捲邊陰影,撕起時才顯示 */
.fold_shadow {
position:absolute; right:0; bottom:0;
width:60px; height:60px;
background: linear-gradient(135deg, rgba(0,0,0,.28), rgba(0,0,0,0));
opacity:0; pointer-events:none;
}
</style>
</head>
<body>
<div class="book" id="book">
<div class="page_next">下一頁的內容</div>
<div class="page_cur" id="pageCur">
當前頁的內容,按住右下角往上撕
<div class="fold_shadow" id="foldShadow"></div>
</div>
</div>
<script type="text/javascript">
var book = document.getElementById('book');
var page = document.getElementById('pageCur');
var shadow = document.getElementById('foldShadow');
var dragging = false;
var startX = 0, startY = 0;
function onStart(e) {
var p = e.touches ? e.touches[0] : e;
dragging = true;
startX = p.pageX;
startY = p.pageY;
page.style.cursor = 'grabbing';
}
function onMove(e) {
if (!dragging) return;
var p = e.touches ? e.touches[0] : e;
var dx = p.pageX - startX; // 向左為正撕
var dy = p.pageY - startY; // 向上為正撕
var progress = Math.min(Math.abs(dx) / book.offsetWidth, 1);
// 旋轉 + 輕微 Skew 模擬紙張翹起
page.style.transform =
'rotate(' + (-progress * 12) + 'deg) skewY(' + (progress * 4) + 'deg)';
// 陰影隨撕裂程度加深
shadow.style.opacity = progress;
if (e.cancelable) e.preventDefault();
}
function onEnd() {
if (!dragging) return;
dragging = false;
page.style.cursor = 'grab';
var progress = parseFloat(shadow.style.opacity) || 0;
if (progress > 0.33) {
// 超過閾值:完成翻頁
page.style.transition = 'transform .35s ease, opacity .35s ease';
page.style.transform = 'rotate(-100deg) translateX(-120%)';
page.style.opacity = 0;
setTimeout(function () {
// 把下一頁提上來,並重置狀態
document.querySelector('.page_next').style.zIndex = 2;
page.style.cssText = '';
shadow.style.opacity = 0;
}, 350);
} else {
// 未達閾值:回弹
page.style.transition = 'transform .3s ease';
page.style.transform = '';
shadow.style.opacity = 0;
}
}
page.addEventListener('mousedown', onStart);
document.addEventListener('mousemove', onMove);
document.addEventListener('mouseup', onEnd);
page.addEventListener('touchstart', onStart, { passive: true });
document.addEventListener('touchmove', onMove, { passive: false });
document.addEventListener('touchend', onEnd);
</script>
</body>
</html>
上面這版是「近似模擬」:用變形加陰影讓大腦產生撕紙的聯想。如果要還原度高到捲邊呈曲面,就需要引入 turn.js 這類專門做翻書效果的庫,它會把頁面切成多塊三角面,用 CSS 3D 變換逐面計算透視;更極致的做法是用 Canvas 或 WebGL 手寫曲面網格,按正弦曲線計算每一列的位移與明暗,性能換取真實感。
體驗層面還有幾個細節值得注意:拖動過程必須調用 preventDefault 阻止頁面跟著滾動;觸屏設備要給元素加 touch-action: none;閾值不宜過高,否則用戶會覺得「撕不動」;回弹動畫的曲線用 ease-out 更接近紙張的柔軟質感。