js實(shí)現(xiàn)3D旋轉(zhuǎn)相冊
本文實(shí)例為大家分享了js實(shí)現(xiàn)3D旋轉(zhuǎn)相冊的具體代碼,供大家參考,具體內(nèi)容如下
效果展示:

使用圖片:


剩余自己隨意 圖片大小為133*200
代碼展示:
<!DOCTYPE html>
<!--設(shè)置圖片的拖拽事件 不可用-->
<html lang="en" ondragstart="return false">
<head>
<meta charset="UTF-8">
<title>3D效果</title>
<style>
* {
background-color: #000;
}
.container {
border: 1px solid yellow;
perspective: 800px;
overflow: hidden;
}
.box {
position: relative;
border: 1px solid #f00;
width: 133px;
height: 200px;
margin: 300px auto;
transform-style: preserve-3d;
transform:rotateX(-20deg) rotateY(0deg);
}
img {
position: absolute;
/*設(shè)置圖片倒影效果*/
-webkit-box-reflect:below 5px -webkit-gradient(linear,left top, left bottom,from( transparent),color-stop(40%,transparent),to(rgba(250,250,250,0.4)));
}
</style>
</head>
<body>
<div class="container">
<div class="box">
<img src="img/1.jpg" alt="">
<img src="img/2.jpg" alt="">
<img src="img/3.jpg" alt="">
<img src="img/4.jpg" alt="">
<img src="img/5.jpg" alt="">
<img src="img/6.jpg" alt="">
<img src="img/7.jpg" alt="">
<img src="img/8.jpg" alt="">
<img src="img/9.jpg" alt="">
<img src="img/10.jpg" alt="">
<img src="img/11.jpg" alt="">
</div>
</div>
<script>
// a 獲取所有 img 元素
var mimg = document.querySelectorAll("img");
var mlength = mimg.length;
// 動態(tài)獲取 圖片的旋轉(zhuǎn)角度
var mdeg = 360/mlength;
//獲取box 容器
var mbox = document.querySelector(".box");
/*頁面加載后執(zhí)行。。效果*/
window.onload= function () {
// 1 圖片旋轉(zhuǎn)動畫
for(var i = 0;i<mlength;i++){
// console.log(mimg);
//每張圖片 1 60 2 120 3 180 4 240 60為圖片的平分角
mimg[i].style.transform = "rotateY("+(mdeg*i)+"deg) translateZ(350px)";
// 添加過渡效果 動畫執(zhí)行多長時間 多久后開始執(zhí)行動畫 此時的效果 從最后一張開始動畫
// console.log(mlength-i);
mimg[i].style.transition = "1s "+(mlength-i)*0.1+"s"; // 0.1 動畫調(diào)節(jié)
//從第一張開始動畫
// mimg[i].style.transition = "1s "+i+"s";
}
// 獲取鼠標(biāo)點(diǎn)的位置 獲取 差值 改變 轉(zhuǎn)換的rotate: x y
var newX,newY,lastX,lastY,cvalueX,cvalueY, rotX=-20,rotY=0;
// 鼠標(biāo)滑動后改變效果 (使用鼠標(biāo) 按下 替換點(diǎn)擊事件)
document.onmousedown = function (e) {
// 鼠標(biāo)點(diǎn)擊
// console.log("點(diǎn)擊");
lastX = e.clientX;
lastY = e.clientY;
// 鼠標(biāo)移動
this.onmousemove = function (e) {
// console.log("移動");
newX = e.clientX;
newY = e.clientY;
console.log(newX +" "+newY);
//獲取移動的差值
cvalueX = newX-lastX;
cvalueY = newY-lastY;
//獲取旋轉(zhuǎn)的角度
rotX -= cvalueY; /*因?yàn)橐蚯斑\(yùn)動所以是負(fù)值*/
rotY += cvalueX;
// 將角度添加上 img容器
mbox.style.transform = "rotateX("+rotX*0.1+"deg) rotateY("+rotY*0.1+"deg)"
// 差值太大(轉(zhuǎn)動太快) 調(diào)節(jié)每次的差值是和上一次差 而不是之前差(初始值的差)
lastX = newX;
lastY = newY;
}
// 鼠標(biāo)抬起
this.onmouseup = function () {
// console.log("抬起");
// 要停止移動的方法
this.onmousemove = null;
}
}
}
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript 彈出的窗口返回值給父窗口具體實(shí)現(xiàn)
這篇文章主要介紹了javascript 彈出的窗口返回值給父窗口具體實(shí)現(xiàn),有需要的朋友可以參考一下2013-11-11
微信小程序如何實(shí)現(xiàn)精確的日期時間選擇器
這篇文章主要介紹了微信小程序如何實(shí)現(xiàn)精確的日期時間選擇器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01
Bootstrap中定制LESS-顏色及導(dǎo)航條(推薦)
這篇文章主要介紹了Bootstrap中定制LESS-顏色及導(dǎo)航條的相關(guān)資料,非常不錯具有參考借鑒價值,感興趣的朋友一起看看吧2016-11-11
JS數(shù)組去重常用方法實(shí)例小結(jié)【4種方法】
這篇文章主要介紹了JS數(shù)組去重常用方法,結(jié)合實(shí)例形式總結(jié)分析了4種常用的數(shù)據(jù)去重實(shí)現(xiàn)方法,涉及javascript數(shù)組的遍歷、判斷、追加等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
JavaScript中判斷對象是否包含某個屬性(元素)的幾種方法
在JavaScript編程中,判斷對象數(shù)組是否包含某個特定對象是一項(xiàng)常見的任務(wù),這篇文章主要介紹了JavaScript中判斷對象是否包含某個屬性(元素)的幾種方法,需要的朋友可以參考下2025-08-08
淺談js圖片前端預(yù)覽之filereader和window.URL.createObjectURL
下面小編就為大家?guī)硪黄獪\談js圖片前端預(yù)覽之filereader和window.URL.createObjectURL。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06
Google AJAX 搜索 API實(shí)現(xiàn)代碼
Google AJAX 搜索 API實(shí)現(xiàn)代碼,需要的朋友可以參考下。2010-11-11
Bootstrap基本樣式學(xué)習(xí)筆記之按鈕(4)
篇文章主要介紹了Bootstrap學(xué)習(xí)筆記之按鈕基本樣式的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12

