vue属性指令

【 属性指令 】

# 控制属性的
v-bind:属性名='值'
# 可以简写成
:属性='值'
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://unpkg.com/vue@2.6.14/dist/vue.js"></script>
    <style>
        img{
            height: 300px;
            width: 200px;
        }
    </style>
</head>
<body>
<div class = one>
    <p>属性指令</p>
    <!-- v-bind:属性名='变量' -->
    <!-- :属性名='变量' -->
    <img :src="image">

</div>

<script>
    var vm = new Vue({
        el: '.one',
        data:{
            image:'img/14.jpg'
        }
    })
</script>
</body>
</html>
  • 切换图片跟轮播图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://unpkg.com/vue@2.6.14/dist/vue.js"></script>
    <style>
        img {
            height: 300px;
            width: 200px;
        }
    </style>
</head>
<body>
<div class=one>
    <p>属性指令</p>
    <!-- v-bind:属性名='变量' -->
    <!-- :属性名='变量' -->
    <img :src="image">
    <br>
    <button @click="clickone">点击切换</button>
    <button @click="clicktwo">选秀开始</button>

</div>

<script>
    var vm = new Vue({
        el: '.one',
        data: {
            image: 'img/14.jpg',
            images: ['img/14.jpg', 'img/16.jpg'],
            timer: null,
            index: 0
        },
        methods: {
            clickone() {
                //切换到下一张图片
                this.index = (this.index + 1) % this.images.length
                this.image = this.images[this.index];

            },
            clicktwo() {
                if (this.timer != null) {
                    //停止定时器
                    clearInterval(this.timer)
                    this.timer = null
                } else {
                    //启动定时器
                    //使用定时器
                    var image_list = [
                        'img/20.jpg',
                        'img/21.jpg',
                        'img/22.jpg',
                        'img/23.jpg',
                        'img/24.jpg',
                        'img/15.jpg',
                    ]
                    var _this = this
                    var num = 0
                    this.timer = setInterval(function () {
                        _this.image = image_list[num]
                        num += 1
                        // 判断是否到达最后一张图片
                        if (num >= image_list.length) {
                            num = 0; // 重置为第一张图片
                        }
                    }, 1000)
                }
            }
        }
    })

</script>
</body>
</html>

style和class

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://unpkg.com/vue@2.6.14/dist/vue.js"></script>
    <style>
        img{
            height: 300px;
            width: 300px;
        }
        .red {
            background-color: mediumvioletred;
        }
        .green{
            background-color: green;
        }
    </style>
</head>
<body>

<div class="one">
    <img :src="image" alt="" v-show="ishow">
    <p @click="hand" style="color: aqua">点我点我</p>
    <br>
    <br>
    <button @click="backyun">点我切换背景色!!</button>
    <hr>
    <div :class="backing">
        点我就对了
    </div>
</div>

<script>


    var vm = new Vue ({
        el: ".one",
        data: {
            image: 'img/14.jpg',
            ishow:false,
            backing: "blue"
        },
        methods: {
            hand() {
                this.ishow = !this.ishow;
            },
            backyun(){
                this.backing = this.backing == "blue" ? "green" : "red";
            }
        }
    })
</script>

</body>
</html>

image-20240425212150834

​ style和class是两个常用的属性,用于动态设置元素的样式和类。以下是它们的使用方法及注意事项总结:

1. Style属性的方法:

  • 字符串形式: 直接将样式字符串赋给:style,例如::style="'background-color: aqua; height: 250px; width: 250px; font-size: 60px;'"
  • 数组形式: 将包含多个样式对象的数组赋给:style,例如::style="[{ 'background-color': 'green' }, { height: '300px' }, { width: '300px' }]"
  • 对象形式: 将样式对象直接赋给:style,例如::style="{ backgroundColor: 'red', height: '360px', width: '300px' }"
  • 注意事项:
    • 使用对象形式时,属性名要使用驼峰命名法。
    • 使用数组形式时,每个样式对象都是独立的,可以单独修改,但无法添加新的响应式属性。

2. Class属性的方法:

  • 字符串形式: 直接将类名字符串赋给:class,例如::class="'div1 div2'"
  • 列表形式: 将包含类名的数组赋给:class,例如::class="['div1', 'div2']"
  • 对象形式: 将包含类名及其对应的布尔值的对象赋给:class,例如::class="{ div1: true, div2: true, div3: false, div4: true, red: true }"
  • 注意事项:
    • 使用对象形式时,键为类名,值为布尔值,控制类名的显示与隐藏。
    • 使用数组形式时,每个类名都是独立的,可以单独修改,但无法添加新的响应式类名。

