CSS3簡易切割輪播圖
實作思路
- 首先創建一個父容器,用兩個無序串列通過彈性布局將父容器分為兩欄,
- 在li里面存放圖片通過給左邊的
li {background: url('圖片地址') no-repeat; background-size: 200% 100%;}給右邊的li{background-position-x: -300(父容器寬度的一半)px;}來實作將圖片分割成兩欄, - 給
ul{ransform-style: preserve-3d; }屬性來開啟瀏覽器的3D顯示, - 用子絕父相來將li疊放到一起
ul{position: relative;} li {position: absolute;}, - 通過transform屬性來設定li的旋轉,
- 到這里可以添加
.box:hover>ul { transition: all 5s;transform: rotateX(360deg); }來看看效果, - 最后添加兩個按鈕來讓用戶可以自己切換圖片,
- 點擊時只用改變ul的旋轉角度
btn1.onclick = ()=>{
item++;
let r = item * 90;
letf.style.transform = 'rotateX(' + r + 'deg)';
letf.style.transition = 'all 1s';
right.style.transform = 'rotateX(' + r + 'deg)';
right.style.transition = 'all 1s .3s';
}
btn2.onclick = ()=>{
item--;
let r = item * 90;
letf.style.transform = 'rotateX(' + r + 'deg)';
letf.style.transition = 'all 1s';
right.style.transform = 'rotateX(' + r + 'deg)';
right.style.transition = 'all 1s .3s';
}
最后附上全部代碼,希望對學習前端的你有所幫助
html代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>切割輪播圖</title>
</head>
<body>
<div class="box">
<ul class="letf">
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<ul class="right">
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
<button id="btn1">上一頁</button><button id="btn2">下一頁</button>
</body>
</html>
css 代碼
*{
margin: 0;
padding: 0;
}
body{
perspective: 800px;
}
.box{
display: flex;
width: 600px;
height: 350px;
margin: 150px auto;
}
.box:hover ul li:nth-child(1){
transition: all 5s;
transform: rotateX(360deg);
}
ul{
flex: 1;
list-style: none;
padding: 0;
margin: 0;
transform-style: preserve-3d; /* 開啟瀏覽器的3D顯示 */
position: relative;
}
li{
width: 100%;
height: 100%;
position: absolute;
}
li:nth-child(1){
background: url('../images/9.jpg') no-repeat;
background-size: 200% 100%;
transform: translateZ(175px);
}
li:nth-child(2){
background: url('../images/10.jpg') no-repeat;
background-size: 200% 100%;
transform: rotateX(90deg) translateZ(175px);
}
li:nth-child(3){
background: url('../images/11.jpg') no-repeat;
background-size: 200% 100%;
transform: rotateX(180deg) translateZ(175px);
}
li:nth-child(4){
background: url('../images/12.jpg') no-repeat;
background-size: 200% 100%;
transform: rotateX(-90deg) translateZ(175px);
}
.right li{
background-position-x: -300px;
}
js代碼
let item = 0;
let btn1 = document.getElementById('btn1');
let btn2 = document.getElementById('btn2');
let letf = document.querySelector('.letf');
let right = document.querySelector('.right')
btn1.onclick = ()=>{
item++;
let r = item * 90;
letf.style.transform = 'rotateX(' + r + 'deg)';
letf.style.transition = 'all 1s';
right.style.transform = 'rotateX(' + r + 'deg)';
right.style.transition = 'all 1s .3s';
}
btn2.onclick = ()=>{
item--;
let r = item * 90;
letf.style.transform = 'rotateX(' + r + 'deg)';
letf.style.transition = 'all 1s';
right.style.transform = 'rotateX(' + r + 'deg)';
right.style.transition = 'all 1s .3s';
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/231018.html
標籤:其他
