真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯網站制作重慶分公司

js評分組件怎么用

這篇文章給大家分享的是有關js評分組件怎么用的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

創(chuàng)新互聯主營祁縣網站建設的網絡公司,主營網站建設方案,重慶APP軟件開發(fā),祁縣h5微信小程序搭建,祁縣網站營銷推廣歡迎祁縣等地區(qū)企業(yè)咨詢

1.html部分


    

解釋

1.在大的div里綁定starType是因為在整個App中,有多個評分組件,而它們的大小不一樣,所以根據大小動態(tài)的綁定class.

同樣的原理,在上一節(jié)header組件開發(fā)中也有介紹,但直到寫到這里我開始漸漸明白vue.js中:class的意義。以前我想既然可以直接添加class,為什么要用綁定class來多此一舉。現在我明白的,基礎的樣式設定,直接添加class就可以了,但是有時候涉及到根據不同的狀態(tài)有不同的樣式時,就要用綁定class了。

2.v-for 這里我們沒有寫5個span,而是遍歷itemClasses,這是vue.js中的一種常用方法。既減少了代碼,而且動態(tài)獲取數據。

2.js部分

1. 得到評分數據

像上一節(jié)一樣,我們通過props來接收數據。我們要接收的是兩個number類型的數據,一個是星星的尺寸,一個是分數。

props: {
      size:{
        type:Number
      },
      score:{
        type:Number
      }
    }

2.屬性的計算

1).接收size動態(tài)綁定不同的class

starType() {
        return 'star-'+this.size;
      }
  .star-48 {
    width: 20px;
    height: 20px;
    margin-right: 22px;
    background-size: 20px 20px;
    
  }
  .star-36 {
    width: 15px;
    height: 15px;
    margin-right: 6px;
    background-size: 15px 15px;
  }
  .star-24 {
    width: 10px;
    height: 10px;
    margin-right: 3px;
    background-size: 10px 10px;
  }

2). 通過計算確定添加全星半星和無星

const LENGTH = 5;
const CLS_ON = 'on';
const CLS_HALF = 'half';
const CLS_OFF = 'off';
itemClasses() {
        let result = [];
        let score = Math.floor(this.score*2)/2;
        let hasDecimal = score%1 !== 0;
        let integer = Math.floor(score);
        for (var i = 0; i < integer; i++) {
          result.push(CLS_ON);
        }
        if(hasDecimal) {
          result.push(CLS_HALF);
        }
        while (result.length

這段代碼的思路是:創(chuàng)建一個數組儲存星星,判斷分數是否在.5以上,將分數取整,有多少分push幾個on星星進去,有.5以上,push一個half,然后push進off直到長度達到5。

3).css部分
以star-48的尺寸為例

.star-48 .on {
    background-image: url('star48_on@2x.png')
  }
  .star-48 .half {
    background-image: url('star48_half@2x.png')
  }
  .star-48 .off {
    background-image: url('star48_off@2x.png')
  }

4.完整代碼




 const LENGTH = 5;
 const CLS_ON = 'on';
 const CLS_HALF = 'half';
 const CLS_OFF = 'off';
 export default {
  props: {
   size:{
    type:Number
   },
   score:{
    type:Number
   }
  },
  computed:{
   starType() {
    return 'star-'+this.size;
   },
   itemClasses() {
    let result = [];
    let score = Math.floor(this.score*2)/2;
    let hasDecimal = score%1 !== 0;
    let integer = Math.floor(score);
    for (var i = 0; i < integer; i++) {
     result.push(CLS_ON);
    }
    if(hasDecimal) {
     result.push(CLS_HALF);
    }
    while (result.length

 .star {
  font-size: 0;
 }
 /* .star-48 {
  width: 20px;
  height: 20px;
  margin-right: 22px;
  background-size: 20px 20px;
  
 } */
 .star-48 : last-chlid {
  margin-right: 0;
 }
 .star-48 .on {
  background-image: url('star48_on@2x.png')
 }
 .star-48 .half {
  background-image: url('star48_half@2x.png')
 }
 .star-48 .off {
  background-image: url('star48_off@2x.png')
 }
 .star-36 {
  width: 15px;
  height: 15px;
  margin-right: 6px;
  background-size: 15px 15px;
 }
 .star-36 .no {
  background-image: url('star36_on@2x.png')
 }
 .star-36 .half {
  background-image: url('star36_half@2x.png')
 }
 .star-36 .off {
  background-image: url('star36_off@2x.png')
 }
 .star-24 {
  width: 10px;
  height: 10px;
  margin-right: 3px;
  background-size: 10px 10px;
 }
 .star-24 .no {
  background-image: url('star24_on@2x.png')
 }
 .star-24 .half {
  background-image: url('star24_half@2x.png')
 }
 .star-24 .off {
  background-image: url('star24_off@2x.png')
 }
 .star-item {
  display: inline-block;
  background-repeat: no-repeat;
  width: 20px;
  height: 20px;
  margin-right: 22px;
  background-size: 20px 20px;

 }

感謝各位的閱讀!關于“js評分組件怎么用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!


當前題目:js評分組件怎么用
分享URL:http://weahome.cn/article/pcjpic.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部