微信小程序左右滑动切换图片酷炫效果
开门见山,先上效果吧!感觉可以的用的上的再往下看。

心动吗?那就继续往下看!
先上页面结构吧,也就是wxml文件,其实可以理解成微信自己封装过的html,这个不多说了,不懂也没必要往下看了。
<scroll-view class="scroll-view_H" scroll-x scroll-with-animation style="width: 100%;height:{{windowHeight}}px" bindscroll="getSelectItem">
<block wx:for="{{proList}}" wx:key="unique" wx:for-index="id" wx:for-item="item">
<view class="scroll_item {{item.selected ? 'selected' : ''}}" data-index='{{item.index}}' bindtap='selectProItem'>
<view class='proImg'><image src="{{item.proUrl}}" class="slide-image" mode="widthFix"/></view>
<view class='detailBox'>
<view class='proTitle'>{{item.proTitle}}</view>
<view class='proDec'>{{item.proDec}}</view>
<view class='proPrice'>¥{{item.proPrice}}</view>
<navigator class='detailLink' url="../detail/detail?id={{item.id}}">查看详情 ></navigator>
</view>
</view>
</block>
</scroll-view>
做该效果样式就不多说了,一个默认状态样式,一个当前选中样式。(开发小程序的时候,注意class的命名,尽量不要使用层级嵌套,所以class取名的时候要注意唯一性)
.scroll-view_H{
width: 100%;
text-align: center;
white-space: nowrap;
}
.scroll_item {
position: relative;
width: 84%;
margin: 0 1%;
left: -2%;
display: inline-block;
border-radius: 20rpx !important ;
overflow: hidden;
transform: scale(0.9);
box-shadow: 0 0 10px #ccc;
vertical-align: top;
top: 5%;
height: 72%;
background-color: #fff;
}
.scroll_item:first-child{
margin-left: 8%;
left: 0;
}
.scroll_item:last-child{
margin-right: 8%;
left: 0;
}
.scroll_item.selected{
transform: scale(1);
border: solid 1px #ffcd54;
}
.scroll_item .proImg{
border-top-left-radius: 20rpx;
border-top-right-radius: 20rpx;
width: 100%;
max-height: 200rpx;
position: absolute;
top: 0;
left: 0;
z-index: 0;
}
.scroll_item image {
width: 100%;
float: left;
border-top-left-radius: 20rpx;
border-top-right-radius: 20rpx;
}
.detailBox {
padding: 30rpx 5% 30rpx 5%;
float: left;
width: 90%;
border-bottom-left-radius: 20rpx;
border-bottom-right-radius: 20rpx;
position: absolute;
bottom: 0;
left: 0;
z-index: 1;
background: #fff;
}
.proTitle {
font-size: 36rpx;
color: #666;
line-height: 50rpx;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.proDec {
font-size: 30rpx;
color: #999;
line-height: 50rpx;
}
.proPrice {
font-size: 48rpx;
color: #CA414C;
line-height: 90rpx;
}
.detailLink{
color: #666;
font-size: 30rpx;
}
js部分:该效果基于小程序的组件 scroll-view 开发的,利用bindscroll事件加载回调函数。
回调事件原理:
通过滚动宽度和每个item的宽度从而获取当前滚动的item是第几位,然后给对应的item加上选中class,其他的则去除选中class。
//滑动获取选中商品
getSelectItem:function(e){
var that = this;
var itemWidth = e.detail.scrollWidth / that.data.proList.length;//每个商品的宽度
var scrollLeft = e.detail.scrollLeft;//滚动宽度
var curIndex = Math.round(scrollLeft / itemWidth);//通过Math.round方法对滚动大于一半的位置进行进位
for (var i = 0, len = that.data.proList.length; i < len; ++i) {
that.data.proList[i].selected = false;
}
that.data.proList[curIndex].selected = true;
that.setData({
proList: that.data.proList,
giftNo: this.data.proList[curIndex].id
});
},
ps:有时候一些酷炫的效果其实实现起来非常简单,建议开发前先理清实现思路,虽然整个文章言简意赅,能看懂就自然懂,乐在分享。
微信小程序左右滑动切换图片酷炫效果的更多相关文章
- 微信小程序左右滑动切换图片酷炫效果(附效果)
开门见山,先上效果吧!感觉可以的用的上的再往下看. 心动吗?那就继续往下看! 先上页面结构吧,也就是wxml文件,其实可以理解成微信自己封装过的html,这个不多说了,不懂也没必要往下看了. < ...
- 微信小程序左右滑动切换页面示例代码--转载
微信小程序——左右滑动切换页面事件 微信小程序的左右滑动触屏事件,主要有三个事件:touchstart,touchmove,touchend. 这三个事件最重要的属性是pageX和pageY,表示X, ...
- 微信小程序写tab切换
微信小程序之tab切换效果,如图: 最近在学习微信小程序并把之前的公司app搬到小程序上,挑一些实现效果记录一下(主要是官方文档里没说的,毕竟官方文档只是介绍功能) .wxml代码: <view ...
- 微信小程序--canvas画布实现图片的编辑
技术:微信小程序 概述 上传图片,编辑图片大小,添加文字,改变文字颜色等 详细 代码下载:http://www.demodashi.com/demo/14789.html 概述 微信小程序--ca ...
- 微信小程序Tab选项卡切换大集合
代码地址如下:http://www.demodashi.com/demo/14028.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...
- 微信小程序:本地资源图片无法通过 WXSS 获取,可以使用网络图片或者 base64或者使用image标签
微信小程序:本地资源图片无法通过 WXSS 获取,可以使用网络图片或者 base64或者使用image标签 一.问题 报错信息: VM696:2 pages/user/user.wxss 中的本地资源 ...
- 微信小程序缓存滑动距离,当页面浏览到一定位置,滑动其他页面后返回该页面记录之前的滑动距离
15.微信小程序缓存滑动距离 我们在浏览页面的时候,然后左滑或者右滑到新的页面,等返回此页面,我们希望可以记录上次滑动的距离 虽然这个实现起来并不难,但是会遇到一些坑,因为scroll-view的组件 ...
- 微信小程序-显示外链图片 bug
微信小程序-显示外链图片 bug 显示外链图片 bug 403 bug 禁止外链,未授权 https://httpstatuses.com/403 image component 图片.支持 JPG. ...
- 微信小程序手势滑动卡片案例
最近工作中有项目要使用微信小程序技术进行开发,其中一项功能困扰了我很久,卡片滑动动效以及手势识别.经过一番研究和参考,现在把成果展示.记录自己踩到的坑,如果大家有需要,也可以帮助到大家. 效果图: 首 ...
随机推荐
- mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类
相信大家在刚开始学习mybatis注解方式,或者spring+mybatis注解方式的时候,一定会有一个疑问,为什么mybatis的dao接口只需要一个接口,不需要实现类,就可以正常使用,笔者最开始的 ...
- Flask知识点一
1 flask安装 pip3 install falsk 一Werkzeug Werkzeug是什么? Werkzeug就是Python对WSGI的实现的一个通用库,它是Flask所使用的底层WSGI ...
- ionic2 安装(一)
1.安装java JDK 2.安装nodejs 3.安装最新版ionic 指令:npm install ionic@latest 4.安装cordova 指令:npm install -g cordo ...
- python之 模块与包
一. 模块 1.模块定义: 将代码量较大的程序分割成多个有组织的.彼此独立但又能互相交互的代码片段,这些自我包含的有组织的代码段就是模块. 2.模块分类: a.标准库(又称内置模块) b.开源模块(又 ...
- 三目运算:and/or 技巧
三目运算:and/or 技巧 and, or 联合起来有个小技巧: print 2 < 3 and True or False 说明: 如果 2 小于 3 了,则输出 True , ...
- macOS 中 apache vhosts 配置备忘
1. 修改 apache 服务器指向的根目录 macOS 默置了 apache,有以下几个常用命令: sudo apachectl -v // 查看 apache 版本 httpd -v // 同上 ...
- IdentityServer Topics(6)- Windows身份验证
在支持的平台上,您可以让IdentityServer使用Windows身份验证(例如,对Active Directory)对用户进行身份验证. 当您使用以下身份托管IdentityServer时,当前 ...
- 一个通用的makefile(一)
最近在编写Android编译系统时,需要遍历每一个目录下每一个文件夹下的makefile,网上的方法有些繁琐 :就直接贴上自己遍历子目录深度为1:(for temporary)(之后会继续更新) 下 ...
- Android基础_一次上传多张图片
获取权限 <uses-permission android:name="android.permission.CAMERA"/> <uses-permission ...
- HDU 2296:Ring
Problem Description For the hope of a forever love, Steven is planning to send a ring to Jane with a ...