国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

微信小程序之判斷頁面滾動方向的示例代碼

2019-11-19 13:06:19
字體:
供稿:網(wǎng)友

微信小程序中如果判斷頁面滾動方向?

解決方案

1.用到微信小程序API

獲取頁面實際高度 nodesRef.boundingClientRect([callback])

監(jiān)聽用戶滑動頁面事件onPageScroll

2.獲取頁面實際高度

<!--WXML--><view id="box">  <view class="list" wx:for="{{List}}" wx:key="List{{index}}">    <image mode='aspectFill' class='list_img' src="{{item.imgUrl}}" ></image>  </view></view>
  /* JS */ // 封裝函數(shù)獲取ID為box的元素實際高度  getScrollHeight: function() {  wx.createSelectorQuery().select('#box').boundingClientRect((rect) => {   this.setData({    scrollHeight: rect.height   })   console.log(this.data.scrollHeight)  }).exec() }, // 假設(shè)數(shù)據(jù)請求 getDataList: function() {  wx.request({   url: 'test.php', //僅為示例,并非真實的接口地址   success: function(res) {   // 如果該元素下面的數(shù)據(jù)是動態(tài)獲取的,此方法在wx.request請求成功的回調(diào)函數(shù)中調(diào)用    this.getScrollHeight()   }  }) },

3.監(jiān)聽用戶滑動頁面事件

  //監(jiān)聽用戶滑動頁面事件 onPageScroll: function(e) {    if (e.scrollTop <= 0) {   // 滾動到最頂部   e.scrollTop = 0;  } else if (e.scrollTop > this.data.scrollHeight) {   // 滾動到最底部   e.scrollTop = this.data.scrollHeight;  }  if (e.scrollTop > this.data.scrollTop || e.scrollTop >= this.data.scrollHeight) {   //向下滾動    console.log('向下 ', this.data.scrollHeight)  } else {   //向上滾動    console.log('向上滾動 ', this.data.scrollHeight)  }  //給scrollTop重新賦值   this.setData({   scrollTop: e.scrollTop  }) },

PS:微信小程序滾動到某個位置改變效果

<scroll-view><view>Some of the words<view><view bindscroll="scroll" class="{{variable>200 ? 'class1' : 'class2'}}"</view></scroll-view>
//JS文件 //滾動監(jiān)聽 scroll: function (e) { this.setData({  scrollTop:e.detail.scrollTop }) }

其中,variable為全局變量,class1、class2即為相應(yīng)的css

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 诸暨市| 永胜县| 诸暨市| 资源县| 高阳县| 东乌珠穆沁旗| 鲁山县| 临潭县| 赞皇县| 明溪县| 塔城市| 酒泉市| 闻喜县| 丹东市| 高淳县| 鄯善县| 三台县| 上虞市| 浦县| 云南省| 临清市| 巧家县| 剑川县| 民勤县| 民县| 绥棱县| 翁源县| 泉州市| 乌拉特前旗| 唐山市| 星座| 武定县| 舒兰市| 达州市| 龙岩市| 宜兴市| 尚义县| 沧源| 忻州市| 方山县| 鹤壁市|