微信小程序实现卡片层叠滑动效果
程序员文章站
2022-05-25 23:09:50
本文实例为大家分享了微信小程序之卡片层叠滑动效果的具体代码,供大家参考,具体内容如下
代码:
js:
// index/gun/jsswiper2/jss...
本文实例为大家分享了微信小程序之卡片层叠滑动效果的具体代码,供大家参考,具体内容如下
代码:
js:
// index/gun/jsswiper2/jsswiper2.js page({ /** * 页面的初始数据 */ data: { startx: 0, endx: 0, icenter: 3, datas: [{ id: 1, zindex: 2, opacity: 0.2, left: 40, iamge: "../../images/1.jpg", animation: null }, { id: 2, zindex: 4, opacity: 0.4, left: 80, iamge: "../../images/2.jpg", animation: null }, { id: 3, zindex: 6, opacity: 0.6, left: 120, iamge: "../../images/3.jpg", animation: null }, { id: 4, zindex: 8, opacity: 1, left: 160, iamge: "../../images/4.jpg", animation: null }, { id: 5, zindex: 6, opacity: 0.6, left: 200, iamge: "../../images/5.jpg", animation: null }, { id: 6, zindex: 4, opacity: 0.4, left: 240, iamge: "../../images/6.jpg", animation: null }, { id: 7, zindex: 2, opacity: 0.2, left: 280, iamge: "../../images/7.jpg", animation: null }, ], order: [] }, /** * 生命周期函数--监听页面加载 */ onload: function (options) { this.__set__(); this.move(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onready: function () { }, /** * 生命周期函数--监听页面显示 */ onshow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onhide: function () { }, /** * 生命周期函数--监听页面卸载 */ onunload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onpulldownrefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onreachbottom: function () { }, /** * 用户点击右上角分享 */ onshareappmessage: function () { }, move: function () { var datas = this.data.datas; /*图片分布*/ for (var i = 0; i < datas.length; i++) { var data = datas[i]; var animation = wx.createanimation({ duration: 200 }); animation.translatex(data.left).step(); this.setdata({ ["datas[" + i + "].animation"]: animation.export(), ["datas[" + i + "].zindex"]: data.zindex, ["datas[" + i + "].opacity"]: data.opacity, }) } }, /**左箭头 */ left: function () { // var last = this.data.datas.pop(); //获取数组的最后一个 this.data.datas.unshift(last);//放到数组的第一个 var orderfirst = this.data.order.shift(); this.data.order.push(orderfirst); this.move(); }, /** */ right: function () { var first = this.data.datas.shift(); //获取数组的第一个 this.data.datas.push(first);//放到数组的最后一个位置 var orderlast = this.data.order.pop(); this.data.order.unshift(orderlast); this.move(); }, /**点击某项 */ choose: function (e) { var that = this; var id = e.currenttarget.dataset.id; var order = that.data.order; var index = 0; for (var i = 0; i < order.length; i++) { if (id == order[i]) { index = i; break; } } if (index < that.data.icenter) { for (var i = 0; i < that.data.icenter - index; i++) { this.data.datas.push(this.data.datas.shift()); //获取第一个放到最后一个 this.data.order.unshift(this.data.order.pop()); // this.right() } } else if (index > that.data.icenter) { for (var i = 0; i < index - that.data.icenter; i++) { this.data.datas.unshift(this.data.datas.pop()); //获取最后一个放到第一个 this.data.order.push(this.data.order.shift()); // this.left(); } } this.move(); }, /**新的排列复制到新的数组中 */ __set__: function () { var that = this; var order = that.data.order; var datas = that.data.datas; for (var i = 0; i < datas.length; i++) { that.setdata({ ["order[" + i + "]"]: datas[i].id }) } }, //手指触发开始移动 movestart: function (e) { console.log(e); var startx = e.changedtouches[0].pagex; this.setdata({ startx: startx }); }, //手指触摸后移动完成触发事件 moveitem: function (e) { console.log(e); var that = this; var endx = e.changedtouches[0].pagex; this.setdata({ endx: endx }); //计算手指触摸偏移剧距离 var movex = this.data.startx - this.data.endx; //向左移动 if (movex > 20) { this.left(); } if (movex < -20) { this.right(); } }, })
wxml:
<view class="teachers_banner"> <view class="container clearfix teachers_b"> <view class="slide" id="slide" bindtouchstart='movestart' bindtouchend='moveitem'> <block wx:for="{{datas}}"> <li animation="{{item.animation}}" style="z-index: {{item.zindex}} ;opacity:{{item.opacity}};" bindtap="choose" data-id="{{item.id}}"> <image src="{{item.iamge}}"></image> </li> </block> </view> </view> </view>
wxss:
.teachers_banner { width: 100%; height: 650px; background-size: cover; position: relative; overflow: hidden; } .teachers_b { position: relative; margin-top: 80px; } #slide { margin: 0 auto; width: 100%; height: 350px; position: relative; } image { width: 400rpx; height: 550rpx; } #slide li { position: absolute; width: 400rpx; display: -webkit-box; display: -webkit-flex; display: flex; align-items: flex-start; -webkit-box-align: flex-start; -webkit-align-items: flex-start; overflow: hidden; box-shadow: 0 0 20px #1d374d; } #slide li img { width: 100%; height: 100%; } .slide_right { padding: 40px; -webkit-box-flex: 1; -webkit-flex: 1; flex: 1; min-width: 0; } .slide_right dl { padding-top: 10px; } .arrow .prev, .arrow .next { position: absolute; width: 50px; top: 38%; z-index: 9; font: 700 96px 'simsun'; opacity: 0.3; color: #fff; cursor: pointer; }
效果:
1.左右滑动时,向相应方向移动一个卡片位置;
2.点击某一项时,将点击项位置移动到中间位置;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: 浅析AngularJS中的指令