微信小程序设置scroll-view高度并设置点击事件获取点击位置

(1)使用scroll-view必须设置高度,如何占用出去header之外的空间。
(2)如何获取scroll-view列表点击的具体位置(不考虑bindtap,catchtap)

<view>
新闻界面展示
</view>
<scroll-view scroll-y = "{{true}}" style="height:{{windowHeight}}px" bindscrolltoupper="upper" bindscrolltolower="lower">
<block wx:for = "{{newslist}}">
<view style='width:100%' bindtap='clickitem' data-index="{{index}}">
<image src='{{item.newsimg}}' style='width:100px;height:100px;overflow:hidden;display:inline-block;float:left'></image>
<view style='float:left;width:250px'>
<text style='display:block'>title   :{{item.newstitle}}</text>
<text style='display:block'>author  :{{item.newsauthor}}</text>
<text style='display:block'>time    :{{item.newstime}}</text>
</view>
<view style='clear:both;display:block;height:10px;background-color:white'/>
</view>
</block>

</scroll-view>
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowHeight:100,
    newslist:[
      {newstitle:"title1",
        newsimg: "https://ss0.baidu.com/73x1bjeh1BF3odCf/it/u=2839142893,408396789&fm=85&s=72B9616CAEE5A37650E7B08B0200E08A",
        newsauthor:"zuozhe1",
        newstime:"newstime1"},
      {
        newstitle: "title2",
        newsimg: "https://ss0.baidu.com/73x1bjeh1BF3odCf/it/u=2839142893,408396789&fm=85&s=72B9616CAEE5A37650E7B08B0200E08A",
        newsauthor: "zuozhe2",
        newstime: "newstime2"
      },
      {
        newstitle: "title3",
        newsimg: "https://ss0.baidu.com/73x1bjeh1BF3odCf/it/u=2839142893,408396789&fm=85&s=72B9616CAEE5A37650E7B08B0200E08A",
        newsauthor: "zuozhe3",
        newstime: "newstime3"
      },
      {
        newstitle: "title4",
        newsimg: "https://ss0.baidu.com/73x1bjeh1BF3odCf/it/u=2839142893,408396789&fm=85&s=72B9616CAEE5A37650E7B08B0200E08A",
        newsauthor: "zuozhe4",
        newstime: "newstime4"
      },
      {
        newstitle: "title5",
        newsimg: "https://ss0.baidu.com/73x1bjeh1BF3odCf/it/u=2839142893,408396789&fm=85&s=72B9616CAEE5A37650E7B08B0200E08A",
        newsauthor: "zuozhe5",
        newstime: "newstime5"
      },
      {
        newstitle: "title16",
        newsimg: "https://ss0.baidu.com/73x1bjeh1BF3odCf/it/u=2839142893,408396789&fm=85&s=72B9616CAEE5A37650E7B08B0200E08A",
        newsauthor: "zuozhe6",
        newstime: "newstime6"
      },
      {
        newstitle: "title7",
        newsimg: "https://ss0.baidu.com/73x1bjeh1BF3odCf/it/u=2839142893,408396789&fm=85&s=72B9616CAEE5A37650E7B08B0200E08A",
        newsauthor: "zuozhe7",
        newstime: "newstime7"
      },
      {
        newstitle: "title18",
        newsimg: "https://ss0.baidu.com/73x1bjeh1BF3odCf/it/u=2839142893,408396789&fm=85&s=72B9616CAEE5A37650E7B08B0200E08A",
        newsauthor: "zuozhe8",
        newstime: "newstime8"
      },
      {
        newstitle: "title19",
        newsimg: "https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=2545785285,72806503&fm=173&app=25&f=JPEG?w=218&h=146&s=DBC004C60CB39E514A27E23C03008049",
        newsauthor: "zuozhe9",
        newstime: "newstime9"
      },
      {
        newstitle: "title10",
        newsimg: "https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=2545785285,72806503&fm=173&app=25&f=JPEG?w=218&h=146&s=DBC004C60CB39E514A27E23C03008049",
        newsauthor: "zuozhe10",
        newstime: "newstime10"
      },
      {
        newstitle: "title11",
        newsimg: "https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=2545785285,72806503&fm=173&app=25&f=JPEG?w=218&h=146&s=DBC004C60CB39E514A27E23C03008049",
        newsauthor: "zuozhe11",
        newstime: "newstime11"
      },
      
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //设置scroll-view高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  upper:function(){
    console.log("======uppperupper========");
  },
  lower:function(e){
    console.log("========lowerlower============");
  },
  clickitem:function(e){
    var position = e.currentTarget.dataset.index;
    console.log("================"+position);
  }

})

解决:
(1)

 onLoad: function (options) {
    var that = this;
    //设置scroll-view高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
        //此处获取可用屏幕高度,顶部还有其他view,直接减去其他view的高度就是scrollview的高度。
          windowHeight: res.windowHeight
        });
      }
    });
  },

(2)

<view style='width:100%' bindtap='clickitem' data-index="{{index}}">
 clickitem:function(e){
    var position = e.currentTarget.dataset.index;
    console.log("================"+position);
  }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值