原来我觉得的头标签就只是原装的<head></head>,而后在里面balabala写,结果今天才知道,它也是能够“我想怎么样就怎么样”的~
来左边跟我一块儿放个logo,来右边放一个编辑~css
为了适应不一样的显示器,咱们用px单位可想而知是不够的。因此咱们用rem作到全部设备自适应。rem是css的相对单位。
html
确定是不能写死的,用动态,如:16rem=100%,这个就是动态宽度的100%。
16其实就是引入了栅格系统,将一行分为16列,好比设为8:8,它其实就是两列左右对等布局,4:4:4其实就是三列式布局。那么要怎么作呢?要对html的fontSize进行一个计算。vue
新建的项目只须要勾选babel和router便可。
html5
在main.js中,先导入咱们要的rem。
import './config/rem'
大型项目中组件如何兼容? rem是第一项要考虑的。
git
src/config/rem.js,在这里手动计算rem。github
(function(doc, win){
var docEl = doc.documentElement, //获取html
// docEl.style.fontSize = '23.5px'; //先写死一个值测试一下
})(document, window) // 建一个闭包, 当即执行函数,传两个实参
复制代码
(function(doc, win){
var docEl = doc.documentElement, //获取html
recalc = function() {
// 设备无差别计算出 16rem = 100% width
var clientWidth = docEl.clientWidth; //html的整个窗口的宽度
// console.log(clientWidth);
if (!clientWidth) return;
docEl.style.fontSize = 20 * clientWidth/320 + 'px';
// console.log(clientWidth);
};
doc.addEventListener('DOMContentLoaded', recalc, false);
// docEl.style.fontSize = '23.5px'; //先写死一个值测试一下
})(document, window) // 建一个闭包, 当即执行函数,传两个实参
复制代码
doc.addEventListener('DOMContentLoaded', recalc, false);
监听DOMContentLoaded事件。在vue里面也有生命周期,DOMContentLoaded事件比loaded事件先发生,由于loaded要等到全部图片都加载完,整个页面都加载完了才触发,而DOMContentLoaded事件只要DOM结构加载完了(html结构)加载完成了,就会触发。此时咱们就去计算fontSize的大小。,当它触发DOMContentLoaded事件的时候,咱们去调用recalc函数,第三个参数为false。20 * clientWidth/320
呢?<div style="width:16rem;height:2rem;background-color:red;"></div>
复制代码
好比设备可能由纵向,变成横向,还有可能屏幕尺寸发生改变。那怎么作呢?bash
(function(doc, win){
var docEl = doc.documentElement, //获取html
// 是否换了方向,横屏? 调整窗口宽度->resize事件
resizeEvt = 'orientationchange' in win ? 'orientationchange': 'resize',
recalc = function() {
// 设备无差别计算出 16rem = 100% width
var clientWidth = docEl.clientWidth; //html的整个窗口的宽度
// console.log(clientWidth);
if (!clientWidth) return;
docEl.style.fontSize = 20 * clientWidth/320 + 'px';
// console.log(clientWidth);
// ?设备可能由纵向变模着拿 尺寸发生变化
win.addEventListener(resizeEvt, recalc, false);
};
doc.addEventListener('DOMContentLoaded', recalc, false);
// docEl.style.fontSize = '23.5px'; //先写死一个值测试一下
})(document, window) // 建一个闭包, 当即执行函数,传两个实参
复制代码
来到App.vue,先定义一个组件:babel
export default {
components: { //组件声明
// 不能与标签名字header重合,怎么解决?
// head-top是在页面上的标签名 Header是类名
"head-top": Header
}
}
复制代码
将咱们自定义的header封装:
import Header from './components/header/header'; //封装这个组件
闭包
在components下建一个header/header.vue 下面先展现一个错误的:less
<template>
<header id="head_top">
header
</header>
</template>
复制代码
"head-top": Header
,其中"head-top"
就是咱们在页面上写的占位符(标签名)。而Header
实际上是类名。<template>
<head-top id="编辑地址">
header
</head-top>
</template>
复制代码
<head-top headTitle="编辑地址">
<span class="head_logo" >ele.me</span>
</head-top>
复制代码
来到header.vue
<script>
export default {
props: ['headTitle']
}
</script>
复制代码
<header id="head_top">
<!-- slot在{{headTitle}}前面,因此就插好了 用一个name -->
<slot name="logo"></slot>
<!-- section是一个区块 这里写了两个类名 -->
<!--用户传了headTitle咱们才输出,不然不输出-->
<section class="title_head ellipsis" v-if="headTitle">
<span class="title_text">
{{headTitle}}
</span>
</section>
</header>
复制代码
<style lang="styl" scoped>
@import "../../style/mixin.styl";
/* $blue是定义的stylus变量
position fixed固定定位*/
#head_top
background-color $blue
position fixed
z-index 100
left 0
top 0
wh(100%, 1.95rem)
.title_head
center()
width 50%
color #fff
text-align center
.title_text
sc(0.8rem, #fff)
text-align center
font-weight bold
.head_goback
left 0.4rem
wh(0.6rem, 1rem)
line-height 2.2rem
margin-left 0.4rem
</style>
复制代码
咱们用的是stylus,因此记得先install一下,在终端中yarn add stylus stylus-loader
。
import导入样式文件时必定必定记得加分号!
4. 在这里面咱们复用了样式,写在了mixin.styl文件中,注意看哦!
<!--定义了一个超级变量blue-->
$blue = #3190e8;
wh($width, $height)
width $width
height $height
center()
top 50%
left 50%
position absolute
transform translate(-50%, -50%)
sc($size, $color)
font-size $size
color $color
ct()
top 50%
position absolute
transform translateY(-50%)
复制代码
一样用slot方式插在标题右边:
<section class="title_head ellipsis" v-if="headTitle">
<span class="title_text">
{{headTitle}}
</span>
</section>
<slot name="edit"></slot>
复制代码
<head-top head-title="编辑地址" go-back="true">
<span class="head_logo" slot="logo">ele.me</span>
<!-- slot的name来区分编辑和logo的不一样位置 -->
<span class="edit" slot="edit">编辑</span>
</head-top>
复制代码
header.vue中
<!-- section是一个区块 -->
<section class="head_goback" v-if="goBack" @click="$router.go(-1)">
<svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg" version="1.1">
<polyline points="12,18 4,9 12,0" style="fill:none;stroke:rgb(255,255,255);stroke-width:2"/>
</svg>
</section>
复制代码
@click="$router.go(-1)"
能够实现单击后返回上一层。
简单的一个关于rem,和header,以及css模块化的小例子就分享到这了,喜欢的点个start~
github戳这里