开门见山,先上效果吧!感觉可以的用的上的再往下看。

  心动吗?那就继续往下看!

  先上页面结构吧,也就是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:有时候一些酷炫的效果其实实现起来非常简单,建议开发前先理清实现思路,虽然整个文章言简意赅,能看懂就自然懂,乐在分享。

微信小程序左右滑动切换图片酷炫效果的更多相关文章

  1. 微信小程序左右滑动切换图片酷炫效果(附效果)

    开门见山,先上效果吧!感觉可以的用的上的再往下看. 心动吗?那就继续往下看! 先上页面结构吧,也就是wxml文件,其实可以理解成微信自己封装过的html,这个不多说了,不懂也没必要往下看了. < ...

  2. 微信小程序左右滑动切换页面示例代码--转载

    微信小程序——左右滑动切换页面事件 微信小程序的左右滑动触屏事件,主要有三个事件:touchstart,touchmove,touchend. 这三个事件最重要的属性是pageX和pageY,表示X, ...

  3. 微信小程序写tab切换

    微信小程序之tab切换效果,如图: 最近在学习微信小程序并把之前的公司app搬到小程序上,挑一些实现效果记录一下(主要是官方文档里没说的,毕竟官方文档只是介绍功能) .wxml代码: <view ...

  4. 微信小程序--canvas画布实现图片的编辑

    技术:微信小程序   概述 上传图片,编辑图片大小,添加文字,改变文字颜色等 详细 代码下载:http://www.demodashi.com/demo/14789.html 概述 微信小程序--ca ...

  5. 微信小程序Tab选项卡切换大集合

    代码地址如下:http://www.demodashi.com/demo/14028.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

  6. 微信小程序:本地资源图片无法通过 WXSS 获取,可以使用网络图片或者 base64或者使用image标签

    微信小程序:本地资源图片无法通过 WXSS 获取,可以使用网络图片或者 base64或者使用image标签 一.问题 报错信息: VM696:2 pages/user/user.wxss 中的本地资源 ...

  7. 微信小程序缓存滑动距离,当页面浏览到一定位置,滑动其他页面后返回该页面记录之前的滑动距离

    15.微信小程序缓存滑动距离 我们在浏览页面的时候,然后左滑或者右滑到新的页面,等返回此页面,我们希望可以记录上次滑动的距离 虽然这个实现起来并不难,但是会遇到一些坑,因为scroll-view的组件 ...

  8. 微信小程序-显示外链图片 bug

    微信小程序-显示外链图片 bug 显示外链图片 bug 403 bug 禁止外链,未授权 https://httpstatuses.com/403 image component 图片.支持 JPG. ...

  9. 微信小程序手势滑动卡片案例

    最近工作中有项目要使用微信小程序技术进行开发,其中一项功能困扰了我很久,卡片滑动动效以及手势识别.经过一番研究和参考,现在把成果展示.记录自己踩到的坑,如果大家有需要,也可以帮助到大家. 效果图: 首 ...

随机推荐

  1. 【转】Install libimobiledevice on Mac OSX

    About the App App name: libimobiledevice App description: Library to communicate with iOS devices na ...

  2. 关于“System.Data.OleDb.OleDbException,外部数据库驱动程序 (1) 中的意外错误。”的解决方案

    网站之前运行一直很正常,但有一次用户在导入格式为xls的excel文件,发生了错误,跟踪错误后抛出如下的异常: 错误提示: 未处理System.Data.OleDb.OleDbException HR ...

  3. 由浅入深SCF无服务器云函数实践

    欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 作者:陈杰,腾讯云架构平台部技术专家 近年来,互联网服务从一开始的物理服务器托管,虚拟机,容器,发展到现在的云函数,逐步无服务器化,如下表所示. ...

  4. python入门之函数

    为什么要用函数 python的函数是由一个新的语句编写,即def ,def是可执行的语句--函数并不存在,知道python运行了def后才存在. 函数是通过赋值函数传递的,参数通过赋值传递给函数. d ...

  5. webp图像批量转换软件推荐——XnConvert

    XnConvert是一款简单易用的批量图像格式转换软件,其所支持图片格式有JPG.PNG.TIFF.GIF.RAW.JPEG2000.WebP.OpenEXR等等.你可以轻松的实现图像格式的转换.缩放 ...

  6. Oracle01——基本查询、过滤和排序、单行函数、多行函数和多表查询

    作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7272236.html Oracle的集群 Oracle的体系结构 SQL> --当 ...

  7. springboot项目启动时提示Address already in use: bind

    PS:web项目在启动的时候,一般会报Address already in use: bind,常规的处理思路为:删除任务管理器中的javaw.exe进程即可:当删除仍然解决不了时,一般处理思路如下, ...

  8. 学习笔记 - 用js判断页面是否加载完成实现代码

    用document.onreadystatechange的方法来监听状态改变, 然后用document.readyState == "complete"判断是否加载完成 docum ...

  9. Android应用程序使用两个LinearLayout编排5个Button控件

    学习存档: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:androi ...

  10. 【C#学习笔记之一】C#中的关键字

    C#中的关键字 关键字是对编译器具有特殊意义的预定义保留标识符.它们不能在程序中用作标识符,除非它们有一个 @ 前缀.例如,@if 是有效的标识符,但 if 不是,因为 if 是关键字. 下面是列出的 ...