欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信小程序功能之全屏滚动效果的实现代码

程序员文章站 2022-11-09 16:12:17
想做一个全屏滚动的效果,于是在网上找了一个差不多的例子,但是觉得有些地方不是很好,于是改进了一下; 先给大家展示下效果图,感觉不错,请参考实例代码。 代码:...

想做一个全屏滚动的效果,于是在网上找了一个差不多的例子,但是觉得有些地方不是很好,于是改进了一下;

先给大家展示下效果图,感觉不错,请参考实例代码。

微信小程序功能之全屏滚动效果的实现代码

代码:

wxml: 

<!-- 第一页 -- >
  <view id='hook1' class="section section01 {{scrollindex==0?'active':''}}" style='background:red' bindtouchstart="scrolltouchstart" bindtouchmove='scrolltouchmove' bindtouchend="scrolltouchend">
      <view class='cont'>
        <view class='cont-body'>
          <view>one</view>
        </view>
      </view>
    </view>
    <!-- 第二页 -->
    <view id='hook2' class="section section02 {{scrollindex==1?'active':''}}" style='background:pink' bindtouchstart="scrolltouchstart" bindtouchmove='scrolltouchmove' bindtouchend="scrolltouchend">
      <view class='cont'>
        <view class='cont-body'>
          <view>two</view>
        </view>
      </view>
    </view>
    <!-- 第三页 -->
    <view id='hook3' class="section section03 {{scrollindex==2?'active':''}}" style='background:blue' bindtouchstart="scrolltouchstart" bindtouchmove='scrolltouchmove' bindtouchend="scrolltouchend">
      <view class='cont'>
        <view class='cont-body'>
          <view>three</view>
        </view>
      </view>
    </view>
    <!-- 第四页 -->
    <view id='hook4' class="section section04 {{scrollindex==3?'active':''}}" style='background:green' bindtouchstart="scrolltouchstart" bindtouchmove='scrolltouchmove' bindtouchend="scrolltouchend">
      <view class='cont'>
        <view class='cont-body'>
          <view>foure</view>
        </view>
      </view>
    </view>

wxss:

page {
  height: 100%;
  background: fff;
  color: #282828;
}
.container {
  flex: 1;
  flex-direction: column;
  box-sizing: border-box;
  padding: 0;
  align-items: initial;
  justify-content: first baseline;
}
.container-fill {
  height: 100%;
  overflow: hidden;
}
.scroll-fullpage {
  height: 100%;
}
.section {
  height: 100%;
}
.cont {
  width: 100%;
  height: 100%;
  margin: 0 auto;
  position: relative;
}
.cont .cont-body {
  width: 75%;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

js:

page({
  /**
   * 页面的初始数据
   */
  data: {
      scrollindex: 0, // 当前页面的索引值
        totalnum: 4, // 总共页面数
        starty: 0, // 开始的位置x
        starttime: 0,  // 开始的时间戳
        endy: 0, // 结束的位置y
        endtime: 0,  // 结束的时间戳
        critical: 80, // 触发翻页的临界值
        maxtimecritical: 300,  // 滑动的时间戳临界值上限
        mintimecritical: 100,  // 滑动的时间戳临界值下限
        margintop: 0, // 滑动下拉距离
      currenttarget: null,  // 当前点击的元素的id
  },
  scrolltouchstart: function(e) {
    let py = e.touches[0].pagey,
      stamp = e.timestamp,
      currenttarget = e.currenttarget.id;
    console.log(py);
    this.setdata({
      starty: py,
      starttime: stamp,
      currenttarget: currenttarget
    })
  },
  scrolltouchmove(e) {
    // console.log(e);
  },
  scrolltouchend: function(e) {
    let py = e.changedtouches[0].pagey,
      stamp = e.timestamp,
      d = this.data,
      timestampdiffer = stamp - d.starttime;
    this.setdata({
      endy: py,
      endtime: stamp
    })
    console.log('开始:' + d.starty, '结束:' + e.changedtouches[0].pagey);
    console.log('时间戳之差: ' + timestampdiffer);
    if (timestampdiffer <= d.maxtimecritical && timestampdiffer > d.mintimecritical && (d.starty > e.changedtouches[0].pagey)) {
      let currenttarget = parseint(d.currenttarget.slice(4)),
        nexttarget = currenttarget + 1;
      const query = wx.createselectorquery();
      query.select(`#hook${nexttarget}`).boundingclientrect();
      query.selectviewport().scrolloffset();
      query.exec(function (res) {
        // console.log(res);
             if (res[0] != null) {
          wx.pagescrollto({
            scrolltop: res[0].height * currenttarget,
          })
        }
      })
      } else if (timestampdiffer <= d.maxtimecritical && timestampdiffer > d.mintimecritical && (d.starty < e.changedtouches[0].pagey)) {  // 下拉
      let currenttarget = parseint(d.currenttarget.slice(4)),
        pretarget = currenttarget - 2 == -1 ? 0 : currenttarget - 2;
        const query = wx.createselectorquery();
        query.select(`#hook1`).boundingclientrect();
        query.selectviewport().scrolloffset();
        query.exec(function (res) {
          console.log(res);
          wx.pagescrollto({
            scrolltop: res[0].height * pretarget
          })
        })
      }    
   },
})

总结

以上所述是小编给大家介绍的微信小程序功能之全屏滚动效果的实现代码,希望对大家有所帮助