前端 Vue框架学习实例(四)

1.使用v-bind绑定style

<h2 :style="{css属性名:值}">{{message}}</h2>

<body>
    <div id="app">
        <!-- <h2 :style="{css属性名:值}">{{message}}</h2> -->
        <!-- <h2 :style="{fontSize:size}">{{message}}</h2> -->
        <h2 :style="{fontSize:size + 'px',color:fineColor}">{{message}}</h2>
    </div>
    <script>
        const app = new Vue({
            el: "#app",
            data: {
                message: "你好",
                size: 500,
                fineColor: 'red'
            }
        })
    </script>
</body>

2.v-bind和v-for联合使用

  • 实现一个列表,点击相应list改变颜色

思路:

  1. 使用v-for将数据循环出列表 。v-for="(items,index) in message"  。items,index有用
  2. 绑定样式。:class="{active:currentIndex === index}",默认currentIndex: 0 ,即index=0默认选中颜色。
  3. 点击list改变颜色。@click="colorred(index)"  点击事件绑定一个函数                                         colorred: function(index) {

                        this.currentIndex = index

                    }

css样式

        .active {

            color: aqua;

        }

<body>
    <div id="app">
        <ul>
            <li v-for="(items,index) in message" :class="{active:currentIndex === index}" @click="colorred(index)">
                {{items}}
            </li>
        </ul>
    </div>
    <script>
        const app = new Vue({
            el: "#app",
            data: {
                message: ['海贼王', '火影忍者', '死神', '进击的巨人'],
                currentIndex: 0 //记录由谁来显示颜色
            },
            methods: {
                colorred: function(index) {
                    this.currentIndex = index
                }
            }
        })
    </script>
</body>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值