vue2.0基础

样式绑定

style绑定

:style="{color:activeColor,fontSize:fontSize+'px'}"
:style="{style1,style2}"

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
    <style>
        .box {
            width: 300px;
            height: 300px;
            border: 1px solid #000;
        }
    </style>
</head>

<body>
    <div id="add">
        <!-- <div class="box" style="background: yellow;">张三</div> -->
        <!-- <div class="box" :style="{background: 'yellow'}"></div> -->
        <!-- <div class="box" :style="obj">
            张三
        </div> -->
        <div class="box" :style="[obj,obj1]">
            张三
        </div>
    </div>
</body>

</html>

其中activeColor/fontSize是data属性

class绑定

1: :class = "xxx"

2:表达式是字符串:"classA"

3: 表达式的对象:{{classA:isA,classB:isB}}

4: 表达式是数组:['classA','classB']

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
    <style>
        .box {
            width: 300px;
            height: 300px;
            border: 2px solid #000;
        }

        .box1 {
            background-color: yellow;
        }

        .box2 {
            font-weight: bold;
            font-size: 100px;
        }
    </style>
</head>

<body>
    <div id="add">
        <!-- 
            class样式绑定
            1.变量
            2.绑定对象
            3.绑定数组
         -->
        <!-- <div class="box" :class="{box1:true,box2:true}">
            张三
        </div> -->

        <!-- <div class="box" :class="obj">
            张三
        </div> -->

        <div class="box" :class="['box1','box2']">
          张三
        </div>
    </div>
</body>

</html>

vue自定义指令

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
</head>

<body>
    <div id="add">
        <div v-box></div>
    </div>
</body>
<script>
    let vm = new Vue({
        el: '#add',
        data() {
            return {

            }
        },
        directives: {
            box(e) {
                e.style.border = '1px solid #000'
                e.style.height = '150px'
                e.style.width = '300px'


                var input = document.createElement('input')
                input.style.background = '#ccc'
                input.autofocus = true;
                input.placeholder='请输入...'
                input.style.height = '20px'


                e.appendChild(input)
            }
        }
    })
</script>

</html>

自定义指令

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .box {
        width: 300px;
        height: 200px;
        border: 2px solid #000;
    }
</style>

<body>
    <div class="box">
        <!-- <input type="text"> -->
    </div>
</body>
<script>
    // 直接获取dom
    var box = document.querySelector('.box')
    var input = document.createElement('input')
    input.placeholder = "请输入..."
    input.autofocus = true
    input.style.background = 'yellow'

    box.appendChild(input)
</script>

</html>

页面滚动触底

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .item {
            width: 500px;
            height: 200px;
            border: 1px solid #000;
        }
    </style>
</head>

<body>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</body>

</html>

<script>
    window.addEventListener('scroll', () => {
        // console.log('123');

        // 可见区域高度
        let clientHeight = document.documentElement.clientHeight;

        // 整个文档高度
        let scrollHeight = document.documentElement.scrollHeight;
        // console.log(scrollHeight);

        // 滚动的高度
        let scrollTop = document.documentElement.scrollTop;

        if (clientHeight + scrollTop > scrollHeight) {
           console.log('触底了');
        }
    })
</script>

过滤器

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
</head>

<body>
    <div id="add">
        <!-- 管道符:竖线 | -->
        <div>{{message | myCut | myAdd}}</div>
    </div>
</body>

</html>

<script>
    let vm = new Vue({
        el: '#add',
        data: {
            message: '河北唐山欢迎你'
        },
        filters: {
            myCut(val) {
                console.log(val);
                return val.slice(4, 6)
            },
            myAdd(val) {
                return val + '你来河北唐山吃烧烤'
            }
        }
    })
</script>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值