美文网首页
用动画实现自定义轮播图

用动画实现自定义轮播图

作者: 一名有马甲线的程序媛 | 来源:发表于2018-03-05 11:50 被阅读142次

新接了一个做微信小程序的活,编码方式跟vue很相似,样式编写比普通css样式轻松的多,现要实现一个轮播图的效果。


image.png

请教了我的同学,她说小程序有两种方法能实现这个效果:

  1. 小程序自带的swiper组件,然后自己改样式;
  2. view,touchstart,touchmove,touchend事件和动画自己写。
    现在就分别来搞一下这个轮播~

方法一:小程序自带的swiper组件

微信小程序—swiper组件文档
wxml文件:

<swiper interval="{{interval}}" duration="{{duration}}" display-multiple-items="{{displayMultipleItems}}" previous-margin="{{previousMargin}}" next-margin="{{nextMargin}}">
  <block wx:for="{{imgUrls}}" wx:for-index="current">
    <swiper-item class="swiper-image">
      <image src="{{item}}" id="swiper{{current}}"  class="slide-image" width="355" height="150"/>
    </swiper-item>
  </block>
</swiper>

js文件:

Page({
  data: {
    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    displayMultipleItems:1,
    duration: 500, // 滑动动画时长
    previousMargin: 30,
    nextMargin:30
  }
})

法一实现出来的效果图是酱紫的:


法一效果图

方法二:用事件和动画自己写轮播

wxml文件:

<view style="white-space: nowrap;" class="box" bindtouchstart="touchstart" bindtouchmove="touchmove" bindtouchend="touchend">
  <view class="club" animation="{{animation1}}" bindtap="scrollLeft">
    <image src="{{clubs[0].image}}"/>
    <text>{{clubs[0].name}}</text>
  </view>
  <view class="club" animation="{{animation2}}" bindtap="scrollLeft">
    <image src="{{clubs[1].image}}"/>
    <text>{{clubs[1].name}}</text>
  </view>
  <view class="club" animation="{{animation3}}">
    <image src="{{clubs[2].image}}"/>
    <text>{{clubs[2].name}}</text>
  </view>
  <view class="club" animation="{{animation4}}" bindtap="scrollRight">
    <image src="{{clubs[3].image}}"/>
    <text>{{clubs[3].name}}</text>
  </view>
  <view class="club" animation="{{animation5}}" bindtap="scrollRight">
    <image src="{{clubs[4].image}}"/>
    <text>{{clubs[4].name}}</text>
  </view>
</view>

js文件:

