04-属性

1、tab切换

 <style>
        .mainBox {
            width: 300px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .onlyBox {
            flex: 1;
            height: 100%;
            border: 1px solid #777;

        }

        .contentBox {
            width: 300px;
            height: 600px;
            background-color: #ececec;
            text-align: center;
            line-height: 600px;
            font-size: 23px;
            color: #ff0000;
        }

        /* 被选中时加载该样式 */
        .selectSty {
            background-color: chocolate;
            color: #fff;
            font-weight: bold;
        }
    </style>
<div id="app">

    <div class="mainBox">
    <!-- :class = "{className1:(条件1),className2:(条件2)}" -->
    <div class="onlyBox" :class={selectSty:index===0} 
    @click="setIndex(0)">项目1</div>
    <div class="onlyBox" :class={selectSty:index===1} 
    @click="setIndex(1)">项目2</div>
    <div class="onlyBox" :class={selectSty:index===2} 
    @click="setIndex(2)">项目3</div>
    </div>

     <div class="contentBox" v-if="index === 0">内容1</div>
     <div class="contentBox" v-if="index === 1">内容2</div>
     <div class="contentBox" v-if="index === 2">内容3</div>
</div>
<script>
    var vm = new Vue({
    el: '#app',
    data: {
    index: 0 //通过index 判断 显示哪一项(index0 显示 内容1 index1 显示内容2 )
  },
    methods: {
    setIndex(ind) {
    console.log(ind);
    this.index = ind;
    }
  }
})
</script>

上述运行结果:

 2、v-bind

<div id="app"> 
    <!-- 动态绑定class -->
    <div v-bind:class="{colorSty:isShow}">动态绑定class</div>
    <!-- 通过三元运算符 动态绑定class  -->
    <div :class="[isShow?'colorSty':'colorElse']">我是class 的三元运算符</div>
    <!-- 在不同条件下 执行不同的class -->
    <div class="sizeSty" 
    :class="{colorSty: index ===0,colorElse:index === 1,
    colorDs: index === 2}">在不同条件下 执行不同的
</div>

    <input type="number" v-model.number="index">
        <!-- : === v-bind   简写的方式 -->
        <div :style="{fontSize: size+'px'}">我是动态绑定Style</div>
        <!-- 通过三元运算符进行动态绑定 -->
        <div :style="{color:(isShow?'#0099ff':'#ff0000')}">我是三元运算符</div>

        <button @click="setSize">改变字体大小</button>
        <button @click="setSrc">改变isShow</button>
    </div>
<script>
    var vm = new Vue({
    el: "#app",
    data: {
    isShow: true,
    size: 30,
    index: 0
    },
    methods: {
        setSrc() {
        this.isShow = !this.isShow
    },
        setSize() {
        // this.size = 20;
        // if (this.size === 20) {
        //     this.size = 30
        // } else {
        //     this.size = 20
        // }
        //三元运算的方法
        this.size = this.size === 30 ? 20 : 30
        },
     }
 })
</script>

上述运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七色的天空

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值