3. 注意事项:

  • 若要动态添加或删除样式或类名,应当通过Vue.js提供的响应式方法,如Vue.set()或Vue.js的数据绑定方式来操作,以确保响应式更新生效。
  • 在使用响应式数据更新样式或类名时,要注意避免直接修改数组或对象,应当使用Vue.js提供的响应式方法进行修改。
# 1 style和class 也是属性,比较特殊--》单独再讲讲

# 2 class 可以绑定 字符串,数组,对象
	字符串: 'div1 div2'---》字符串替换控制样式
    数组:['div1','div2']--->追加数组,删除数组控制样式
    对象:{div1: true, div2: true, div3: true, div4: false}--》通过true和false控制样式
    
# 3 style 可以绑定 字符串,数组,对象
	字符串'background-color: green;height: 300px;width: 300px'---》字符串替换控制样式
    数组:[{'background-color': 'green'}, {height: '300px'}, {width: '300px'}]--->追加数组,删除数组控制样式
    对象:{backgroundColor: 'green', height: '330px', width: '300px'} --》通过根据key修改value控制样式

    
    
# 4 坑:以后如果改了对象,或数组,发现页面没有变化,实际上值被真正的改了
	Vue.set(this.style_list,3,{'margin':'auto'})
    
# 5 哪个操作会触发响应式?哪个不会?
	-不用记----》以后有问题--》使用Vue.set
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://unpkg.com/vue@2.6.14/dist/vue.js"></script>
    <style>
        img {
            height: 300px;
            width: 200px;
        }

        .div1 {
            /* 宽高 */
            height: 300px;
            width: 300px;
        }

        .red {
            background-color: mediumvioletred;
        }

        .green {
            background-color: green;
        }

        .div2 {
            /* 背景色 */
            background-color: lightpink;
        }

        .div3 {
            /* 字体 */
            font-size: 60px;
        }

        .div4 {
            /* 居中 */
            margin: auto;
        }

    </style>
</head>
<body>

<div id="app">
    <img :src="image" alt="" v-show="ishow">
    <p @click="hand" style="color: aqua">点我点我</p>
    <br>
    <br>
    <button @click="backyun">点我切换背景色!!</button>
    <hr>
    <div :class="backing">
        点我就对了
    </div>


    <h2>class属性方法</h2>
    <!--<div :class="class_str">wis</div>-->

    <!--<div :class="class_list">wis</div>-->

    <div :class="class_obj">wis</div>

    <button @click="handtext">点点点我</button>

    <hr>

    <h2>style属性的方法</h2>
    <!--   字符串 -->
    <!--    <div :style="style_json">style属性</div>-->
    <!--    数组 -->
<!--    <div :style="style_list">style属性</div>-->
    <!--    对象 -->
        <div :style="style_obj">style属性</div>

    <button @click="handstyle">点点点点</button>

</div>
<script>


    var vm = new Vue({
        el: "#app",
        data: {
            image: 'img/14.jpg',
            ishow: false,
            backing: "blue",
            //字符串
            class_str: "div1 div2",
            //列表格式
            class_list: ["div1", "div2"],
            // 对象的格式
            class_obj: {div1: true, div2: true, div3: false, div4: true, red: true},

            //style字符串
            style_json: "background-color: aqua;height: 250px;width:250px;",
            //style数组
            style_list: [{'background-color': 'green'}, {height: '300px'}, {width: '300px'}],
            // style对象
            style_obj: {backgroundColor: 'red',height: "360px",width:'300px'},
        },
        methods: {
            hand() {
                this.ishow = !this.ishow;
            },
            backyun() {
                this.backing = this.backing == "blue" ? "green" : "red";
            },
            handtext() {
                // 字符串格式
                // this.class_str = 'div1 div2 div3 div4'‘
                // 列表格式
                // this.class_list.push("div3");
                // 删除 默认末尾
                // this.class_list.pop()


                // 对象
                this.class_obj.div3 = true

            }
            ,
            handstyle() {
                //     字符串形式
                // this.style_json = "background-color: aqua; height: 250px; width: 250px; font-size: 60px;margin: auto;";
                // 数组形式
                // this.style_list.push({"margin": "auto"}, {"font-size": "60px"})

                // 换背景色
                // 第一种方法
                // this.style_list[0]['background-color'] = 'black';
                //第二种方法
                // Vue.set(this.style_list,0,{"background-color": "pink"});

                //对象形式
                // this.style_obj["font-size"] = "60px" // 这个就是原来列表中没有,后来在加进去,就不会触发响应式。

                //使用set解决
                Vue.set(this.style_obj,"font-size","60px");
            }
        }
    })
</script>

</body>
</html>

image-20240425212910816

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值