js實現(xiàn)3D輪播圖效果
本文實例為大家分享了js實現(xiàn)3D輪播圖效果的具體代碼,供大家參考,具體內(nèi)容如下

主要有平移和旋轉(zhuǎn)構(gòu)成3d效果的輪播圖,小白一只,不足之處還請大家多多指教,代碼如下
css代碼:
*{
padding: 0;
margin: 0;
}
.box{
position: relative;
width: 100%;
height: 100%;
top: 200px;
margin: auto;
}
.warpper{
position: absolute;
width: 100%;
height: 100%;
perspective: 800px;
transform-style:preserve-3d;
}
.box .warpper img{
width: 300px;
height: 200px;
float: left;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;
border-radius: 8px;
transition: all 1s ease-in-out;
}
.btn{
position: relative;
top: 50%;
left: 50%;
width: 1200px;
transform: translate(-50%,-20px);
}
.btn .left,.btn .right{
height: 50px;
width: 50px;
font-size: 30px;
text-align: center;
line-height: 50px;
background-color: black;
color: white;
border-radius: 15%;
position: absolute;
}
.btn .left{
left: 0;
}
.btn .right{
right: 0;
}
.btn span:hover{
background-color: rgba(0,0,0,0.8);
}
.points{
position: absolute;
left: 50%;
bottom: 10px;
transform: translate(-50%,200px);
height: 14px;
}
.points li{
display: inline-block;
list-style: none;
width: 14px;
height: 14px;
border: 1px solid #000;
border-radius: 50%;
background-color: white;
margin: 0 5px;
}
.points .current{
background-color: red;
}
HTML代碼:
<div class="box">
<div class="warpper">
<img src="./勵志3.jpg" alt="">
<img src="./勵志2.jpg" alt="">
<img src="./2f1d.jpg" alt="">
<img src="./aimg.jpg" alt="">
<img src="./peg.jpg" alt="">
</div>
<div class="btn" id="btn">
<span class="left"> < </span>
<span class="right"> > </span>
</div>
<ul class="points">
</ul>
</div>
<script src="swarp.js"></script>
JS代碼:
var imgs = document.querySelectorAll("img")
var btns = document.querySelectorAll("span")
var ul = document.querySelector(".points")
var lis = document.getElementsByTagName("li")
var timer
var current = 0 // 當(dāng)前播放圖片的索引
var flag = true //點擊防抖節(jié)流
var len = imgs.length //圖片長度
function loadFirst() {
imgMove()
bind()
btnClick()
getDot()
showDot()
autoPlay()
}
loadFirst()
function imgMove() {
var mlen = Math.floor(len / 2)
for (var i = 0; i < mlen; i++) {
// 當(dāng)前播放圖片索引值
var rimg = current + 1 + i
var limg = len + current - 1 - i
if (rimg >= len) {
rimg -= len
}
if (limg >= len) {
limg -= len
}
imgs[limg].style.transform = `translateX(${150 * (i + 1)}px) translateZ(${200 - i * 100}px) rotateY(-30deg)`
imgs[rimg].style.transform = `translateX(${-150 * (i + 1)}px) translateZ(${200 - i * 100}px) rotateY(30deg)`
}
imgs[current].style.transform = `translateZ(300px)`
}
// 自動播放的函數(shù)
function autoPlay() {
timer = setInterval(function () {
if (current >= len - 1) {
current = 0
} else {
current++
}
imgMove()
autoLi()
}, 3000)
}
// 點擊圖片進行播放
function bind() {
for (let i = 0; i < imgs.length; i++) {
imgs[i].onclick = function () {
current = i
imgMove()
autoLi()
}
imgs[i].onmouseover = function () {
clearInterval(timer)
}
imgs[i].onmouseout = function () {
autoPlay()
}
}
}
// 點擊左右按鈕
function btnClick() {
for (let i = 0; i < btns.length; i++) {
btns[i].onclick = function () {
// 防止狂點擊
if (!flag) {
return
}
flag = false
if (i == 0) {
// 上一張
if (current <= 0) {
current = len - 1
} else {
current--
}
} else {
//下一張
if (current >= len - 1) {
current = 0
} else {
current++
}
}
setTimeout(function () {
flag = true
}, 1000)
imgMove()
autoLi()
}
btns[i].onmouseenter = function () {
clearInterval(timer)
}
btns[i].onmouseout = function () {
autoPlay()
}
}
}
// 點
function getDot() {
for (var i = 0; i < len; i++) {
ul.innerHTML += `<li></li>`
}
lis[0].classList.add("current")
}
function showDot() {
for (let i = 0; i < len; i++) {
lis[i].onclick = function () {
for (var j = 0; j < len; j++) {
lis[j].classList.remove("current")
}
this.classList.add("current")
current = i
imgMove()
}
}
}
function autoLi() {
for (var i = 0; i < len; i++) {
if (i == current) {
lis[i].classList.add("current")
} else {
lis[i].classList.remove("current")
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Raphael帶文本標(biāo)簽可拖動的圖形實現(xiàn)代碼
Javascript和Raphael順便學(xué)習(xí)了一下,主要是為了實現(xiàn)一個可拖動的矩形同時矩形上還得顯示標(biāo)簽,網(wǎng)上關(guān)于這方面的知識提的很是于是本人自不量力寫了一下,感興趣的你可不要錯過了哈,希望可以幫助到你2013-02-02
上傳圖片預(yù)覽JS腳本 Input file圖片預(yù)覽的實現(xiàn)示例
需要一個用戶上傳頭像預(yù)覽的功能,因此寫了一段上傳圖片預(yù)覽JS腳本,Input file圖片預(yù)覽的實現(xiàn),需要的朋友可以看看2014-10-10
window.print打印指定div指定網(wǎng)頁指定區(qū)域的方法
這篇文章主要介紹了window.print打印指定div指定網(wǎng)頁指定區(qū)域的方法,需要的朋友可以參考下2014-08-08
DOM_window對象屬性之--clipboardData對象操作代碼
clipboardData 對象提供了對于預(yù)定義的剪貼板格式的訪問,以便在編輯操作中使用。2011-02-02
JavaScript實現(xiàn)的圓形浮動標(biāo)簽云效果實例
這篇文章主要介紹了JavaScript實現(xiàn)的圓形浮動標(biāo)簽云效果,涉及javascript字符串與數(shù)組的遍歷、排序操作以及元素樣式動態(tài)操作與數(shù)學(xué)運算等相關(guān)技巧,是非常實用的一段代碼,需要的朋友可以參考下2015-08-08
javascript 不停(setInterval)/延時(setTimeout)函數(shù)使用實例
如果想實現(xiàn)頁面的一些函數(shù)的定時執(zhí)行,需要用到下面的函數(shù)大家可以看下用法。2009-08-08

