原生js實(shí)現(xiàn)3D輪播圖
3D輪播圖是我做過(guò)復(fù)雜的圖片輪播了,由于是利用坐標(biāo),層級(jí)之間的關(guān)系,所以實(shí)現(xiàn)起來(lái)原理比較簡(jiǎn)單但是bug巨多,在推推拖拖了好久后,下定決心重新整理成博客,與大家分享!
首先分析一下3D圖片輪播的功能需求:
和其它圖片輪播大體一致,無(wú)非就是點(diǎn)擊按鈕向左、右翻頁(yè),點(diǎn)擊下方提示位置小點(diǎn)切換到小點(diǎn)表示對(duì)的位置頁(yè)(我是真的不知道那個(gè)小點(diǎn)叫什么名字,大家將就著聽吧)
上代碼:
基本代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>立體輪播圖</title>
<style>
.block{
position: relative;
width: 900px;
height: 370px;
margin: 0 auto;
overflow: hidden;
}
.imgae_div{
position: relative;
width: 900px;
height: 300px;
}
.imgae_div>div{
position: absolute;
width: 400px;
height: 200px;
transition: all 1s linear;
}
.imgae_div img{
width: 400px;
height: 200px;
}
.imgae_div>div:nth-child(1){
top: 150px;
left: 250px;
z-index: 6;
}
.imgae_div>div:nth-child(2){
top: 100px;
left: 0px;
z-index: 5;
}
.imgae_div>div:nth-child(3){
top: 50px;
left: 0px;
z-index: 4;
}
.imgae_div>div:nth-child(4){
top: 0px;
left: 250px;
z-index: 3;
}
.imgae_div>div:nth-child(5){
top: 50px;
left: 500px;
z-index: 4;
}
.imgae_div>div:nth-child(6){
top: 100px;
left: 500px;
z-index: 5;
}
.btn{
width: 900px;
height: 40px;
position: absolute;
top: 155px;
z-index: 999;
overflow: hidden;
}
.btn>span:nth-child(1){
width: 30px;
background-color: rgba(164, 150, 243, 0.336);
display: block;
float: left;
text-align: center;
line-height: 40px;
margin-left: -30px;
cursor: pointer;
opacity: 0;
transition: all 0.5s linear;
}
.btn>span:nth-child(2){
width: 30px;
background-color: rgba(164, 150, 243, 0.336);
display: block;
float: right;
text-align: center;
line-height: 40px;
margin-right: -30px;
cursor: pointer;
opacity: 0;
transition: all 0.5s linear;
}
.marright{
margin-right: 0px !important;
opacity: 1 !important;
}
.marleft{
margin-left: 0px !important;
opacity: 1 !important;
}
.point{
width: 108px;
height: 14px;
position: absolute;
bottom: 0;
left: 396px;
z-index: 10;
}
.point>div{
width: 14px;
height: 14px;
border-radius: 50%;
background-color: white;
float: left;
margin: 0 2px;
border: 2px solid black;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="block">
<div class="imgae_div">
<div><img src="./img/111.jpg" alt=""></div>
<div><img src="./img/222.jpg" alt=""></div>
<div><img src="./img/333.jpg" alt=""></div>
<div><img src="./img/444.jpg" alt=""></div>
<div><img src="./img/555.jpg" alt=""></div>
<div><img src="./img/666.jpg" alt=""></div>
</div>
<div class="btn">
<span><</span>
<span>></span>
</div>
<div class="point">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</div>
<script src="./js/index.js"></script>
</body>
</html>
js代碼:
// 簡(jiǎn)單說(shuō)一下我是怎么想的:1.分步實(shí)現(xiàn),先實(shí)現(xiàn)圖片自己動(dòng),在加其它的功能
// 2.每實(shí)現(xiàn)一個(gè)功能要立馬去測(cè)bug,因?yàn)榉诺胶竺婢驮诫y找了。
// 3.輪播向左,向右是兩個(gè)互相聯(lián)系的方法,需要找到彼此的關(guān)系
var imgae_div = document.getElementsByClassName('imgae_div')[0];
var imgae_div_child = imgae_div.children;
var btn=document.getElementsByClassName('btn')[0];
var block=document.getElementsByClassName('block')[0];
var new_point = document.getElementsByClassName("point")[0].children;
new_point[0].style.backgroundColor = "#000000";
// 利用函數(shù)的封裝 ps:圖片輪播離不開計(jì)時(shí)器,且個(gè)人覺(jué)得用setIntervar比較多
img_work();
function img_work() {
time = setInterval(function () {
img_workfirst('left', 0);//兩個(gè)參數(shù),判斷向左還是向右輪播,索引
}, 1500);
}
// console.log(point.child);
function img_workfirst(left_right, cindex) {
// 這里面首先說(shuō)一下css中寫好的默認(rèn)層關(guān)系:從第1張到第6張為別為 6 5 4 3 4 5,和頁(yè)面的布局有關(guān)
var firstpage = {//當(dāng)前頁(yè)的各種屬性
// getComputedStyle()獲取css屬性
left: window.getComputedStyle(imgae_div_child[cindex]).left,
top: window.getComputedStyle(imgae_div_child[cindex]).top,
zIndex: window.getComputedStyle(imgae_div_child[cindex]).zIndex,
backcolor: window.getComputedStyle(new_point[cindex]).backgroundColor
};
if (left_right == 'left') {
// 向左輪播為默認(rèn)輪播
for (var i = 0; i < imgae_div_child.length; i++) {
// for循環(huán)遍歷所有元素
if (i == imgae_div_child.length - 1) {
// 當(dāng)前頁(yè)的下一張為 最后一張(位置都是動(dòng)態(tài)切換的)
imgae_div_child[i].style.left = firstpage.left;
imgae_div_child[i].style.top = firstpage.top;
imgae_div_child[i].style.zIndex = firstpage.zIndex;
new_point[i].style.backgroundColor = firstpage.backcolor;
}
else {
// 其它頁(yè)對(duì)應(yīng)為它前面元素的屬性
imgae_div_child[i].style.left = window.getComputedStyle(imgae_div_child[i + 1]).left;
imgae_div_child[i].style.top = window.getComputedStyle(imgae_div_child[i + 1]).top;
imgae_div_child[i].style.zIndex = window.getComputedStyle(imgae_div_child[i + 1]).zIndex;
new_point[i].style.backgroundColor = window.getComputedStyle(new_point[i + 1]).backgroundColor;
}
}
}
// 向右輪播,借助向左輪播分析
else {
for (var i = imgae_div_child.length - 1; i >= 0; i--) {
if (i == 0) {
imgae_div_child[i].style.left = firstpage.left;
imgae_div_child[i].style.top = firstpage.top;
imgae_div_child[i].style.zIndex = firstpage.zIndex;
new_point[i].style.backgroundColor = firstpage.backcolor;
}
else {
imgae_div_child[i].style.left = window.getComputedStyle(imgae_div_child[i - 1]).left;
imgae_div_child[i].style.top = window.getComputedStyle(imgae_div_child[i - 1]).top;
imgae_div_child[i].style.zIndex = window.getComputedStyle(imgae_div_child[i - 1]).zIndex;
new_point[i].style.backgroundColor = window.getComputedStyle(new_point[i - 1]).backgroundColor;
}
}
}
firstpage = null;
// 將表示當(dāng)前頁(yè)的數(shù)據(jù)清空,防止bug(因?yàn)楫?dāng)前頁(yè)也是動(dòng)態(tài)變化的,需要?jiǎng)討B(tài)創(chuàng)建)
}
// console.log(new_point);
// 消除一些bug
window.onblur = function () {//窗口失焦時(shí),計(jì)時(shí)器停止
clearInterval(time);
}
window.onfocus = function () {
img_work();//獲焦時(shí)開啟計(jì)時(shí)器
}
document.onselectstart = function () {//禁止用戶復(fù)制
return false;
}
block.οnmοuseenter=function(){//鼠標(biāo)進(jìn)入輪播圖時(shí),兩個(gè)按鈕滑動(dòng)出來(lái)
clearInterval(time);
btn.children[1].className='marright';
btn.children[0].className='marleft';
}
block.οnmοuseleave=function(){//離開時(shí)和平時(shí)隱藏
img_work();
btn.children[1].className='';
btn.children[0].className='';
for (var k = 0; k < imgae_div_child.length; k++) {
imgae_div_child[k].style.transitionDuration = "0.5s";
}
}
// 對(duì)應(yīng)的左右按鈕的點(diǎn)擊事件
btn.children[0].οnclick=function(event){
if(event.detail==1){//用于控制鼠標(biāo)的連擊,但是效果對(duì)于故意測(cè)試來(lái)說(shuō)還是有所缺陷 下同
img_workfirst('left',0);
}
}
btn.children[1].οnclick=function(event){
if(event.detail==1){
img_workfirst('right',imgae_div_child.length-1);
}
}
// point的事件
for(var i=0;i<new_point.length;i++){
new_point[i].index=i;
new_point[i].οnclick=function(){
for(var k=0;k<imgae_div_child.length;k++){
imgae_div_child[k].style.transitionDuration='0.1s';//動(dòng)畫完成
}
var oldindex=0;
for(var k=0;k<new_point.length;k++){
if(new_point[k].style.backgroundColor== 'rgb(0, 0, 0)'){//格式必須統(tǒng)一
oldindex=new_point[k].index;
}
}
var num =0;//判斷計(jì)算轉(zhuǎn)動(dòng)次數(shù)
if(this.index>oldindex){//所需頁(yè)在當(dāng)前頁(yè)的左(左右相對(duì)于下方點(diǎn)來(lái)說(shuō))
num=this.index-oldindex;
var timego=setInterval(function(){
num--;
if(num<0){
clearInterval(timego);
}
else{
img_workfirst('right',5)//因?yàn)榉较蜃兞?,所以下一?yè)就為當(dāng)前頁(yè)的上一頁(yè),也就是cindex為5
}
},100);//動(dòng)畫時(shí)間縮短,優(yōu)化體驗(yàn)
}
else{//所需頁(yè)在當(dāng)前頁(yè)的左(左右相對(duì)于下方點(diǎn)來(lái)說(shuō))
num=Math.abs(this.index-oldindex);
var timego=setInterval(function(){
num--;
if(num<0){
clearInterval(timego);
}
else{
img_workfirst('left',0)
}
},100);
}
}
}
關(guān)于出現(xiàn)的bug的一些總結(jié):
1、注意左右的區(qū)分與聯(lián)系
2、注意連續(xù)點(diǎn)擊的bug
3、注意切換窗口,切換頁(yè)面,最小化等這些切換的bug
4、注意代碼格式,在js中寫css樣式時(shí),要注意格式
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 原生js實(shí)現(xiàn)輪播圖的示例代碼
- js實(shí)現(xiàn)支持手機(jī)滑動(dòng)切換的輪播圖片效果實(shí)例
- js實(shí)現(xiàn)輪播圖的完整代碼
- js實(shí)現(xiàn)點(diǎn)擊左右按鈕輪播圖片效果實(shí)例
- JS實(shí)現(xiàn)左右無(wú)縫輪播圖代碼
- JS輪播圖實(shí)現(xiàn)簡(jiǎn)單代碼
- JS實(shí)現(xiàn)自動(dòng)輪播圖效果(自適應(yīng)屏幕寬度+手機(jī)觸屏滑動(dòng))
- 原生js實(shí)現(xiàn)無(wú)限循環(huán)輪播圖效果
- 基于vue.js輪播組件vue-awesome-swiper實(shí)現(xiàn)輪播圖
- zepto中使用swipe.js制作輪播圖附swipeUp,swipeDown不起效果問(wèn)題
相關(guān)文章
js動(dòng)態(tài)獲取子復(fù)選項(xiàng)并設(shè)計(jì)全選及提交的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇js動(dòng)態(tài)獲取子復(fù)選項(xiàng)并設(shè)計(jì)全選及提交的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的, 現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
用javascript實(shí)現(xiàn)檢測(cè)指定目錄是否存在的方法
今天看到一篇關(guān)于onegreen被掛馬的代碼發(fā)現(xiàn)這個(gè)函數(shù),它用js就可以檢測(cè),制定的目錄或指定的文件是否存在,一般用來(lái)讀chm文件中的圖片來(lái)檢測(cè),目錄的存在。高手就是不學(xué)好。2008-01-01
BootStrap實(shí)現(xiàn)手機(jī)端輪播圖左右滑動(dòng)事件
用bootstrap做出的項(xiàng)目輪播圖在手機(jī)端不能滑動(dòng),為此找了好多插件、框架。但是都不能和bootstrap良好的結(jié)合。經(jīng)過(guò)一番折騰終于找到了解決方法,下面小編通過(guò)本文給大家簡(jiǎn)單介紹下2016-10-10
詳解MVC如何使用開源分頁(yè)插件(shenniu.pager.js)
本文主要分享了shenniu.pager.js整個(gè)插件內(nèi)容,不多且清晰。具有很好的參考價(jià)值,需要的朋友一起來(lái)看下吧2016-12-12
如何用純js實(shí)現(xiàn)返回頁(yè)面頂部功能
頁(yè)面返回頂部是在Web中常見的效果,在一個(gè)很長(zhǎng)的頁(yè)面中,頁(yè)面返回頂部按鈕可以方便用戶回到頁(yè)面的頂部,增強(qiáng)用戶體驗(yàn),這篇文章主要給大家介紹了關(guān)于如何用純js實(shí)現(xiàn)返回頁(yè)面頂部功能的相關(guān)資料,需要的朋友可以參考下2024-06-06
Bootstrap作品展示站點(diǎn)實(shí)戰(zhàn)項(xiàng)目2
這篇文章主要為大家分享了Bootstrap作品展示站點(diǎn)實(shí)戰(zhàn)項(xiàng)目,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
JavaScript中常見內(nèi)置函數(shù)用法示例
這篇文章主要介紹了JavaScript中常見內(nèi)置函數(shù)用法,結(jié)合實(shí)例形式分析了JavaScript常用內(nèi)置函數(shù)的功能、參數(shù)、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-05-05
一文總結(jié)JS中邏輯運(yùn)算符的特點(diǎn)
在JavaScript的眾多運(yùn)算符里,提供了三個(gè)邏輯運(yùn)算符&&、||和!,下面這篇文章主要給大家介紹了關(guān)于JS中邏輯運(yùn)算符的特點(diǎn),文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03

