Vue.js v-bind:style 伪元素::内容图标之后

Iva*_*ich 3 html css vue.js bootstrap-4

我有一个 Bootstrap Vue ProgressBar。我需要添加到类“.progress-bar”伪元素 :: after with content icon(来自 FontAwsome)。我也希望它是动态的。因为我已经在我的 ProgressBar(从 0 tp 100 开始)中实现了步骤,我想要,当我点击时,这个图标将与 ProgressBar 行一起。

<b-progress v-bind:style="styleProgressBar" :value="counter" :max="max"></b-progress>

 export default {
        components:{
            'navbar':navbar
        },
        name: "myPage",
        data() {
            return {
                counter: 0,
                max: 100,
                step:1,
            }
        },
        methods:{
            prev() {
                this.step--;
            },
            next() {
                this.step++;
                if (this.counter < 100) {
                    this.counter += 34;
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我也看到了这个:https : //vuejs.org/v2/guide/class-and-style.html

<div v-bind:style="styleObject"></div>

data: {
  styleObject: {
    color: 'red',
    fontSize: '13px'
  }
}
Run Code Online (Sandbox Code Playgroud)

Jad*_*ian 8

使用css var()

进而:style="{ '--varName': xxx}"


nik*_*ong 6

假设您有一个父组件:

<div id="parent">
  <ChildComponent id="child"> // Possibly from another library?
</div>

// renders ->

<div id="parent">
   <div id="child">
     <div id="child-component-item">
        ::after
     </div>
   </div>
</div>
Run Code Online (Sandbox Code Playgroud)

挑战在于为#child-component-item:after选择器创建绑定。

我们可以使用 css vars 来解决这个问题,通过使用一些 CSS 来“进入”子组件。请注意,::v-deep如果您的样式是scoped.

父组件.js

<div id="parent-component" :style="{'--bgColor': bgColor}">
   <ChildComponent>
</div>

<script>
  export default {
    data() {
      return {
        bgColor: 'red'
      }
    }
  }
</script>

<style>
   #child-component-item:after {
      background-color: var(--bgColor)
   }
</style>
Run Code Online (Sandbox Code Playgroud)


Sph*_*inx -1

您似乎想在进度栏后面添加一个图标。

如果是这样,请查看下面的演示,它使用一个跨度模拟图标,然后绑定left移动图标。

Vue.config.productionTip = false
app = new Vue({
  el: "#app",
  data: {
    counter: 0,
    max: 100,
    intervalID: null
  },
  methods: {
    runTask: function () {      
      clearInterval(this.intervalID)
      this.counter = 0
      this.intervalID = setInterval(() => {
        this.counter = (this.counter+7)%this.max
      }, 1000)
    }
  }
})
Run Code Online (Sandbox Code Playgroud)
.badge {
  background-color:green;
  border: 1px solid black;
  padding: 2px;
  transition: 1s;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/vue@2.5.16/dist/vue.js"></script>
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css"/>
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css"/>

<script src="//unpkg.com/babel-polyfill@latest/dist/polyfill.min.js"></script>
<script src="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"></script>
<div id="app">
  <button @click="runTask()">Run</button>
  <b-progress class="mt-1" :max="max" show-value>
     <b-progress-bar :value="counter" variant="success">
        <span class="badge" style="position:absolute;" :style="{'left':counter*100/max + '%'}" v-show="counter > 0">x</span>
     </b-progress-bar>
  </b-progress>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 虽然这是一个很好的答案,但这并不是OP关于如何设置伪元素样式的问题。这一切所做的只是添加一个额外的元素并对其应用内联样式。 (2认同)