1. 程式人生 > >「小程序JAVA實戰」小程序的組件(23)

「小程序JAVA實戰」小程序的組件(23)

原來 用例 mini ping Language pla int images ndt

轉自:https://idig8.com/2018/08/11/xiaochengxu-chuji-23/

開始了解下小程序的組件。源碼:https://github.com/limingios/wxProgram.git 中的No.10

組件

  • 多個組件構成一張視圖頁面
    >經過樣式和布局,頁面其實理解成html

  • 組件包含<開始標簽></結束標簽>

  • 每個組件都包含一些公用屬性

  • 官方的闡述

    https://developers.weixin.qq.com/miniprogram/dev/component/

技術分享圖片

view視圖組件

  • view 組件
    >用的最多的,也是之前的樣例也講過。https://developers.weixin.qq.com/miniprogram/dev/component/view.html

技術分享圖片

  • 演示用例
<!--view.wxml-->

<!--手機按住1秒變成灰色,手指松開後2秒變回原來的紅色-->
<view class="container" hover-class=‘hover-class‘ hover-start-time="10000" hover-stay-time="2000">
</view>


.container{
  background-color: red;
}

.hover-class{
  background-color: gray;
}

技術分享圖片

scroll-view 視圖組件

  • 官網的介紹
    >https://developers.weixin.qq.com/miniprogram/dev/component/scroll-view.html

技術分享圖片

技術分享圖片

  • 演示
<!--scroll-view.wxml-->
<scroll-view class="container-wrapp" style=‘height:300rpx‘ scroll-y=‘true‘
bindscrolltoupper="scrolltoupper"
bindscrolltolower="scrolltolower"
upper-threshold="0"
lower-threshold="0"
scroll-top="100"
enable-back-to-top="true"
scroll-with-animation="true"
bindscroll="scroll"
scroll-into-view="b"
>
  <view id="a" class=‘sizeY a‘>a</view>
  <view id="b" class=‘sizeY b‘>b</view>
  <view id="c" class=‘sizeY c‘>c</view>
  <view id="d" class=‘sizeY d‘>d</view>
  <view id="e" class=‘sizeY e‘>e</view>
</scroll-view>


<scroll-view class="container-nowrap" style=‘margin-top:250rpx‘ scroll-x=‘true‘ scroll-left="200">
  <view id="a" class=‘sizeX a‘>a</view>
  <view id="b" class=‘sizeX b‘>b</view>
  <view id="c" class=‘sizeX c‘>c</view>
  <view id="d" class=‘sizeX d‘>d</view>
  <view id="e" class=‘sizeX e‘>e</view>
</scroll-view>


//scroll-view.js
//獲取應用實例

Page({
  scrolltoupper:function(){
      console.log("滾動到頂部");
  },
  scrolltolower:function(){
      console.log("滾動到底部");
  },
  scroll:function(){
    console.log("滾動中。。。");
  }

})

.container-wrap{
  display: flex;
  flex-wrap:wrap;
}

.container-nowrap{
  display:flex;
  white-space: nowrap;
}

.sizeY{
  width: 100%;
  height: 150rpx;
}

.sizeX{
  width: 250rpx;
  height: 150px;
  display: inline-block;
}
.a {
  background: red;
}
.b {
  background: yellow;
}

.c {
  background: blue;
}

.d {
  background: green;
}

.e {
  background: gold;
}

技術分享圖片

註意:enable-back-to-top=”true” 在開發工具沒辦法演示只能在手機上才能演示出來點擊直接到達頂部的效果。關於scrollview 只有橫向和縱向,其實這塊還是比較重要的多加練習吧。

swiper組件

  • 俗稱 輪播圖
  • 官方介紹
    >https://developers.weixin.qq.com/miniprogram/dev/component/swiper.html

技術分享圖片

技術分享圖片

技術分享圖片

  • 演示
<!--swiper.wxml-->
<swiper indicator-dots="{{indicatorDots}}"
  autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}">
  <block wx:for="{{imgUrls}}">
    <swiper-item>
      <image src="{{item}}" class="slide-image" width="355" height="150"/>
    </swiper-item>
  </block>
</swiper>
<button bindtap="changeIndicatorDots"> indicator-dots </button>
<button bindtap="changeAutoplay"> autoplay </button>
<slider bindchange="intervalChange" show-value min="500" max="2000"/> interval
<slider bindchange="durationChange" show-value min="1000" max="10000"/> duration
//swiper.js
//獲取應用實例

Page({
  data: {
    imgUrls: [
      ‘http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg‘,
      ‘http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg‘,
      ‘http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg‘
    ],
    indicatorDots: false,
    autoplay: false,
    interval: 5000,
    duration: 1000
  },
  changeIndicatorDots: function (e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function (e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function (e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function (e) {
    this.setData({
      duration: e.detail.value
    })
  }
})

技術分享圖片

技術分享圖片

  • 演示
<!--movable.wxml-->

<movable-area class="container">
  <movable-view class=‘size‘ direction=‘all‘ inertia=‘true‘ out-of-bounds=‘true‘ x=‘50‘ y=‘50‘
  damping=‘100‘ friction=‘100‘ bindchange=‘onchange‘ bindscale=‘onscale‘ scale="true">
  </movable-view>
</movable-area>



.container{
  background-color: red;
  width: 100%;
  height: 650rpx;
}

.size{
  background-color: yellow;
  width: 300rpx;
  height: 250rpx;
}
//movable.js
//獲取應用實例

Page({
  onchange:function(){
    console.log("在移動。。");
  },
  onscale:function(){
    console.log("在縮放")
  }
})

技術分享圖片

PS:跟老鐵一起過了一遍wx小程序關於視圖的api,感覺還是組件很豐富,很好用!

「小程序JAVA實戰」小程序的組件(23)