微信小程序?qū)崿F(xiàn)3D輪播圖效果(非swiper組件)
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)3D輪播圖效果的具體代碼,供大家參考,具體內(nèi)容如下
首先上一下效果圖:
在做的時(shí)候首先想到的是用swiper組件但是發(fā)現(xiàn)swiper組件放進(jìn)去圖片的時(shí)候會(huì)沒有3d的效果,原因是swiper組件自帶的style屬性限制了3d效果所需要的屬性,所以單獨(dú)寫了一個(gè)組件。
index.html
<view class='page-con'> rotate.wxml <view class='stage'> <view class='wrapper' bindtouchstart='start' bindtouchend='end'> <block wx:for='{{swiperList}}'> <image class='imgBasic {{index === 0 ? "" : "fold"}} {{item.active ? item.swpClass : ""}}' src='{{item.imgsrc}}' data-index='{{index}}'></image> </block> </view> <view class='dots'> <block wx:for='{{swiperList}}' wx:key='unique'> <view data-i='{{index}}' bindtap='fn' class='dot {{index === currentImageIndex ? "active" : ""}}'></view> </block> </view> </view> </view>
index.css:
.stage{ perspective: 3000rpx; perspective-origin: 50% 50%; border: 2rpx solid rgba(0, 0, 0, 0.5); padding: 20rpx 28rpx; background: rgba(255, 255, 255,0.8); height: 520rpx; } .wrapper{ height:480rpx; margin-top: 20rpx; transform-style: preserve-3d; position: relative; } .imgBasic{ position: absolute; width:480rpx; height:480rpx; border-radius:10rpx; border: 7rpx solid #fff; } image:nth-child(1){ -webkit-transform: rotateY(0deg); transform: rotateY(0deg); } image:nth-child(2){ left: 260rpx; } image:nth-child(3){ left: 320rpx; } image:nth-child(4){ left: 380rpx; } .fold{ transform: rotateY(-80deg) scale3d(1,0.85,1) translateZ(-10%); -webkit-transform: rotateY(-80deg) scale3d(1,0.85,1); background: rgba(255, 255, 255,0.8); } .swiper-con{ height: 520rpx; } .scrollCon{ display: flex; flex-direction: row; justify-content: flex-start; align-items: center; flex-wrap: nowrap; background: red; } .itemParent{ position: relative; width: 100%; height: 1000rpx; } .item{ width: 100rpx; float: left; height: 100rpx; } .item-con{ height:500rpx; } swiper-item{ width:480rpx; height:480rpx; } .idx-content { perspective: 1500; } .idx-content .idx-swiper { position: relative; margin: 40rpx 0; padding-bottom: 100%; transform-style: preserve-3d; } .idx-content .idx-swiper .idx-cswp { width: 70%; height: 100%; position: absolute; transform-style: preserve-3d; top: 0; border-radius: 6px; } .idx-content .idx-swiper .idx-cswp image { width: 100%; max-height: 600rpx; } .idx-content .idx-swiper .idx-cswp .swp-title .swp-btime { text-align: center; font-size: 28rpx; } .idx-content .idx-swiper .idx-cswp .swp-title .swp-bname { text-align: center; font-size: 24rpx; } /* 右邊的圖片展開動(dòng)畫效果 */ @keyframes rotateImage{ from{ transform:rotateY(-80deg); -webkit-transform:rotateY(-80deg); left: 250rpx; } to{ transform: rotateY(0deg) scale3d(1,1,1); -webkit-transform: rotateY(0deg) scale3d(1,1,1); left: 0rpx; } } @keyframes backRotateImage{ from{ transform: roateteY(0deg) scale3d(1,1,1); -webkit-animation: rotateY(0deg) scale3d(1,1,1); filter: contrast(100%) brightness(100%); } to{ transform: rotateY(280deg) scale3d(1,0.85,1); -webkit-animation: rotateY(280deg) scale3d(1,0.85,1); left: 210rpx; } } @keyframes leftMoveAnimation{ from{ /* transform:translateX(-300rpx); */ /* left: 260rpx; */ }to{ transform:translateX(-40%) scale3d(1,0.85,1) rotateY(-80deg); -webkit-transform:translateX(-40%) scale3d(1,0.85,1) rotateY(-80deg); } } @keyframes leftMove2Animation{ from{ }to{ transform:translateX(-35%) scale3d(1,0.85,1) rotateY(-80deg); -webkit-transform:translateX(-35%) scale3d(1,0.85,1) rotateY(-80deg); } } /* 功能介紹:向左邊展開,放大,位移操作 */ .swp-left { animation: rotateImage 1s normal; -webkit-animation: rotateImage 1s normal; animation-iteration-count:1; animation-fill-mode: forwards; transform-origin: right; backface-visibility: hidden; } /* 功能介紹:單獨(dú)從左側(cè)位移到屏幕的最后側(cè)位置并且播放折疊動(dòng)畫 */ .swp-right { animation: backRotateImage 1s normal; -webkit-animation: backRotateImage 1s normal; animation-iteration-count:1; animation-fill-mode: forwards; transform-origin: right; backface-visibility: hidden; } /* 右邊的動(dòng)畫依次向左移動(dòng),放大,旋轉(zhuǎn)操作 */ .move-left1{ transform:rotateY(-80deg) scale3d(1,0.85,1); animation: leftMoveAnimation 1s normal; -webkit-animation: leftMoveAnimation 1s normal; animation-iteration-count:1; animation-fill-mode: forwards; transform-origin: right; backface-visibility: hidden; } .move-left2{ transform:rotateY(-80deg) scale3d(1,0.85,1); animation: leftMove2Animation 1s normal; -webkit-animation: leftMove2Animation 1s normal; animation-iteration-count:1; animation-fill-mode: forwards; transform-origin: right; backface-visibility: hidden; } .dots{ display: flex; flex-wrap: nowrap; align-items: center; justify-content: center; margin: 0 8rpx; position: absolute; left: 0rpx; right: 0rpx; bottom: 15rpx; } .dot{ margin: 0 8rpx; height: 15rpx; width: 15rpx; background: #da91f5; border-radius: 15rpx; } .active{ width: 40rpx; height: 15rpx; border-radius: 15rpx; }
控制層index.js:
// pages/lck/testJing/perfact.js let app = getApp(); Page({ /** * 頁面的初始數(shù)據(jù) */ data: { host: app.host, dkheight: 300, dkcontent: `你好<br/>nihao,<br/><br/><br/><br/><br/><br/><br/>這個(gè)是測(cè)試<br/><br/>你同意了嗎<br/><br/><br/>hehe<b class='nihao'>n你好啊,我加粗了kk</b > <p><img src='http://shop.ykplay.com/upload/1/App.ico'/><strong>asdfasdfasd</strong></p>`, typeValue: null, showRightToast: false, changeImg: false, show: true, swiperList: [ { index: 0, aurl: "../start/start", swpClass: "swp-left", active: false, imgsrc: "../../resources/test.png", }, { index: 1, aurl: "#", swpClass: "swp-right", active: false, imgsrc: "../../resources/800.jpg" }, { index: 2, aurl: "#", swpClass: "swp-right", active: false, imgsrc: "../../resources/900.jpg" }, { index: 3, aurl: "#", swpClass: "swp-right", active: false, imgsrc: "../../resources/1000.jpg" }], played: false, //滑動(dòng)觸點(diǎn)開始的時(shí)候 startPoint: 0, currentImageIndex: 0 }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ onLoad: function (options) { this.data.typeValue = {}; this.data.typeValue.size = 'M'; this.data.typeValue.color = 'red'; console.log("typeValue is ", this.data.typeValue); this.setData({ typeValue: this.data.typeValue }) let winPage = this; wx.getSystemInfo({ success: function (res) { let winHeight = res.windowHeight; console.log(winHeight); winPage.setData({ dkheight: winHeight - winHeight * 0.05 - 80 }) } }) wxParse.wxParse('dkcontent', 'html', this.data.dkcontent, this, 5); }, /** * 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成 */ onReady: function () { }, /** * 生命周期函數(shù)--監(jiān)聽頁面顯示 */ onShow: function () { // wx.hideLoading(); }, /** * 生命周期函數(shù)--監(jiān)聽頁面隱藏 */ onHide: function () { }, /** * 生命周期函數(shù)--監(jiān)聽頁面卸載 */ onUnload: function () { }, /** * 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動(dòng)作 */ onPullDownRefresh: function () { }, /** * 頁面上拉觸底事件的處理函數(shù) */ onReachBottom: function () { }, /** * 用戶點(diǎn)擊右上角分享 */ onShareAppMessage: function () { }, previewImage: function (e) { var that = this, //獲取當(dāng)前圖片的下表 index = e.currentTarget.dataset.index, //數(shù)據(jù)源 pictures = this.data.pictures; wx.previewImage({ //當(dāng)前顯示下表 current: pictures[index], //數(shù)據(jù)源 urls: pictures }) }, onShareAppMessage: function (ops) { }, show: function (event) { this.setData({ show: true }) }, back: function () { this.setData({ show: false }) }, //顯示求換按鈕 swpBtn: function (event) { let swp = this.data.swiperList; let max = swp.length; let dataSet = event.currentTarget.dataset; let idx = dataSet.index; console.log("idx is ", idx); let prev = swp[idx - 1]; let curView = swp[idx]; let next = swp[idx + 1]; console.log("prev is ", prev); console.log("curView is ", curView); for (let j = 0; j < max; j++) { swp[j].active = true; } if (idx == 1 && prev && next) { prev.swpClass = 'swp-right'; curView.swpClass = 'swp-left'; console.log("curView.index is ", curView.index); for (let i = 2; i < max; i++) { if (i === 2) { swp[i].swpClass = 'move-left1'; } else if (i === 3) { swp[i].swpClass = 'move-left2'; } } let self = this; this.setData({ swiperList: swp }, () => { console.log("外層的setData被調(diào)用"); //將數(shù)組中的第一個(gè)元素刪除放到最后的位置 let first = swp.shift(); swp.push(first); console.log("swp is ", swp); self.data.swiperList = swp; self.setData({ swiperList: swp }, () => { console.log("最內(nèi)層的setData被調(diào)用"); }) }) } }, start: function (e) { console.log("e is ", e); this.data.startPoint = e.changedTouches[0].pageX; console.log("startPoint is ", this.data.startPoint); }, end: function (e) { let isLeft = false; let isRight = false; console.log("e is ", e); console.log("endPoint is ", e.changedTouches[0].pageX); let endPoint = e.changedTouches[0].pageX; console.log("是否向左移動(dòng)?", (endPoint - this.data.startPoint) < 0 ? (isLeft = true) : (isRight = true)); console.log("isLeft is ", isLeft); console.log("isRight is ", isRight); //如果向左移動(dòng)的話執(zhí)行相應(yīng)方法 if (isLeft) { this.moveLeft(1); } else { // this.moveRight(); } }, moveLeft: function (idx) { if (idx === 1) { let swp = this.data.swiperList; let max = swp.length; let prev = swp[idx - 1]; let curView = swp[1]; let next = swp[idx + 1]; console.log("prev is ", prev); console.log("curView is ", curView); for (let j = 0; j < max; j++) { swp[j].active = true; } if (prev && next) { prev.swpClass = 'swp-right'; curView.swpClass = 'swp-left'; console.log("curView.index is ", curView.index); // this.data.currentImageIndex = curView.index; this.setData({ currentImageIndex: curView.index }) for (let i = 2; i < max; i++) { if (i === 2) { swp[i].swpClass = 'move-left1'; } else if (i === 3) { swp[i].swpClass = 'move-left2'; } } let self = this; this.setData({ swiperList: swp }, () => { console.log("外層的setData被調(diào)用"); //將數(shù)組中的第一個(gè)元素刪除放到最后的位置 let first = swp.shift(); swp.push(first); console.log("swp is ", swp); self.data.swiperList = swp; self.setData({ swiperList: swp }, () => { console.log("最內(nèi)層的setData被調(diào)用"); }) }) } } }, icon: function (e) { console.log("e is ", e); } })
源碼地址:rotate3d
現(xiàn)在的功能是左滑移動(dòng),感興趣的朋友可以試試右滑移動(dòng)。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
TypeScript條件類型與內(nèi)置條件類型超詳細(xì)講解
我們可以使用TypeScript中的條件類型來根據(jù)邏輯定義某些類型,就像是在編寫代碼那樣。它采用的語法和我們?cè)贘avaScript中熟悉的三元運(yùn)算符很像:condition ? ifConditionTrue : ifConditionFalse。我們來看看他是怎么工作的2023-03-03常用的JavaScript驗(yàn)證正則表達(dá)式匯總
這篇文章主要是對(duì)常用的JavaScript驗(yàn)證正則表達(dá)式進(jìn)行了詳細(xì)的匯總介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-11-11JS求1到任意數(shù)之間的所有質(zhì)數(shù)的方法詳解
這篇文章主要介紹了JS求1到任意數(shù)之間的所有質(zhì)數(shù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05JavaScript作用域深度剖析之動(dòng)態(tài)作用域
這篇文章主要為大家介紹了?JavaScript作用域?qū)W習(xí)之動(dòng)態(tài)作用域深度剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05javascript模擬post提交隱藏地址欄的參數(shù)
想要隱藏地址欄的參數(shù),就只能用javascript模擬post提交,下面是示例代碼,需要的朋友可以看看2014-09-09JavaScript實(shí)現(xiàn)輪播圖方法(邏輯清晰一看就懂)
這篇文章主要給大家介紹了關(guān)于JavaScript實(shí)現(xiàn)輪播圖方法的相關(guān)資料,JS輪播圖的實(shí)現(xiàn)核心是使用JavaScript來控制圖片的切換和顯示,配合HTML和CSS完成布局和樣式設(shè)置,文中介紹的方法邏輯清晰一看就懂,需要的朋友可以參考下2023-12-12