首页 > 其他分享 >实例:事件绑定、样式绑定、class绑定、style绑定、条件渲染、循环渲染

实例:事件绑定、样式绑定、class绑定、style绑定、条件渲染、循环渲染

时间:2022-08-31 09:47:46浏览次数:57  
标签:style console log 渲染 color 绑定 height width

 

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <script src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/2.6.14/vue.js"></script>
</head>

<body>
    <style>
        .box1 {
            background-color: green;
            width: 300px;
            height: 300px;
        }

        .box2 {
            background-color: lawngreen;
            width: 200px;
            height: 200px;
        }

        .box3 {
            background-color: greenyellow;
            width: 100px;
            height: 100px;
        }

        .meta {
            background-color: black;
            width: 300px;
            height: 100px;
            color: aqua;
        }

        .meta2 {
            background-color: rgb(129, 129, 129);
            width: 300px;
            height: 100px;
            color: aqua;
        }

        button {
            width: 200px;
            height: 20px;
        }

        .lastbox {
            border-radius: 30px;
            text-align: center;
        }
    </style>
    <div class="content">
        <!-- 事件绑定 -->
        <h1>事件绑定</h1>
        <div class="box1" v-on:click="onbox1">{{box1}}
            <div class="box2" v-on:click="onbox2">{{box2}}
                <div class="box3" v-on:click="onbox3">{{box3}}
                </div>
            </div>
        </div>

        <!-- class绑定 -->
        <h1>class绑定</h1>
        <button @click="btn">{{tip}}</button>
        <div :class="{meta:m}"></div>
        <div :class="['lastbox',mode]">111111</div>

        <!-- style绑定 -->
        <h1>style绑定</h1>
        <div v-bind:style="{backgroundColor:bgcColor,color:txtColor,fontSize:txtFont+'px'}">
            {{text}}
        </div>
        <div :style="[box4,{backgroundColor:'lawngreen',display:d}]"></div>

        <h1>条件渲染</h1>
        <button @click="change">{{btnchange}}</button>
        <div v-if="flag">{{iF}}</div>
        <div v-show="flag">{{show}}</div>

        <h1>循环渲染</h1>
        <div v-for="ele in arr">
            <div :style="{width:'20px',height:'20px',borderRadius:'50%',backgroundColor:bC1}">
                {{ele.tip}}
            </div>
            <div :style="{width:'30px',height:'30px',borderRadius:'50%',backgroundColor:bC2}">
                {{ele.colorname}}
            </div>
            <div :style="{width:'50px',height:'50px',borderRadius:'50%',backgroundColor:bC3}">
                {{ele.cname}}
            </div>
        </div>
    </div>
    <script>
        let option = {
            el: ".content",
            data: {
                box1: "box1",
                box2: "box2",
                box3: "box3",
                m: true,
                mode: "meta",
                flag: true,
                tip: "点击",
                text: "一段文字",
                txtColor: "green",
                txtFont: 30,
                box4: {
                    width: "200px",
                    height: "20px",
                },
                d: "block",
                bgcColor: "green",
                btnchange: "点击产生条件渲染效果",
                iF: "v-if:删除节点",
                show: "v-show:display:none",
                arr: [{
                    tip: "1",
                    colorname: "green",
                    cname: "绿色"
                },
                {
                    tip: "2",
                    colorname: "lawngreen",
                    cname: "荧光绿"
                },
                {
                    tip: "3",
                    colorname: "greenyellow",
                    cname: "黄绿色"
                }],
                bC1:"green",
                bC2: "lawngreen",
                bC3:"greenyellow"
            },
            methods: {
                onbox1() { console.log("box1"); },
                onbox2() { console.log("box2"); },
                onbox3() { console.log("box3"); },
                btn() {
                    this.flag = !this.flag;
                    if (this.flag) {
                        //true
                        this.mode = "meta";
                        this.d = "block"
                        this.bgcColor = "green"
                        console.log("true");
                    } else {
                        this.mode = "meta2";
                        this.d = "none"
                        this.bgcColor = "darkgreen"
                        console.log("false");
                    }
                },
                change() {
                    this.flag = !this.flag;
                }
            },
        }
        var vm = new Vue(option)
        // console.log(vm, option.data, vm == option.data)

    </script>
</body>

</html>

 

标签:style,console,log,渲染,color,绑定,height,width
From: https://www.cnblogs.com/LIXI-/p/16641865.html

相关文章

  • vue3源码学习2-创建和渲染vnode
    创建vnode我们在第一节中在packages/runtime-core/src/apiCreateApp.ts文件的createAppAPI方法中,app.mount()时://通过createVNode方法创建了根组件的vnodeconstvnod......
  • vue+element 的使用问题记录-下拉列表绑定值为整数
     vue+element的使用问题记录 1、下拉列表绑定值为整数问题现象:下拉列表没有显示对应的文字,显示的是数字。  解决方法:对应的对象的类中的数据类型是Integer......
  • Vue-条件渲染
    条件渲染条件渲染的属性有两个:1.v-if/v-elsev-if的方法是删除元素<body> <divid="app"> <divv-if="flag">上课</div> <divv-if="n">......
  • Vue-循环渲染
    循环渲染循环渲染使用的是v-for<body> <divid="app"> <divv-for="iteminarr">{{item}}</div> <divv-for="iteminarr2">{{item.time}}</div> <div......
  • Vue-样式绑定
    1.对class属性进行绑定<style> .app{ width:200px; height:200px; background-color:purple; } .app1{ width:100px; height:100px; bac......
  • vue3 基础-表单元素双向绑定
    通常是在form表单相关的场景中会用到双向绑定相关,核心是v-model的应用.input输入框<!DOCTYPEhtml><htmllang="en"><head><title>input</title><script......
  • 条件渲染(面试)
    v-if/v-else或者v-show<divid="app"><divv-if="flag">hello</div><divv-show="flag">world</div></div><scripttype="text/javascript">newV......
  • 循环渲染(面试)
    v-for1.for和if放在了同一个标签中没有先后顺序的要求,但是先执行for渲染过程为:对arr每一项先做map循环判断v-if给出的条件,再做一遍for循环渲染这样引起的问题是:arr......
  • 数据绑定
    响应式数据:只能由代码改变UI或者只能由UI改变代码双向数据绑定:代码改变UI,UI也能改变代码双向数据绑定的实现:2种方式1.自己实现,vue可以自己实现(没必要)微信开发......
  • 样式绑定
    切换效果的实现:1、做切换效果的技术(样式绑定)2、组件或者模块的切换(动态组件v-if/v-show)3、路由切换(router)样式绑定(1)对class属性进行绑定 1、v-bind:class指......