Vue:如果满足特定条件,则应用CSS类

时间:2018-11-07 21:47:41

标签: javascript vue.js vuejs2

我正在使用vuex制作自定义图像滑块,我想对用于导航滑块的点应用特定的类,因此,如果该点处于活动状态,则将应用dotActive类。我想为此使用activeSlider变量

这是滑块组件:

<template>
<section class="slider_maincontainer">
    <transition-group name="slider-fade">
        <div class="slider_item" v-show="activeSlider===1" style="background-color:red;">
            <!--slider content-->
        </div>
        <div class="slider_item" v-show="activeSlider===2" style="background-color:blue;">
            <!--slider varied content-->
        </div>
        <div class="slider_item" v-show="activeSlider===3" style="background-color:green;">
            <!--another slider-->
        </div>
    </transition-group>
    <button class="slider_buttom_prev" @click="prevSlider()">
        <i class="slider_buttom_icon fas fa-angle-left"></i>
    </button>
    <button class="slider_buttom_next" @click="nextSlider()">
        <i class="slider_buttom_icon fas fa-angle-right"></i>
    </button>
    <div class="slider_dots_container">
    <!--how to apply custom class to this dot depending of activeIndex
        <span class="slider_dots_dot" v-for="slider in slidersCount" :key="slider" @click="goToSlider(slider)"></span>
    </div>
</section>
</template>
<!--SCRIPTS-->
<script>
import { mapState, mapActions } from 'vuex'
export default {
name: 'MainSlider',

computed:{
    ...mapState('MainSlider', ['activeSlider', 'slidersCount']),
},

mounted() {
    console.log(this.$options.name+' component successfully mounted');
    this.startSlider();
},

methods:{
    ...mapActions('MainSlider', ['nextSlider', 'prevSlider']),
}
};
</script>

还有我的Slider Store:

//STATE
const state = {
  slidersCount: 3,
  sliderInterval: 3000,
  activeSlider: 1,

}

//GETTERS
const getters = {

}

//ACTIONS
const actions = {


    prevSlider ({ commit, state }) {
        if(state.activeSlider == 1){
            commit( 'TO_LAST_SLIDER' );
        }
        else{
            commit( 'PREV_SLIDER' );
        }
    },


    nextSlider ({ commit, state }) {
        if(state.activeSlider == state.slidersCount){
            commit( 'TO_FIRST_SLIDER' );
        }
        else{
            commit( 'NEXT_SLIDER' );
        }
    },


    goToSlider ({ commit, sliderIndex }) {
        commit('GO_TO_SLIDER', sliderIndex)
        
    },



}

//MUTATIONS
const mutations = {


    PREV_SLIDER (state) {
        state.activeSlider--;
    },

    NEXT_SLIDER (state) {
        state.activeSlider++;
    },

    GO_TO_SLIDER (state, sliderIndex) {
        state.activeSlider = sliderIndex;
    },

    TO_FIRST_SLIDER (state) {
        state.activeSlider = 1;
    },

    TO_LAST_SLIDER (state) {
        state.activeSlider = state.slidersCount;
    },


}

export default {
    namespaced: true, state, getters, actions, mutations
  }

我知道,如果每个dom滑块与一个对象相关联并使用v-for,但afaik却无法使用原始dom元素做到这一点,那么我不会从后端或任何东西。

3 个答案:

答案 0 :(得分:1)

您可以使用此:

:class="{'activeDotClass':variableThatReturnsTrueOrFalse}"

基本上,您正在做的是在{}中使用javascript。记住用::绑定:class

有关类和样式绑定的更多信息: https://vuejs.org/v2/guide/class-and-style.html

答案 1 :(得分:0)

您可以如下使用类绑定:

<div v-bind:class="{ active: isActive }"></div>

其中“ isActive”是一个布尔变量,用于控制是否激活“ active”类。

此示例直接摘自在线@ www.vuejs.org上找到的Vue.js官方文档

答案 2 :(得分:0)

仅当dotActivespan的{​​{1}}匹配时,才可以使用class binding来激活特定activeSlider的{​​{1}}类索引:

span

slider
<span v-for="slider in sliderCount"
      :class="{ dotActive: slider === activeSlider }">
new Vue({
  el: '#app',
  data: () => ({
    activeSlider: 1,
    sliderCount: 3,
  }),
  methods: {
    slide(diff) {
      let slider = (this.activeSlider + diff) % (this.sliderCount + 1);

      // wrap around
      if (slider === 0) {
        slider = (diff > 0)
          ? 1
          : this.sliderCount;
      }

      this.activeSlider = slider;
    },
  }
})