【问题标题】:VueJS to dynamically control the CSSVueJS 动态控制 CSS
【发布时间】:2018-03-30 06:14:39
【问题描述】:

我的用例是这样的,

  1. 我从后端获得了星级作为数字(1,2,3,4,5)。
  2. 如果它是 3 号,我想将前 3 颗星涂成黄色,然后将其扩孔为默认颜色。 如何使用 Vue JS 实现这一目标?

这是我的代码。

<template>
  <div id="app">
    <span class="demo">&#9733;&#9733;&#9733;&#9733;&#9733;</span>
  </div>
</template>

<script>

export default {
  name: 'app',
  data () {
    return {

    }
  },
  methods:{

  }
}
</script>

<style>
.demo{
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  font-size: 6em;
  color:yellow;
  text-shadow: 0 0 15px blue;
}
</style>

【问题讨论】:

    标签: css vue.js vuejs2


    【解决方案1】:

    您可以使用v-for 和一些 CSS 类,如下所示。

    他们将迭代starRank 次创建filled-star 星,然后迭代5 - starRank 次创建not-filled-star 星。

    在下面的示例中,starRank3,因此,三颗星将是黄色的。

    new Vue({
      el: '#app',
      data() {
        return {
          starRank: 3
        }
      }
    })
    .demo {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      font-size: 6em;
      text-shadow: 0 0 15px blue;
    }
    
    .filled-star { color: yellow; }
    .not-filled-star { color: blue; }
    <script src="https://unpkg.com/vue"></script>
    
    <div id="app">
      <span class="demo">
          <span v-for="index in starRank" class="filled-star">&#9733;</span><span v-for="index in (5-starRank)" class="not-filled-star">&#9733;</span>
      </span>
    </div>

    【讨论】:

    • 不错!我盯着index in (5-starRank) 看,想不出它是如何工作的:D 似乎在醒来后看 StackOverflow 不是一个好主意 :)
    • @sarneeh 哈哈,看那个,我真的要睡觉了!但是我晚上确实好多了,我真的只在午餐后开机:P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 2019-07-26
    • 2020-06-17
    • 2017-06-07
    • 2017-04-27
    • 2015-09-16
    相关资源
    最近更新 更多