微信小程序左右滑动切换图片酷炫效果(附效果) - 高丰鸣

gaofengming 2021-11-12 原文


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


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

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

  先上页面结构吧,也就是wxml文件,其实可以理解成微信自己封装过的html,这个不多说了,不懂也没必要往下看了。

 1  <scroll-view class="scroll-view_H" scroll-x scroll-with-animation style="width: 100%;height:{{windowHeight}}px" bindscroll="getSelectItem">
 2     <block wx:for="{{proList}}"  wx:key="unique" wx:for-index="id" wx:for-item="item">
 3       <view class="scroll_item {{item.selected ? \'selected\' : \'\'}}" data-index=\'{{item.index}}\' bindtap=\'selectProItem\'>
 4       <view class=\'proImg\'><image src="{{item.proUrl}}" class="slide-image"  mode="widthFix"/></view>
 5         <view class=\'detailBox\'>
 6             <view class=\'proTitle\'>{{item.proTitle}}</view>
 7             <view class=\'proDec\'>{{item.proDec}}</view>
 8             <view class=\'proPrice\'>¥{{item.proPrice}}</view>
 9             <navigator class=\'detailLink\'  url="../detail/detail?id={{item.id}}">查看详情 ></navigator>
10         </view>
11       </view>
12     </block>
13   </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:有时候一些酷炫的效果其实实现起来非常简单,建议开发前先理清实现思路,虽然整个文章言简意赅,能看懂就自然懂,乐在分享。

发表于
2018-01-31 13:33 
高丰鸣 
阅读(14706
评论(0
编辑 
收藏 
举报

 

版权声明:本文为gaofengming原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/gaofengming/p/8391783.html

微信小程序左右滑动切换图片酷炫效果(附效果) - 高丰鸣的更多相关文章

  1. 3dmax详细讲解全套攻略在线视频教程 – luenmicro

    3dmax详细讲解全套攻略在线视频教程 3dmax详细讲解全套攻略在线视频教程 课程目录 试学课 课时11MA […]...

  2. LeetCode 304. Range Sum Query 2D – Immutable 二维区域和检索 – 矩阵不可变(C++/Java)

    题目: Given a 2D matrix matrix, find the sum of the eleme […]...

  3. win7 – net 命令

    NET命令是Windows中的一个功能强大的工具。虽然必须用命令行方式执行,但它的功能确覆盖了Windows中 […]...

  4. winform ListView和DataGridView实现分页 – 广交天下好友

    winform ListView和DataGridView实现分页   用到的控件有   code: list […]...

  5. C语言入门—第七章 C语言函数 – 萧橘子

    C语言入门—第七章 C语言函数   函数就是一段封装好的,可以重复使用的代码,它使得我们的程序更加 […]...

  6. 日志统计分析 – 远方的人

    日志统计分析 今天闲来无事,想到一个问题: 日志,我们目前项目是使用logback做日志记录。。我想看一个服务 […]...

  7. Spring 定时器 定时访问数据库并发送邮件 – qgc

    Spring 定时器 定时访问数据库并发送邮件 check 我这里有两个案例的方法: 第一种:使用Spring […]...

  8. 手把手教你制作可以上线官方商店的微信动态表情包 – Leo雷

    手把手教你制作可以上线官方商店的微信动态表情包 01_Why do you make an expressio […]...

随机推荐

  1. sqlite3函数接口

    sqlite3函数接口(重点★): int sqlite3_open(const char*, sqlite3 […]...

  2. mac 查看python安装路径

    1、terminal :  input: which Python 2、terminal: input : p […]...

  3. Android颜色大全 – wgwyanfs

    Android颜色大全 Android颜色大全 颜  色    RGB值 英文名 中文名   #FFB6C1 […]...

  4. OAuth及第三方登录

            现在的生活中运用互联网的有好多地方,我们既要申请微博,申请博客,申请邮箱等等;哪怕登录一个小网 […]...

  5. 目标检测算法原理

    1.概述 1.1 目标检测的定义 识别图片中有哪些物体以及物体的位置(坐标位置)。 其中,需要识别哪些物体是人 […]...

  6. NodeJS模块

    NodeJS模块 惠善一的博客:https://huishanyi.club   &emsp;& […]...

  7. 用了三星Dex,我已经快一个月回家没开过电脑了 – Gary Zhang

    用了三星Dex,我已经快一个月回家没开过电脑了 用一根电缆将手机轻松与显示器,键盘,鼠标连接,享受电脑般的体验 […]...

  8. 加油站三维可视化监控系统,安全管理智慧运营

    国家经济的高速发展,使得国民的收入日益增强,对机动车辆的购买力也不断地增加。因此,加油站的数量也在不断增多,遍 […]...

展开目录

目录导航