翻书特效是一种常见的网页交互效果,用于模拟真实书籍翻页的效果。这种效果通常通过JavaScript结合CSS动画来实现。下面是一个简单的翻书特效的实现例子:
翻书特效主要依赖于以下几个基础概念:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>翻书特效</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="book">
<div class="page" id="page1">第一页内容</div>
<div class="page" id="page2">第二页内容</div>
<div class="page" id="page3">第三页内容</div>
</div>
<script src="script.js"></script>
</body>
</html>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
margin: 0;
}
.book {
position: relative;
width: 300px;
height: 400px;
perspective: 1000px;
}
.page {
position: absolute;
width: 100%;
height: 100%;
background-color: white;
border: 1px solid #ccc;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
transform-origin: left;
transition: transform 1s;
}
#page1 { background-color: #ffcccc; }
#page2 { background-color: #ccffcc; }
#page3 { background-color: #ccccff; }
document.querySelectorAll('.page').forEach((page, index) => {
page.addEventListener('click', () => {
if (index < document.querySelectorAll('.page').length - 1) {
page.style.transform = 'rotateY(-180deg)';
document.querySelector(`#page${index + 2}`).style.transform = 'rotateY(0deg)';
}
});
});
requestAnimationFrame
优化动画性能,减少不必要的DOM操作。transition
属性的时间和缓动函数,确保动画平滑。通过以上步骤和代码示例,你可以实现一个简单的翻书特效。根据具体需求,还可以进一步优化和扩展功能。
领取专属 10元无门槛券
手把手带您无忧上云