【问题标题】:Vue.js v-bind:style Pseudo element :: after content iconVue.js v-bind:style 伪元素 :: 在内容图标之后
【发布时间】:2018-11-10 13:34:30
【问题描述】:

我有一个 Bootstrap Vue ProgressBar。我需要在带有内容图标(来自FontAwsome)之后添加到类“.progress-bar”伪元素::。我也希望它是动态的。因为我已经在我的 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;
                }
            }
        }
    }

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

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

data: {
  styleObject: {
    color: 'red',
    fontSize: '13px'
  }
}

【问题讨论】:

    标签: html css vue.js bootstrap-4


    【解决方案1】:

    假设你有一个父组件:

    <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>
    

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

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

    父组件.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>
    

    【讨论】:

    • 这对我有用。如果您想使用具有不同样式道具的组件,它会保留该组件的样式。
    【解决方案2】:

    使用css var()

    然后:style="{ '--varName': xxx}"

    【讨论】:

    【解决方案3】:

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

    如果是,请查看下面的demo,它使用一个span模拟图标,然后绑定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)
        }
      }
    })
    .badge {
      background-color:green;
      border: 1px solid black;
      padding: 2px;
      transition: 1s;
    }
    <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>

    【讨论】:

    • 虽然是一个很好的答案,但这并不是 OP 关于如何设置伪元素样式的问题。所有这一切都是添加一个额外的元素并对其应用内联样式。
    猜你喜欢
    • 1970-01-01
    • 2018-03-18
    • 2013-09-26
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2013-11-07
    相关资源
    最近更新 更多