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

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

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

前言

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

創(chuàng)建模版

wxml文件:

以<template>為根節(jié)點(diǎn),添加name屬性用來(lái)區(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" /> <!--這里引用模板文件最好使用相對(duì)路徑-->
<template is="starsTemplate" data="{{stars:stars,score:average}}" /> <!--is指定使用模版的名稱-->

引用wxss模版:

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

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

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

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

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é)束了,我們這評(píng)分轉(zhuǎn)換成我們需要的評(píng)分?jǐn)?shù)組,在使用模版的地方傳入數(shù)據(jù)即可。

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

相關(guān)文章

最新評(píng)論

微山县| 鄢陵县| 新泰市| 云安县| 凤阳县| 中卫市| 兴文县| 达拉特旗| 丰原市| 丹巴县| 阿拉善右旗| 太康县| 容城县| 德化县| 巩留县| 旅游| 新建县| 文昌市| 河源市| 额敏县| 罗城| 新巴尔虎左旗| 临朐县| 隆尧县| 灌南县| 南皮县| 塔河县| 长治县| 泾川县| 拉萨市| 鹿邑县| 长宁区| 玉龙| 沅陵县| 东阿县| 乐清市| 吴堡县| 施甸县| 隆昌县| 平原县| 德令哈市|