样式绑定
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>