美文网首页
Vue 里style、class 内联三元表达式和img里src

Vue 里style、class 内联三元表达式和img里src

作者: 桐生一猫 | 来源:发表于2019-11-26 17:46 被阅读0次

    首先是style和class使用三元表达式处理:

    通常给class绑定个对象,就可以动态的切换样式了。(data中定义isActive的true、false)

    <div :class="{ active: isActive }">hello</div>
    handleClick(){ this.isActive = !this.isActive }
    

    推荐、不加{}就算是加字符串,最好绑定class用[]、{判断},style用{}

    <i class="iconfont "  :class="[current=='0'?'class1':'class2']"></i> 
    

    v-bind:style="{样式名:'样式值'}" 必须是字符串形式:

    <span v-bind:style="{display:isActive ? 'block':'none'}">hello</span>
    <div :style="{width:width,height:height}"></div>
    

    数组形式:

    <div :class='["classify",current=="0" ? "active" : ""]'  @click='current=0'>xx</div>
    <div :class='["classify", isActive? "active" : ""]'>xx</div>
    注意:数组中的classify如果不加引号的话,代表的是data中的一项,并不是类名,将classify加上双引号,变成字符串就可以变成类名
    

    字符串拼接:

    <div :class="'classify'+(current=='0'?' active':'')"  @click='current=0'>xx</div>
    注意:active前要加一个空格(必须有),字符串拼接时,两个字符串之间要有空格
    <button class="tk" v-show="(active==0 || active==1) || active==2">退款</button>
    

    处理img动态scr使用三元运算符是不行的,其实vue是将图片转换成base64的,所有我们最好的方式是用v-if:

     <img v-if="aimsImageurl"src=".~assets/img/tvscreen/arrow04.png'' />
     <img v-else src="~assets/img/tvscreen/arrow03.png" />
    

    谢谢,希望对你有所帮助

    相关文章

      网友评论

          本文标题:Vue 里style、class 内联三元表达式和img里src

          本文链接:https://www.haomeiwen.com/subject/oljywctx.html