微信小程序swiper实现 句子控app首页滑动卡片
微信小程序swiper实现 句子控app首页滑动卡片
引言:最近看到句子控APP首页的效果很清新,可是发现他的微信小程序端没有实现这个功能,我看了一下难度不大,于是尝试着去实现。
实现效果如下:

1、定义一个yiyancard自定义组件,在根目录下新建一个components文件夹并在其内部新建一个yiyancard文件夹。
2、在pages文件夹下新建一个home页面
3、在home页面的json引入yiyancard组件,并在wxml中使用
index.json
{
"usingComponents": {
"s-yiyancard": "../../components/yiyancard/index"
}
}
index.wxml
<view class="container">
<s-yiyancard style="width:100vw"> </s-yiyancard>
</view>
4、编写yiyancard相关代码
index.js
Page({
data: {
cardCur: 0,
swiperList: [{
id: 0,
type: 'image',
url: 'https://7368-shuyuabn-9gke6t6k962d48ca-1304045188.tcb.qcloud.la/image/6.JPG?sign=39d14c3902ca802c5bbdca9487c4dfc8&t=1612537023',
yiyan: '没有无聊的人生,只有无聊的人生态度',
form: '刘瑜',
iflike: "false"
}, {
id: 1,
type: 'image',
url: 'https://7368-shuyuabn-9gke6t6k962d48ca-1304045188.tcb.qcloud.la/image/1.JPG?sign=6a578b89d06a74141a01b35b26684e04&t=1612536951',
yiyan: '没有任何一种逃避能得到赞赏,喜欢就去追,饿了就吃饭,管他是失败或是发胖',
form: '',
iflike: "false"
}, {
id: 2,
type: 'image',
url: 'https://7368-shuyuabn-9gke6t6k962d48ca-1304045188.tcb.qcloud.la/image/2.JPG?sign=42c68e97e5fcd277e42bdc476a94cdb4&t=1612536964',
yiyan: '万物皆有裂痕,那是光进来的地方',
iflike: "false"
}, {
id: 3,
type: 'image',
url: 'https://7368-shuyuabn-9gke6t6k962d48ca-1304045188.tcb.qcloud.la/image/3.JPG?sign=d607ee45937f227f8ae5fba4e9f846f4&t=1612536974',
yiyan: '不听命于自己者,就要受命于他人',
form: '尼采',
iflike: "false"
}, {
id: 4,
type: 'image',
url: 'https://7368-shuyuabn-9gke6t6k962d48ca-1304045188.tcb.qcloud.la/image/4.JPG?sign=4c986f27559352bb5d9b42a96851ab22&t=1612536983',
yiyan: '我从不曾崩溃瓦解,因为我从不曾完好无缺',
form: '安迪·沃霍尔',
iflike: "false"
}, {
id: 5,
type: 'image',
url: 'https://7368-shuyuabn-9gke6t6k962d48ca-1304045188.tcb.qcloud.la/image/5.JPG?sign=22d43fff1c66594770c1c2b10cd8403c&t=1612537004',
yiyan: '总有人找你这可小星球,了解你的温柔和璀璨,即使旁边的宇宙再浪漫',
form: '',
iflike: "false"
}, {
ifend: true
}],
},
onLoad() {
this.towerSwiper('swiperList');
// 初始化towerSwiper 传已有的数组名即可
},
DotStyle(e) {
this.setData({
DotStyle: e.detail.value
})
},
// cardSwiper
cardSwiper(e) {
this.setData({
cardCur: e.detail.current
})
},
// towerSwiper
// 初始化towerSwiper
towerSwiper(name) {
let list = this.data[name];
for (let i = 0; i < list.length; i++) {
list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
list[i].mLeft = i - parseInt(list.length / 2)
}
this.setData({
swiperList: list
})
},
// towerSwiper触摸开始
towerStart(e) {
this.setData({
towerStart: e.touches[0].pageX
})
},
// towerSwiper计算方向
towerMove(e) {
this.setData({
direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
})
},
// towerSwiper计算滚动
towerEnd(e) {
let direction = this.data.direction;
let list = this.data.swiperList;
if (direction == 'right') {
let mLeft = list[0].mLeft;
let zIndex = list[0].zIndex;
for (let i = 1; i < list.length; i++) {
list[i - 1].mLeft = list[i].mLeft
list[i - 1].zIndex = list[i].zIndex
}
list[list.length - 1].mLeft = mLeft;
list[list.length - 1].zIndex = zIndex;
this.setData({
swiperList: list
})
} else {
let mLeft = list[list.length - 1].mLeft;
let zIndex = list[list.length - 1].zIndex;
for (let i = list.length - 1; i > 0; i--) {
list[i].mLeft = list[i - 1].mLeft
list[i].zIndex = list[i - 1].zIndex
}
list[0].mLeft = mLeft;
list[0].zIndex = zIndex;
this.setData({
swiperList: list
})
}
},
// 喜欢图标点击改变
like: function (event) {
const that = this
let likeid = event.currentTarget.dataset.likeid
var a = `swiperList[${likeid}].iflike`
var b =`that.data.swiperList[${likeid}].iflike`
console.log(a)
console.log(b)
if (that.data.swiperList[likeid].iflike=== "false") {
that.setData({
[a] : "true",
})
} else if (that.data.swiperList[likeid].iflike=== "true") {
that.setData({
[a]: "false",
})
}
console.log(that.data.swiperList[likeid].iflike)
}
})
index.json
{
"component": true,
"usingComponents": {}
}
<swiper class="card-swiper " duration="500" bindchange="cardSwiper">
<swiper-item wx:for="{{swiperList}}" wx:key class="{{cardCur==index?'cur':''}}">
<block wx:if="{{!item.ifend}}" >
<view class="swiper-item other ">
<image src="{{item.url}}" mode="aspectFill" class="swiper-img"></image>
<view class="yiyan-body">{{item.yiyan}}</view>
<view wx:if="{{item.form}}" class="yiyan-form">- {{item.form}} -</view>
<view wx:if="{{!item.form}}" class="zhanwei"></view>
<view class="bottom-box">
<view class="like juzhong" bindtap="like" data-likeid="{{item.id}}">
<block wx:if="{{item.iflike =='false'}}">
<image class="icon-img" src="../../images/icon/xihuan.png"></image>
</block>
<block wx:if="{{item.iflike =='true'}}">
<image class="icon-img" src="../../images/icon/xihuan2.png"></image>
</block>
<view class="num">12k</view>
</view>
<view class="liuyan">
<image class="icon-img2" src="../../images/icon/liuyan.png"></image>
<view class="num">36</view>
</view>
<view class="biaoqian">
<image class="icon-img" src="../../images/icon/biaoqian.png"></image>
</view>
<view class="zhuanfa">
<image class="icon-img" src="../../images/icon/zhuanfa.png"></image>
</view>
</view>
</view>
</block>
<block wx:if="{{item.ifend}}">
<view class="swiper-item other juzhong">
<view class="end-title">每日十句精选投稿</view>
<view class="end-body">
感谢支持,每天都有不同的收获。如果意犹未尽,可以点击底部按钮查看更多推荐
</view>
<view class="end-bottom"> 去发现页查看更多推荐</view>
</view> </block> </swiper-item>
</swiper>
index.css
/* ==================
轮播
==================== */ .swiper-img {
height: 35% !important; } .swiper-item image,
.swiper-item video {
width: 100%;
display: block;
height: 100%;
margin: 0;
pointer-events: none;
} .card-swiper { height: 85vh !important;
} .card-swiper swiper-item {
width: 610rpx !important;
left: 70rpx;
box-sizing: border-box;
padding: 40rpx 0rpx 70rpx;
overflow: initial;
} .card-swiper swiper-item .swiper-item {
width: 100%;
display: block;
height: 100%;
border-radius: 10rpx;
transform: scale(0.9);
transition: all 0.2s ease-in 0s;
overflow: hidden;
} .card-swiper swiper-item.cur .swiper-item {
transform: none;
transition: all 0.2s ease-in 0s;
} swiper {
margin-top: 15rpx !important;
} .other {
position: relative; background-color: #ffffff;
display: flex;
/*flex布局方法*/
flex-direction: column;
/*垂直布局*/
align-items: center;
/*水平方向居中*/ box-shadow: 0 6rpx 24rpx rgba(0, 0, 0, 0.08);
position: relative; } /* 内容 */
.yiyan-body {
line-height: 46rpx;
letter-spacing: 5rpx;
margin-top: 38rpx;
font-size: 30rpx;
width: 90%;
height: 35%;
margin-left: 5%;
color: #3e3e3e;
} .yiyan-form {
position: absolute;
bottom: 55px;
display: flex;
flex-direction: column;
align-items: center;
font-size: 25rpx;
color: #999999;
width: 50%;
margin-top: 20%;
margin-left: 25%; } /* 底部 */
.bottom-box {
position: absolute;
bottom: 0px;
width: 100%;
height: 80rpx;
display: flex;
flex-direction: row;
} .juzhong {
display: flex;
justify-content: center;
align-items: center; } .icon-img {
width: 30rpx !important;
height: 30rpx !important;
} .icon-img2 {
width: 30rpx !important;
height: 30rpx !important;
/* margin-top: 5rpx !important; */
} .like {
display: flex;
justify-content: center;
align-items: center;
width: 27%;
} .num {
font-size: smaller;
margin-top: 3rpx;
margin-left: 6rpx;
color: #b4b4b4;
} .liuyan {
width: 27%;
display: flex;
justify-content: center; align-items: center;
} .biaoqian {
width: 23%;
display: flex;
justify-content: center; align-items: center;
} .zhuanfa {
width: 23%;
display: flex;
justify-content: center; align-items: center;
}
/* 最后一页 */
.end-title{
width: 60%;
margin-left: 20%;
font-size: 30rpx;
display: flex;
justify-content: center;
align-items: center;
margin-top: 100rpx;
margin-bottom: 100rpx;
}
.end-body{
width: 80%;
margin-left: 10%;
font-size: 27rpx;
color: #a7a7a7;
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 40rpx;
}
.end-bottom{
position: absolute;
width: 70%;
left: 15%;
bottom: 60rpx;
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 40rpx;
color: #7b9fcb;
font-size: 28rpx;
}
编写完以上代码,相关功能就实现了。
注意:为了方便以上data中的数据是直接在js中自定义好的。
微信小程序swiper实现 句子控app首页滑动卡片的更多相关文章
- 微信小程序实质是什么? Hybrid App
微信小程序是一种不需要下载安装即可使用的应用,用户扫一扫或者搜一下即可打开应用.微信小程序实质是Hybrid技术的应用.Hybrid App(混合模式移动应用). 小程序能够更多的可以更多的调用手机本 ...
- 微信小程序基础之试图控件View、ScrollView、Swiper
今天写一篇关于微信小程序视图控件的文章,主要是介绍界面的搭建和部分操作js交互功能的介绍,转载请注明出处,谢谢~ 首先显示首页结构.创建三个navigator,用来跳转页面: <!--index ...
- 绑定bindchange事件的微信小程序swiper闪烁,抖动问题解决,(将微信小程序切换到后台一段时间,再打开微信小程序,会出现疯狂循环轮播,造成抖动现象)
微信小程序开发文档-组件-swiper后面追加的新闻如上图所示: 如果在bindchange事件给swiper的current属性对应的值{{current}}赋值,就会造成抖动现象. bindcha ...
- 我的微信小程序第三篇(app.json)
前言 端午节回家了,所以好多天没有更新,只想说还是待在家里舒服呀,妈妈各种做好吃的,小侄子侄女各种粘着我在室外玩,导致我三天下来不仅胖了一圈,还黑了一圈,上班第一天有同事就说我晒黑了,哭~~~,为了防 ...
- 微信小程序swiper制作内容高度不定的tab选项卡
微信小程序利用swiper制作内容高度不定的tab选项卡,不使用absolute定位,不定高度,由内容自由撑开主要思路是获取内容区的高度来给swiper动态设置值 .wxml <view cla ...
- 如何自定义微信小程序swiper轮播图面板指示点的样式
https://www.cnblogs.com/myboogle/p/6278163.html 微信小程序的swiper组件是滑块视图容器,也就是说平常我们看到的轮播图就可以用它来做,不过这个组件有很 ...
- 微信小程序swiper bindChange重复执行
swiper是微信小程序的一个滑动组件,非常重要.如果只是做简单的轮播图而不进行复杂的逻辑,直接可以使用,甚至不需要知道组件的方法.今天在做一个如下的页面时,快速滑动swiper出现了问题: 控制台打 ...
- 自定义微信小程序swiper轮播图面板指示点的样式
微信小程序的swiper组件是滑块视图容器,也就是说平常我们看到的轮播图就可以用它来做,不过这个组件有很多样式是固定的,但是,有时候我们的设计稿的面板指示点是需要个性化的,那么如何去修改swiper组 ...
- 微信小程序实现连接蓝牙设备跑步APP
背景 微信小程序兴起,有变成超级APP的趋势,通过微信提供的小程序api,可以通过微信调用到手机原生的支持. 目标 通过微信小程序实现来实现跑步类App的功能. 需求分析 跑步类App需要的两个核心的 ...
随机推荐
- library cache pin解决方法
library cache pin大部分都是因为编译存储过程造成的 查找造成问题的数据库对象(一般为存储过程) SELECT * FROM v$session_wait WHERE event = ' ...
- 性能测试工具locust简单应用
简介 Locust是一种易于使用的分布式用户负载测试工具.可用于对网站(或系统)负载测试,并依据响应数据计算出系统支持的并发用户数. 安装及调试(以下操作在windows环境下进行) Locust基于 ...
- Azure Terraform(六)Common Module
一,引言 之前我们在使用 Terraform 构筑一下 Azure 云资源的时候,直接将所以需要创建的资源全面写在 main.tf 这个文件中,这样写主要是为了演示使用,但是在实际的 Terrafor ...
- es_python_操作
获取es索引 https://www.itranslater.com/qa/details/2583886977221264384
- Eureka详解系列(二)--如何使用Eureka(原生API,无Spring)
简介 通过上一篇博客 Eureka详解系列(一)--先谈谈负载均衡器 ,我们知道了 Eureka 是什么以及为什么要使用它,今天,我们开始研究如何使用 Eureka. 在此之前,先说明一点.网上几乎所 ...
- spring data JPA 使用EntityentiListeners实现数据审计功能设计
当系统中有审计需求时,特别是需要对某些数据进行动态监控时,我们可以使用EntityentiListeners来实现,当然这是基于使用JPA而不是mybatis的情况下. 当前我们的需求场景: 1.需要 ...
- Python+Selenium+Unittest实现PO模式web自动化框架(1)
1.什么是PO模式? PO是Page Object的缩写 PO模式是自动化测试项目开发实践的最佳设计模式之一,讲页面定位和业务操作分开,也就是把对象的定位和测试脚本分开,从而提供可维护性. 主要有以下 ...
- 基于nginx的频率控制方案思考和实践
基于nginx的频率控制方案思考 标签: 频率控制 nginx 背景 nginx其实有自带的limit_req和limit_conn模块,不过它们需要在配置文件中进行配置才能发挥作用,每次有频控策略的 ...
- 如何设计一个亿级网关(API Gateway)?
1.背景 1.1 什么是API网关 API网关可以看做系统与外界联通的入口,我们可以在网关进行处理一些非业务逻辑的逻辑,比如权限验证,监控,缓存,请求路由等等. 1.2 为什么需要API网关 RPC协 ...
- P6584 重拳出击
写在前面 来给 zrm 大佬的题写一篇题解. 这题代码实现难度不高,但是比较锻炼思维,而且应该有不少种解法.着实是一道质量很高的题目. 算法思路 首先呢,显然当小 Z 向当前节点的一棵子树走去时,这棵 ...