首頁  >  文章  >  微信小程式  >  微信小程式實作跟隨選單效果和循環嵌套載入資料實例詳解

微信小程式實作跟隨選單效果和循環嵌套載入資料實例詳解

小云云
小云云原創
2018-01-16 09:50:202784瀏覽

本文主要為大家詳細介紹了微信小程式實現跟隨菜單效果和循環嵌套加載數據,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能幫助到大家。

效果如圖:

程式碼如下:

wxml





//使用循环嵌套data数据格式写对即可
<scroll-view class="left" scroll-y>  
  <view wx:for="{{left}}" class="leftlist {{index==_click?&#39;yes&#39;:&#39;&#39;}}" data-i="{{index}}" bindtap="tap">
   {{item.txt}}
  </view>
 </scroll-view>

<scroll-view class="right" scroll-y bindscroll="scroll" scroll-into-view="{{toView}}">
  <view id="{{item.id}}" wx:for="{{right}}">

   <view class="title">
    <text class="line"></text>
    {{item.txt}}
    <text class="line"></text>
   </view> 

   <view class="li" wx:for="{{item.li}}">
    <image src="{{item.src}}"></image>
    <text class="name">{{item.name}}</text>
   </view>

  </view>
 </scroll-view>

js



Page({
 data: {
  toView: &#39;red1&#39;,
  _click:0,
  left: [{ txt: &#39;新品&#39;, id: &#39;new&#39; }, { txt: &#39;手机&#39;, id: &#39;phone&#39; }, { txt: &#39;电视&#39;, id: &#39;mv&#39; }, { txt: &#39;电脑&#39;, id: &#39;computer&#39; }],
  right: [
   { txt: &#39;新品&#39;, id: &#39;new&#39;,li: [{ src: &#39;../../assets/images/max1.jpg&#39;, name: &#39;小米noto&#39; }, { src: &#39;../../assets/images/max1.jpg&#39;, name: &#39;小米mix&#39; }, { src: &#39;../../assets/images/max3.jpg&#39;, name: &#39;小米5c&#39; }, { src: &#39;../../assets/images/max2.jpg&#39;, name: &#39;小米notp&#39; }, { src: &#39;../../assets/images/max2.jpg&#39;, name: &#39;小米note5&#39; }, { src: &#39;../../assets/images/max2.jpg&#39;, name: &#39;小米6&#39; }]}, 
   { txt: &#39;手机&#39;, id: &#39;phone&#39;,li: [{ src: &#39;../../assets/images/max2.jpg&#39;, name: &#39;小米6s&#39; }, { src: &#39;../../assets/images/max3.jpg&#39;, name: &#39;小米max&#39; }, { src: &#39;../../assets/images/max2.jpg&#39;, name: &#39;小米5s&#39; }, { src: &#39;../../assets/images/max1.jpg&#39;, name: &#39;小米li&#39; }, { src: &#39;../../assets/images/max3.jpg&#39;, name: &#39;小米4&#39; }, { src: &#39;../../assets/images/max1.jpg&#39;, name: &#39;小米max&#39; }]}, 
   { txt: &#39;电视&#39;, id: &#39;mv&#39;, li: [{ src: &#39;../../assets/images/max3.jpg&#39;, name: &#39;小米6&#39; }, { src: &#39;../../assets/images/max2.jpg&#39;, name: &#39;小米mix&#39; }, { src: &#39;../../assets/images/max1.jpg&#39;, name: &#39;小米7s&#39; }, { src: &#39;../../assets/images/max3.jpg&#39;, name: &#39;小米2&#39; }, { src: &#39;../../assets/images/max1.jpg&#39;, name: &#39;小米note7&#39; }, { src: &#39;../../assets/images/max3.jpg&#39;, name: &#39;小米8&#39; }] }, 
   { txt: &#39;电脑&#39;, id: &#39;computer&#39;, li: [{ src: &#39;../../assets/images/max1.jpg&#39;, name: &#39;小米2&#39; }, { src: &#39;../../assets/images/max1.jpg&#39;, name: &#39;小米mix&#39; }, { src: &#39;../../assets/images/max2.jpg&#39;, name: &#39;小米max&#39; }, { src: &#39;../../assets/images/max1.jpg&#39;, name: &#39;小米6&#39; }, { src: &#39;../../assets/images/max3.jpg&#39;, name: &#39;小米note&#39; }, { src: &#39;../../assets/images/max1.jpg&#39;, name: &#39;小米max&#39; }] }]
 },

 scroll: function (e) {
  console.log(e)//右侧列表滑动-左侧列表名称样式跟着改变,然而我不会写,搁置中,谁会告诉我,谢谢!
 },
 tap: function (e) { 
  var j = parseInt(e.currentTarget.dataset.i);
  this.setData({
   toView: this.data.left[j].id,//控制视图滚动到为此id的<view>
   _click:j           //控制左侧点击后样式
    })
 },
})

wxss


page{border-top:1px solid #f6f6f6;}

.left{
 height:100%;
 width: 19%;
 display: inline-block;
 background:#fff;
 text-align:center;
 border-right:1px solid #eee;
 }
.leftlist{
 font-size:12px;
 padding:10px;
 }

.right{
 height:100%;
 width: 80%;
 display: inline-block;
 background:#fff;
 text-align:center;
 }

.line{
 width:15px;
 height:1px;
 background:#ddd;
 display:inline-block;
 vertical-align:super;
 margin:0 15px;
}

.li{ 
 height:10%;
 width:30%;
 display:inline-block;
 text-align:center;
}

.li image{width:60px;height:60px;}

.li .name{
 font-size:12px;
 display:block;
 color:#888;
 overflow:hidden;
 white-space:nowrap;
 text-overflow:ellipsis;
}

.title{padding:20px 0;}
.yes{color: #f99;font-size: 14px;}

友善提示:

1、左側點擊樣式改變:

利用自身index與點擊的元素的index比較。

data-i=“{{取得目前index傳給_click儲存}}”,

class=”leftlist {{index==_click?'yes':”}}”, 此處index是自身的,如果自身和點擊的一致就添加,yes類名,否側滯空清除yes樣式。利用scroll-into-view=”{{id}}”,視圖會捲動到id為此id的view標籤。 #3、循環嵌套:data資料格式寫對,依照官方文件就行。

實例詳解ajax實作載入資料功能

微信小程式頁面滑動畫面載入資料的實例詳解######

以上是微信小程式實作跟隨選單效果和循環嵌套載入資料實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn