微信小程序scroll-view(續)

scroll-view

使用scroll-view可滾動到對應區域,

注意: 使用豎向滾動時,需要給scroll-view一個固定高度,通過 WXSS 設置 height

這裏寫兩個應用場景的demo僅供參考,歡迎大神指點…

場景一:

項目中遇到需求:

  1. 頁面中有未知按鈕同一事件每一個按鈕所執行的業務不一樣】,
  2. 點擊當前按鈕觸發事件發送請求後端。數據回來後,頁面顯示相關數據並自動滑動對應區域

效果圖

在這裏插入圖片描述

wxml

 <!-- button -->
<view wx:for="{{getLocalPayData}}" wx:key="*this">
    <button class="" bindtap='startMeasure' id="NAV{{index}}" data-index="{{index}}" data-items='{{item}}' disabled="{{item.flagDisabled}}">
        {{item.flagText}}
    </button>
</view>


<!-- 內容 scroll-view -->
<scroll-view scroll-y scroll-with-animation="true" scroll-into-view="NAV{{toView}}" style="height: 400px;">
    <view class='measureDetailBox'>
        <view class='measureDetailList'>
            <view class='measureDetailItem' wx:for="{{saveAllSubmitData}}" wx:key="" id="NAV{{index}}">
                
            <!-- 其他內容 -->
                
            </view>
        </view>
    </view>
</scroll-view>

js

data: {
    toView: '', //初始化
}


/**
* 點擊開臺測量事件
*/
startMeasure: function(e) {
    // console.log('點擊按鈕獲取', e);
    var that = this;
    let currentIndex = e.currentTarget.dataset.index;
    console.log('當前index:' + currentIndex);

    that.setData({
        currentIndex: currentIndex,
    })

    //改變按鈕文本爲 "測量中"
    that.data.getLocalPayData.forEach((item, i) => {
        if (Number(i) == that.data.currentIndex) {
            var setBtnTxt = "getLocalPayData[" + i + "].flagText";
            that.setData({
                [setBtnTxt]: "測量中",
            })
        }
    })
}


/**
 * 提交事件
 * 後端數據回來渲染頁面並自動滾動到對應區域
 */
measureSubmit: function(paramVal) {
    var that = this;

    var params = {
        "orderitem_id": that.data.currentOrderId,
        "value1": paramVal.value1
    }

    http.postReq('users/gaugesends...', params, function(res) {
        console.log('測量成功返回的數據:', res);
        feedbackApi.hideTimerToast();

        if (res.code != 200) {
            feedbackApi.showToast({
                title: res.msg,
                mask: false
            })
            return
        }
		//保存數據
        let datas = res.data;
        //把每次提交的數據追加保存
        let allDatas = that.data.saveAllSubmitData.concat(datas);
        that.setData({
            saveAllSubmitData: allDatas,
        })
		
        //這裏使用setTimeout,因先渲染數據.
        setTimeout(() => {
            //改變當前按鈕狀態
            that.data.getLocalPayData.forEach((item, i) => {
                if (Number(i) == that.data.currentIndex) {
                    var setDisabled = "getLocalPayData[" + i + "].flagDisabled";
                    var setBtnTxt = "getLocalPayData[" + i + "].flagText";
                    that.setData({
                        [setDisabled]: "true", //禁用button
                        [setBtnTxt]: "獲取成功", //修改button文本
                        //保存當前index,賦值給scroll-into-view屬性,滾動到對應區域
                        toView: that.data.currentIndex, 
                    })
                }
            })
        }, 800)
    })
},

場景二:

已上傳過此demo, 橫縱向可滾動, 請看下圖

效果圖

下圖: 標題採用scroll-x橫向滾動,內容採用scroll-y縱向滾動,當點擊某個分類時滾動到對應區域

在這裏插入圖片描述

wxml

<!-- 使用scroll-view 滾動到對應位置 -->
<view class="scrollViewWrap">
    <!-- 分類 -->
    <view  class="cagetory-scroll">
        <scroll-view scroll-x="true" scroll-into-view="NAV{{status}}" scroll-with-animation="true">
            <text bindtap="getStatus" id="NAV{{index}}" class="cagetory-nav-li {{index === status ? 'cagetory-nav-active' : ''}}" data-index="{{index}}" wx:for="{{navList}}" wx:key="">{{item}}</text>
        </scroll-view>
    </view>

    <!-- 內容 -->
    <view class="cagetory-fixed-y">
        <scroll-view class="cagetory-scroll-y" scroll-y="true" scroll-into-view="NAV{{status}}" scroll-with-animation="true">
            <view wx:for="{{navList}}" wx:key="">
                <view id="NAV{{index}}" class="cagetory-list-head">{{item}}</view>
                <view class="cagetory-list-li">{{item}} 分類 {{index}}</view>
            </view>
        </scroll-view>
    </view>
</view>

js

Page({

    /**
     * 頁面的初始數據
     */
    data: {
        toView: '',
        navList: [
            "關注",
            "推薦",
            "時常",
            "正能量",
            "科技",
            "運動",
            "小視頻",
            "生活",
            "體育",
            "軍事",
        ],
    },
    getStatus(e) {
        console.log('獲取值', e);
        this.setData({
            // 獲取當前index
            status: e.currentTarget.dataset.index,
        })
    },
})

wxss

以下樣式可通過Webstorm編譯爲wxss

.scrollViewWrap {
    .cagetory-fixed-x {
        width: 100%;
        position: fixed;
        top: 0;
        left: 0;
    }

    .cagetory-scroll {
        height: 80rpx;
        line-height: 80rpx;
        width: 100%;
        white-space: nowrap;
        text {
            /* height: 120rpx;
            line-height: 80rpx;
            width: 100%;
            white-space: nowrap; */
        }
        .cagetory-nav-li {
            font-size: 33rpx;
            padding: 0 10rpx;
            &:first-of-type {
                padding-left: 16rpx;
            }
            &:last-of-type {
                padding-right: 16rpx;
            }
        }
         .cagetory-nav-active {
            color: red;
            border-bottom: 3rpx solid red;
        }
    }

    .cagetory-fixed-y {
        width: 100%;
        height: calc(100% - 200rpx);
        position: fixed;
        bottom: 0;
        left: 0;
        .cagetory-scroll-y {
            padding: 0 20rpx;
            height: 100%;
            box-sizing: border-box;
        }
        .cagetory-list-head {
            height: 50px;
            line-height: 50px;
            text-align: center;
            font-size: 30rpx;
            color: skyblue;
        }

        .cagetory-list-li {
            height: 400px;
            padding: 10rpx;
            color: #fff;
            font-size: 50rpx;
            background-color: skyblue;
        }
    } 
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章