//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    clubs: [{
      image: 'imgs/1.jpg',
      name:'111'
    },{
      image: 'imgs/2.jpg',
      name: '222'
    }, {
      image: 'imgs/3.jpg',
      name: '333'
    }, {
      image: 'imgs/4.jpg',
      name: '444'
    }, {
      image: 'imgs/5.jpg',
      name: '555'
    }]
  },

  //触摸开始事件
  touchstart: function (e) {
    console.log(e.touches[0].pageX);
    this.data.touchDot = e.touches[0].pageX;
    var that = this;
    this.data.interval = setInterval(function () {
      that.data.time += 1;
    }, 100);
  },
  //触摸移动事件
  touchmove: function (e) {
    let touchMove = e.touches[0].pageX;
    let touchDot = this.data.touchDot;
    let time = this.data.time;
    console.log("touchMove: " + touchMove + ", touchDot: " + touchDot + ", diff: " + (touchMove - touchDot));
    //向左滑动
    if (touchMove - touchDot <= -40 && time < 10 && !this.data.done) {
      console.log("向左滑动");
      this.data.done = true;
      this.scrollLeft();
    }
    //向右滑动
    if (touchMove - touchDot >= 40 && time < 10 && !this.data.done) {
      console.log("向右滑动");
      this.data.done = true;
      this.scrollRight();
    }
  },
  //触摸结束事件
  touchend: function (e) {
    clearInterval(this.data.interval);
    this.data.time = 0;
    this.data.done = false;
  },
  //向左滑动事件
  scrollLeft() {
    var animation1 = wx.createAnimation({
      duration: 300,
      timingFunction: "linear",
      delay: 0
    })
    var animation2 = wx.createAnimation({
      duration: 300,
      timingFunction: "linear",
      delay: 0
    })
    var animation3 = wx.createAnimation({
      duration: 300,
      timingFunction: "linear",
      delay: 0
    })
    var animation4 = wx.createAnimation({
      duration: 300,
      timingFunction: "linear",
      delay: 0
    })
    var animation5 = wx.createAnimation({
      duration: 300,
      timingFunction: "linear",
      delay: 0
    })

    this.animation1 = animation1;
    this.animation2 = animation2;
    this.animation3 = animation3;
    this.animation4 = animation4;
    this.animation5 = animation5;

    this.animation1.translateX(-60).opacity(0).step();
    this.animation2.translateX(-140).opacity(0.5).scale(0.8, 0.8).step();
    this.animation3.translateX(-110).opacity(0.5).scale(1, 1).step();
    this.animation4.translateX(-70).opacity(1).scale(1.4, 1.4).step();
    this.animation5.translateX(-30).opacity(0.5).scale(1, 1).step();


    this.setData({
      animation1: animation1.export(),
      animation2: animation2.export(),
      animation3: animation3.export(),
      animation4: animation4.export(),
      animation5: animation5.export()
    })

    var that = this;
    setTimeout(function () {
      that.animation1.translateX(-50).opacity(0.2).scale(0.8, 0.8).step({ duration: 0, timingFunction: 'linear' });
      that.animation2.translateX(-40).opacity(0.5).scale(1, 1).step({ duration: 0, timingFunction: 'linear' });
      that.animation3.translateX(0).opacity(1).scale(1.4, 1.4).step({ duration: 0, timingFunction: 'linear' });
      that.animation4.translateX(40).opacity(0.5).scale(1, 1).step({ duration: 0, timingFunction: 'linear' });
      that.animation5.translateX(50).opacity(0.2).scale(0.8, 0.8).step({ duration: 0, timingFunction: 'linear' });
      that.setData({
        animation1: animation1.export(),
        animation2: animation2.export(),
        animation3: animation3.export(),
        animation4: animation4.export(),
        animation5: animation5.export()
      })
    }.bind(this), 195)

    let array = this.data.clubs;
    let shift = array.shift();
    array.push(shift);

    setTimeout(function () {
      this.setData({
        clubs: array
      })
    }.bind(this), 195)
  },

  //向右滑动事件
  scrollRight() {
    var animation1 = wx.createAnimation({
      duration: 300,
      timingFunction: "linear",
      delay: 0
    })
    var animation2 = wx.createAnimation({
      duration: 300,
      timingFunction: "linear",
      delay: 0
    })
    var animation3 = wx.createAnimation({
      duration: 300,
      timingFunction: "linear",
      delay: 0
    })
    var animation4 = wx.createAnimation({
      duration: 300,
      timingFunction: "linear",
      delay: 0
    })
    var animation5 = wx.createAnimation({
      duration: 300,
      timingFunction: "linear",
      delay: 0
    })

    this.animation1 = animation1;
    this.animation2 = animation2;
    this.animation3 = animation3;
    this.animation4 = animation4;
    this.animation5 = animation5;

    this.animation1.translateX(30).opacity(0.5).scale(1, 1).step();
    this.animation2.translateX(70).opacity(1).scale(1.4, 1.4).step();
    this.animation3.translateX(110).opacity(0.5).scale(1, 1).step();
    this.animation4.translateX(120).opacity(0.2).scale(0.8, 0.8).step();
    this.animation5.translateX(130).opacity(0).step();


    this.setData({
      animation1: animation1.export(),
      animation2: animation2.export(),
      animation3: animation3.export(),
      animation4: animation4.export(),
      animation5: animation5.export()
    })

    var that = this;
    setTimeout(function () {
      that.animation1.translateX(-50).opacity(0.2).scale(0.8, 0.8).step({ duration: 0, timingFunction: 'linear' });
      that.animation2.translateX(-40).opacity(0.5).scale(1, 1).step({ duration: 0, timingFunction: 'linear' });
      that.animation3.translateX(0).opacity(1).scale(1.4, 1.4).step({ duration: 0, timingFunction: 'linear' });
      that.animation4.translateX(40).opacity(0.5).scale(1, 1).step({ duration: 0, timingFunction: 'linear' });
      that.animation5.translateX(50).opacity(0.2).scale(0.8, 0.8).step({ duration: 0, timingFunction: 'linear' });
      that.setData({
        animation1: animation1.export(),
        animation2: animation2.export(),
        animation3: animation3.export(),
        animation4: animation4.export(),
        animation5: animation5.export()
      })
    }.bind(this), 195)

    let array = this.data.clubs;
    let pop = array.pop();
    array.unshift(pop);

    setTimeout(function () {
      this.setData({
        clubs: array
      })
    }.bind(this), 195)
  }
})

