微信小程式 scroll-view滾動到索引位置
阿新 • • 發佈:2019-01-07
有一些重要的關鍵點請看上一篇文章
主要用到scroll-into-view 如果你想讓他回頂部 直接用scroll-top即可
話不多說,看程式碼
wxml
- <scroll-viewscroll-y="true"scroll-into-view="{{toView}}">
- <viewclass="brand"wx:for="{{brandList}}">
- <viewclass="line"></view>
- <viewid="{{item.wordindex}}"class="wordindex">{{item.wordindex}}
- <viewclass="line"></view>
- <viewclass="brand_block">
- <viewclass="color_view"wx:for="{{item.brand}}"bindtap="click">
- <imagesrc="{{item.brandimg}}"></image>
- </view>
- </view>
- </view>
- </scroll-view>
- <viewclass="index"
- <textwx:for="{{wordindex}}"bindtap="choiceWordindex"data-wordindex="{{item.wordindex}}">
- {{item.wordindex}}
- </text>
- </view>
wxjs[javascript] view plain copy
- // pages/order/car/add_car/car_brand/car_brand.js
- var app = getApp()
- Page({
- data: {
- "brandList": [],
- "wordindex"
- "toView": '#',
- },
- onLoad: function (options) {
- var that = this;
- // 頁面初始化 options為頁面跳轉所帶來的引數
- app.func.req('getCarBrand?cx=1', function (res) {
- if (res.data.result == 'false') {
- console.log('false');
- that.wetoast.toast({
- title: res.data.msg,
- duration: 2000
- })
- } else {
- that.setData({
- brandList: res.data.brandList,
- wordindex: res.data.brandList,
- });
- var cData = that.data.brandList;
- cData[0].wordindex = "#";//先修改json值
- that.setData({ //再set值
- wordindex: cData
- })
- }
- }, function (res) {
- });
- },
- onReady: function () {
- // 頁面渲染完成
- },
- onShow: function () {
- // 頁面顯示
- },
- onHide: function () {
- // 頁面隱藏
- },
- onUnload: function () {
- // 頁面關閉
- },
- click: function () {
- wx.navigateTo({
- url: '/pages/order/car/add_car/car_model/car_model',
- })
- },
- choiceWordindex: function (event) {
- let wordindex = event.currentTarget.dataset.wordindex;
- if (wordindex == '#') {
- this.setData({
- toView: '常用品牌',
- })
- } else {
- this.setData({
- toView: wordindex,
- })
- }
- console.log(this.data.toView);
- }
- })
wcss:
- /* pages/order/car/add_car/car_brand/car_brand.wxss */
- scroll-view {
- height: 600px;
- float: left;
- width: 94%;
- }
- .wordindex {
- line-height: 50rpx;
- background: #f7f7f7;
- font-size: 14px;
- color: #929292;
- padding-left: 28rpx;
- }
- .color_view {
- text-align: center;
- display: inline-flex;
- flex-direction: column;
- }
- .brand_block {
- margin-top: 28rpx;
- margin-left: 28rpx;
- }
- image {
- width: 140rpx;
- height: 140rpx;
- margin-right: 24rpx;
- margin-bottom: 24rpx;
- border-style: solid;
- border-width: 1rpx;
- border-color: #ededed;
- }
- .index {
- float: right;
- position: fixed;
- right: 0;
- margin-top: 20%;
- margin-right: 10rpx;
- color: #e0004a;
- font-size: 11px;
- }
- .index text {
- display: flex;
- line-height: 18rpx;
- }