当前位置:网站首页>[wechat applet development (III)] realize the stacking and sliding of cards
[wechat applet development (III)] realize the stacking and sliding of cards
2022-07-24 08:15:00 【Sock is a dog】
The renderings look like this , It can slide left and right
List of articles
Preface
Animation : transparency + The zoom
Namely swiper The switching effect of , Then why not swiper Well ?
Built in wechat applet swiper Components , Still quite unfriendly . image h5 Same swiper Plug ins are not available in applets .
One 、 Realize the event capture of sliding left and right
// Slide gesture start event
startEvent: function(event) {
if (event.changedTouches[0].pageX) {
this.data.startPageX = event.changedTouches[0].pageX
} else {
this.data.startPageX = event.changedTouches[0].x
}
},
// Slide gesture end event
endEvent: function(event) {
let endPageX = 0
if (event.changedTouches[0].pageX) {
endPageX = event.changedTouches[0].pageX
} else {
endPageX = event.changedTouches[0].x
}
const moveX = endPageX - this.data.startPageX
if (Math.abs(moveX) < 20) return
if (moveX > 0) {
// Right slip
this.prev() // Write your right slide method here
} else {
// Left slide
this.next() // Write your left slide method here
}
},
Bind the sliding method in the card sliding area
<!-- The middle card -->
<div class="slider-container" bindtouchstart="startEvent" bindtouchend="endEvent">
</div>
At this time, the sliding event is captured , Next, start to write animation
Two 、 For sliding left and right wx.animation
1. First, the static layout should be complete , Then you can implement your own animation
<!-- The middle card -->
<div class="slider-container" bindtouchstart="startEvent" bindtouchend="endEvent">
<div class="{
{card.isActive ? 'slide-item-active': card_index==cardsData.length-1? 'slide-item-last' : 'slide-item'}}" wx:for="{
{cardsData}}" bindtap="cardLinkByType" data-type="{
{card.type}}" data-index="{
{card.index || 0}}" wx:for-item="card" wx:for-index="card_index" wx:key="unique" animation="{
{card.animation}}" style="z-index:{
{
card.zIndex?card.zIndex:199 - card_index}}" data-idx="{
{card_index}}" data-tittle="{
{card.title}}">
<!-- <image src="/images/page_index/ Relieve irritability .jpg" mode='widthFix'></image> -->
<image src="/images/page_index/card_{
{card.image_name}}.jpg" mode='widthFix'></image>
<div class="bottom-mask">
<div class="title">{
{card.title}}</div>
<div class="desc">{
{card.desc}}</div>
<image src="/images/page_index/[email protected]" mode='widthFix'></image>
</div>
</div>
</div>
.slider-container {
margin-top: -65px;
position: relative;
display: flex;
justify-content: center;
align-items: center;
z-index: 99;
height: 800rpx;
width: 100%;
}
.slide-item-active {
width: 630rpx;
height: 800rpx;
border-radius: 20rpx;
overflow: hidden;
position: absolute;
transform: translateX(0%);
}
.slide-item-last {
/* Be careful : // Because you can slide left and right So there is a hidden one on the left of initialization ( Here is the last one , At least 3 A card )*/
width: 630rpx;
height: 800rpx;
border-radius: 20rpx;
overflow: hidden;
position: absolute;
transform: translateX(-500px);
}
.slide-item {
border-radius: 20rpx;
overflow: hidden;
position: absolute;
right: 30rpx;
width: 550rpx;
height: 640rpx;
}
.slide-item-active image ,.slide-item image,.slide-item-last image{
width: 100%;
height: auto;
display: block;
}
2. Loop animation
principle : Put the... Of the card object animation Initialize to null , Use when animation is needed this.setData Animate him :
prev: function() {
this.animationCardRight()
},
next: function() {
this.animationCardLeft()
},
animationCardLeft: function() {
clearInterval(this.timer);
let translateX = -500;
let animation = wx.createAnimation({
duration: 680,
timingFunction: "ease",
});
animation.translateX(translateX).opacity(0.5).step();
let animationNext = wx.createAnimation({
duration: 680,
timingFunction: "ease",
});
animationNext.width('630rpx').height('100%').right('60rpx').step();
let _index = this.data.currentActiveCardIndex;
let _nextIndex = this.data.currentActiveCardIndex + 1;
let _preIndex = this.data.currentActiveCardIndex - 1;
// Now it's the last Make handover
if (this.data.currentActiveCardIndex == this.data.cardsData.length - 1) {
_nextIndex = 0;
}
// The next one is the last one Move the first one there
let _next_nextIndex = _nextIndex + 1;
if (_nextIndex == this.data.cardsData.length - 1) {
_next_nextIndex = 0;
}
// Move the left one to the right and hide it
let animationNextNext = wx.createAnimation({
duration: 0,
timingFunction: "ease",
});
animationNextNext.width('550rpx').height('640rpx').opacity(1).translateX(0).right('30rpx').step();
this.setData({
['cardsData['+ _index +'].animation']: animation.export(),
['cardsData['+ _nextIndex +'].animation']: animationNext.export(),
['cardsData['+ _next_nextIndex +'].animation']: animationNextNext.export(),
['cardsData['+ _index +'].zIndex']: this.data.zIndexInit,
['cardsData['+ _nextIndex +'].zIndex']: this.data.zIndexInit-2,
['cardsData['+ _next_nextIndex +'].zIndex']: this.data.zIndexInit-3,
})
this.data.zIndexInit--
this.data.currentActiveCardIndex = _nextIndex
setTimeout(()=>{
this.createBanerTimer();
},680)
},
animationCardRight: function() {
clearInterval(this.timer);
let translateX = 0;
let animation = wx.createAnimation({
duration: 680,
timingFunction: "ease",
});
animation.translateX(translateX).opacity(1).step();
let animationNext = wx.createAnimation({
duration: 680,
timingFunction: "ease",
});
animationNext.width('550rpx').height('640rpx').right('30rpx').step();
let _index = this.data.currentActiveCardIndex;
let _nextIndex = this.data.currentActiveCardIndex + 1;
let _preIndex = this.data.currentActiveCardIndex - 1;
if (this.data.currentActiveCardIndex == 0) {
_preIndex = this.data.cardsData.length - 1;
}
let _pre_preIndex = _preIndex - 1;
if (_preIndex == 0) {
_pre_preIndex = this.data.cardsData.length - 1;
}
// Move the one in front to the left
let animationPrePre = wx.createAnimation({
duration: 0,
timingFunction: "ease",
});
animationPrePre.width('630rpx').height('100%').opacity(0).translateX(-500).right('60rpx').step();
this.setData({
['cardsData['+ _pre_preIndex +'].animation']: animationPrePre.export(),
['cardsData['+ _preIndex +'].animation']: animation.export(),
['cardsData['+ _index +'].animation']: animationNext.export(),
['cardsData['+ _pre_preIndex +'].zIndex']: this.data.zIndexInit+2,
['cardsData['+ _preIndex +'].zIndex']: this.data.zIndexInit+1,
['cardsData['+ _index +'].zIndex']: this.data.zIndexInit,
})
this.data.zIndexInit++
this.data.currentActiveCardIndex = _preIndex
setTimeout(()=>{
this.createBanerTimer();
},680)
},
summary
You can search on wechat 【 Simple psychological meditation sleep 】 Applet , You can see the effect on the home page
边栏推荐
- Cososcreator upgrade gradle version
- What is NFT? An article to understand the concept of NFT
- UVA572油田 Oil Deposits题解
- [Beijiao] image processing: basic concepts, image enhancement, morphological processing, image segmentation
- *Code understanding *numpy basic (plus code) that must be understood
- VIDAR team team exclusive interview: as we do, as you know
- 【MATLAB】(四)MATLAB在线性代数中的应用
- *Yolo5 learning * data experiment based on yolo5 face combined with attention model CBAM
- Cmake binary installation
- Summary of study notes (I)
猜你喜欢
![[Beijiao] image processing: basic concepts, image enhancement, morphological processing, image segmentation](/img/b3/76d2bcdf4b9769fb6308b7dac9ceb5.jpg)
[Beijiao] image processing: basic concepts, image enhancement, morphological processing, image segmentation
![[Google play access] payment server token acquisition](/img/c6/d095ea2b88a11bf6b4bdd80499932c.png)
[Google play access] payment server token acquisition