wxss文件:

.box {
  height: 340rpx;
  z-index: 0;
  margin: 50rpx 0;
}
.box .club {
  height: 140rpx;
  width: 140rpx;
  position: relative;
  display: inline-block;
}
.club image {
  height: 140rpx;
  width: 140rpx;
}
.club text {
  display: block;
  width: 100%;
  font-size: 24rpx;
  line-height: 40rpx;
  text-align: center;
}
.box .club:nth-child(1) {
  transform: scale(0.8, 0.8) translateX(-120rpx);
  opacity: 0.2;
  z-index: 10;
}
.box .club:nth-child(2) {
  transform: scale(1,1) translateX(-80rpx);
  opacity: 0.5;
  z-index: 100;
}
.box .club:nth-child(3) {
  transform: scale(1.4,1.4);
  z-index: 1000;
}
.box .club:nth-child(4) {
  transform: scale(1,1) translateX(80rpx);
  opacity: 0.5;
  z-index: 100;
}
.box .club:nth-child(5) {
  transform: scale(0.8, 0.8) translateX(120rpx);
  opacity: 0.2;
  z-index: 10;
}
.box .club:nth-child(1) text,
.box .club:nth-child(2) text,
.box .club:nth-child(4) text,
.box .club:nth-child(5) text{
  visibility: hidden;
}

用法二实现出来的效果图是酱紫的:

法二效果图
两种都不完美,但根据代码量及复杂程度,我准备用“法一”来写这个效果。如果不是微信小程序,用jquery写这种轮播图,有现成的swiper插件,用起来也是得心应手~ jquery+swiper实现三图轮播效果

相关文章

  • 用动画实现自定义轮播图

    新接了一个做微信小程序的活,编码方式跟vue很相似,样式编写比普通css样式轻松的多,现要实现一个轮播图的效果。 ...

  • 自定义轮播图-简单不需要引用第三方库

    简单的轮播图,实现自定义轮播icon,带标题,自动轮播,无限轮播 1、自定义属性attr.xml 2、自定义Ban...

  • 2019-06-27 @于志程

    Jquery 动态实现3D轮播效果 (简版) 先看一下效果图 我使用的是animate 自定义动画效果实现 首先定...

  • 项目-轮播图

    整个轮播图分为三部分:轮播指标、轮播项目及轮播导航。用boostrap实现轮播图要比用js、jQuery方便的多,...

  • Jquery 轮播图制作

    轮播图制作主要思路:实现轮播图滚动的效果,主要是使用animate()函数来实现这个过渡的动画效果。当向左点击时,...

  • 轮播图

    间歇播图 思路: 1,用动画来改变ul的高度值; 1,获取ul 2,改变高度值 呼吸轮播图 思路 -用透明度来实现...

  • 使用UICollectionView实现的swift轮播图FYS

    使用UICollectionView实现的swift轮播图FYSliderView 带有动画效果的pageCont...

  • 用动画做图片轮播

    图片轮播的新方法,用动画实现轮播: 1.将需要轮播的图片用标签放在同一位置; 2.通过改变各个图片的透明度实现轮播...

  • iOS轮播图

    用collectionView自定义实现一个无限滚动的轮播图 这里主要说一下无限轮播的核心思路和解决方案 我先以s...

  • Flutter学习六之实现一个带筛选的列表页面

    上期实现了一个网络轮播图的效果,自定义了一个轮播图组件,继承自StatefulWidget,我们知道Flutter...

网友评论

      本文标题:用动画实现自定义轮播图

      本文链接:https://www.haomeiwen.com/subject/nkocfftx.html