最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

微信小程序使用template標(biāo)簽實現(xiàn)五星評分功能

 更新時間:2018年11月03日 11:10:21   作者:T丶快樂知己丬  
這篇文章主要為大家詳細(xì)介紹了微信小程序使用template標(biāo)簽實現(xiàn)五星評分功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

前言

微信小程序特點“無須安裝,無須卸載,觸手可及,用完即走”,適合輕量級應(yīng)用快速開發(fā)。小程序模版思想是它開發(fā)的核心思想,可以幫助開發(fā)者減少很多重復(fù)代碼,我們來看看怎么在小程序中實現(xiàn)市面上常用的評分組件, 看看效果圖:

創(chuàng)建模版

wxml文件:

以<template>為根節(jié)點,添加name屬性用來區(qū)分不同模版:

<template name="starsTemplate">
 <view class='stars-container'>
  <view class='stars'>
  <block wx:for="{{stars}}" wx:for-item="i" wx:key="position">
   <image wx:if="{{i}}" src='/images/icon/star.png'></image>
    <!-- <image wx:elif="{{i==2}}" src='/images/icon/none-star.png'></image> -->
    <image wx:else src='/images/icon/none-star.png'></image>
  </block>
  </view>
  <text class='star-score'>{{score}}</text>
 </view>
</template>

wxss文件:

這里我們采用小程序推薦使用flex模型,橫向排列五顆星星圖片。

.stars-container {
 display: flex;
 flex-direction: row;
}
.stars {
 display: flex;
 flex-direction: row;
 height: 17rpx;
 margin-right: 24rpx;
 margin-top: 6rpx;
}
.stars image {
 padding-left: 3rpx;
 height: 17rpx;
 width: 17rpx;
}
.star-score {
 color: #1f3463;
}

模版使用

引用wxml模版:

<import src="../stars/stars-template.wxml" /> <!--這里引用模板文件最好使用相對路徑-->
<template is="starsTemplate" data="{{stars:stars,score:average}}" /> <!--is指定使用模版的名稱-->

引用wxss模版:

在使用模版文件對應(yīng)的wxss文件中輸入以下表達式即可

@import "../stars/stars-template.wxss";

數(shù)據(jù)裝換:

我看到在使用模版wxml文件時有個data=“{{stars:stars,score:average}}”屬性和數(shù)據(jù),表示指定模版的數(shù)據(jù),我們一般從后臺獲取的評分?jǐn)?shù)據(jù)一般都是一個數(shù)字,比如:3.5,4,5等等,我們需要把這些數(shù)字轉(zhuǎn)換成五個等級數(shù)組,決定評分星星顯示什么圖片,這里我們用1表示顯示高亮的,0表示灰色星星,我們也可以在一個2表示半顆高亮的星星表示0.5的評分:

function convertToStarsArray(stars) {
 var num = stars.toString().substring(0, 1);
 var array = [];
 for (var i = 1; i <= 5; i++) {
  if (i < num) {
   array.push(1);
  } else {
   array.push(0);
  }
 }
 return array;
}

案例wxml代碼:

<import src="stars/stars-template.wxml" />
<view style='margin:100rpx'>
 <template is="starsTemplate" data="{{stars:stars,score:average}}" />
</view>

到這里基本結(jié)束了,我們這評分轉(zhuǎn)換成我們需要的評分?jǐn)?shù)組,在使用模版的地方傳入數(shù)據(jù)即可。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

海晏县| 巴里| 白水县| 平果县| 罗平县| 博客| 大庆市| 沁阳市| 弥勒县| 城步| 民县| 双辽市| 大荔县| 洱源县| 凤庆县| 额尔古纳市| 平邑县| 右玉县| 新郑市| 老河口市| 益阳市| 嵊泗县| 普宁市| 衡阳市| 贵溪市| 榆社县| 汉寿县| 托克托县| 施秉县| 泰顺县| 唐海县| 怀安县| 贵溪市| 紫阳县| 洞头县| 册亨县| 聂荣县| 交口县| 达拉特旗| 谢通门县| 太康县|