*Yolo5 learning * data experiment based on yolo5 face combined with attention model se

Wechat applet subscription message development process
![[wechat applet development] (I) development environment and applet official account application](/img/94/b93d5fb6d9e3515a1f218cc4ec6eef.png)
[wechat applet development] (I) development environment and applet official account application

You can't access this shared folder because your organization's security policies prevent unauthenticated guests from accessing it. These policies can help protect your computer from unsafe or malicio

Wechat payment V3 version of openresty implementation and pit avoidance Guide (service side)

Figure New Earth: how the RVT format BIM model modeled by Revit can accurately match the map with texture

Decision tree - ID3, C4.5, cart

Full revolutionary Siamese networks for object tracking translation
随机推荐
The vision group of Hegong University Sky team trained Day1 - machine learning, and learned to use the Yolo model
Case practice - panoramic image mosaic: feature matching method
生成模型与判别模型
[internationalization] decimal point and comma of application development
DGL库中一些函数或者方法的介绍
Natural language processing Jieba
基于thinkphp将execle表格上传并插入数据库
jmeter中JSON提取器使用
[Yum] configuration and use of Yum source
Generative model and discriminant model
POJ3278抓住那头牛题解
Image feature SIFT (scale invariant feature transform)
FPGA综合项目——图像边缘检测系统
SIFT feature point extraction
Wxml template concise tutorial
Uva572 oil deposits problem solution
mysql使用explain分析sql执行计划帮助查找性能瓶颈
Go: Gin basicauth Middleware
MySQL -- subquery scalar subquery
Digital twin demonstration project -- Talking about simple pendulum (2) vision exploration and application scenarios
