1、网页布局排版
让页面的块级元素横着排列可以采用以下方式:
display: flex; /*将父元素设置为弹性盒子,让它的儿子横着排列*/
flex-wrap: wrap; /*它的儿子横着排后超出部分自动换行排列*/
justify-content: space-around; /*设置儿子排列方式为两端对齐*/
子元素横着排列方式有以下几种:
justify-content:排列方式
排列方式:
flex-start 左对齐
flex-end 右对齐
space-between 两端对齐(靠边)
space-around 两边对齐(不靠边)
课堂练习代码示例:
<template>
<div>
<div class="mainbox">
<div class="box">
<img class="img" :src="img1" >
<div class="lable1">
<div class="ziying">自营</div>
<div class="hot">热卖</div>
</div>
<h3 class="title">{{title}}</h3>
<p class="price">{{price}}</p>
<p class="content">{{content}}</p>
</div>
<div class="box">
<img class="img" :src="img2" >
<div class="lable1">
<div class="ziying">自营</div>
<div class="hot">热卖</div>
</div>
<h3 class="title">{{title}}</h3>
<p class="price">{{price}}</p>
<p class="content">{{content}}</p>
</div>
<div class="box">
<img class="img" :src="img3" >
<div class="lable1">
<div class="ziying">自营</div>
<div class="hot">热卖</div>
</div>
<h3 class="title">{{title}}</h3>
<p class="price">{{price}}</p>
<p class="content">{{content}}</p>
</div>
<div class="box">
<img class="img" :src="img4" >
<div class="lable1">
<div class="ziying">自营</div>
<div class="hot">热卖</div>
</div>
<h3 class="title">{{title}}</h3>
<p class="price">{{price}}</p>
<p class="content">{{content}}</p>
</div>
<div class="box">
<img class="img" :src="img5" >
<div class="lable1">
<div class="ziying">自营</div>
<div class="hot">热卖</div>
</div>
<h3 class="title">{{title}}</h3>
<p class="price">{{price}}</p>
<p class="content">{{content}}</p>
</div>
<div class="box">
<img class="img" :src="img6" >
<div class="lable1">
<div class="ziying">自营</div>
<div class="hot">热卖</div>
</div>
<h3 class="title">{{title}}</h3>
<p class="price">{{price}}</p>
<p class="content">{{content}}</p>
</div>
<div class="box">
<img class="img" :src="img7" >
<div class="lable1">
<div class="ziying">自营</div>
<div class="hot">热卖</div>
</div>
<h3 class="title">{{title}}</h3>
<p class="price">{{price}}</p>
<p class="content">{{content}}</p>
</div>
<div class="box">
<img class="img" :src="img8" >
<div class="lable1">
<div class="ziying">自营</div>
<div class="hot">热卖</div>
</div>
<h3 class="title">{{title}}</h3>
<p class="price">{{price}}</p>
<p class="content">{{content}}</p>
</div>
<div class="box">
<img class="img" :src="img9" >
<div class="lable1">
<div class="ziying">自营</div>
<div class="hot">热卖</div>
</div>
<h3 class="title">{{title}}</h3>
<p class="price">{{price}}</p>
<p class="content">{{content}}</p>
</div>
<div class="box">
<img class="img" :src="img10" >
<div class="lable1">
<div class="ziying">自营</div>
<div class="hot">热卖</div>
</div>
<h3 class="title">{{title}}</h3>
<p class="price">{{price}}</p>
<p class="content">{{content}}</p>
</div>
</div>
</div>
</template>
<script setup>
let img1="https://i1.mifile.cn/a1/pms_1500287084.72131750!220x220.jpg"
let img2="https://i1.mifile.cn/a1/pms_1550642182.7527088!220x220.jpg"
let img3="https://i1.mifile.cn/a1/pms_1560161251.21862332!220x220.jpg"
let img4="https://i1.mifile.cn/a1/pms_1560151939.71121260!220x220.png"
let img5="https://i1.mifile.cn/a1/pms_1515404353.30516645!220x220.jpg"
let img6="https://i1.mifile.cn/a1/pms_1500287084.72131750!220x220.jpg"
let img7="https://i1.mifile.cn/a1/pms_1500287084.72131750!220x220.jpg"
let img8="https://i1.mifile.cn/a1/pms_1500287084.72131750!220x220.jpg"
let img9="https://i1.mifile.cn/a1/pms_1500287084.72131750!220x220.jpg"
let img10="https://i1.mifile.cn/a1/pms_1500287084.72131750!220x220.jpg"
let title="某某标题"
let content="商品内容"
let price="价格"
</script>
<style>
.mainbox{
width: 1170px;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
background-color: rgb(240, 240, 240);
}
.box{
width: 200px;
height:400px;
background-color:white;
margin: 5px;
}
.img{
width: 100%;
}
.lable1{
display: flex;
flex-wrap: nowrap;
justify-content: space-around;
}
.ziying{
width: 60px;
height: 30px;
background-color: orangered;
color: white;
line-height: 30px;
text-align: center;
font-weight: 600;
border-radius: 10px;
}
.hot{
width: 60px;
height: 30px;
background-color: mediumvioletred;
color: white;
line-height: 30px;
text-align: center;
font-weight: 600;
border-radius: 10px;
}
.price{
color: orangered;
margin-top: 20px;
text-align: center;
}
.title{
text-align: center;
font-size: 20px;
}
.content{
margin-top: 10px;
margin-left: 10px;
}
</style>
结果展示:
2. 循环渲染网页元素
2-1变量的对象集合
将一个对象的所有属性写在一个地方,如下图所示的obj={name:"成都理工",age:1000}
- obj可以自行命名,name为键,age为值
- 引用方式需要在前标注自定义名字然后.键
2-2数组:
如下图所示
2-3采用循环语句遍历元素
如下图所示,其中el为变量名,可以任意取
课堂练习代码:
<template>
<div class="box-top" >
<div class="box1" v-for="el in arr1">
{{el}}
</div>
</div>
<div class="box-bottom" >
<div class="boxs" v-for="el in arr2">
<img class="img" :src="el.img" >
<div class="box">
<div>
<h3 class="title">{{el.title}}</h3>
</div>
<div class="lookcounts">
<p>{{el.lookcounts}}</p>
</div>
</div>
<div class="box">
<div>
<img class="userimg" :src="el.userimg" >
</div>
<div>
<p class="nickname">{{el.nickname}}</p>
</div>
<div class="lable">
<p>{{el.lable}}</p>
</div>
</div>
</div>
</div>
</template>
<script setup>
let arr1=["论坛精选","先锋","风光","新手","生活","儿童","手机","旅行","纪实","生态","鸟类","北京","天津","上海","广州","单反","索尼","佳能","尼康","腾龙","富士","…更多"]
let arr2=[{img:"https://th.bing.com/th/id/OIP.TzQLuNduoUdcyFrGP5Z2oAHaE6?w=266&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7",title:"秋日江南风景异",nickname:"空鸣",lable:"生活摄影",lookcounts:"3750",userimg:"https://image3.fengniao.com/head/10744/source/10743469_2.jpg?imageView2/1/w/180/h/180/q/75/ignore-error/1/"},
{img:"https://th.bing.com/th/id/OIP.TzQLuNduoUdcyFrGP5Z2oAHaE6?w=266&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7",title:"秋日江南风景异",nickname:"空鸣",lable:"生活摄影",lookcounts:"3750",userimg:"https://image3.fengniao.com/head/10744/source/10743469_2.jpg?imageView2/1/w/180/h/180/q/75/ignore-error/1/"},
{img:"https://th.bing.com/th/id/OIP.TzQLuNduoUdcyFrGP5Z2oAHaE6?w=266&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7",title:"秋日江南风景异",nickname:"空鸣",lable:"生活摄影",lookcounts:"3750",userimg:"https://image3.fengniao.com/head/10744/source/10743469_2.jpg?imageView2/1/w/180/h/180/q/75/ignore-error/1/"},
{img:"https://th.bing.com/th/id/OIP.TzQLuNduoUdcyFrGP5Z2oAHaE6?w=266&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7",title:"秋日江南风景异",nickname:"空鸣",lable:"生活摄影",lookcounts:"3750",userimg:"https://image3.fengniao.com/head/10744/source/10743469_2.jpg?imageView2/1/w/180/h/180/q/75/ignore-error/1/"},
{img:"https://th.bing.com/th/id/OIP.TzQLuNduoUdcyFrGP5Z2oAHaE6?w=266&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7",title:"秋日江南风景异",nickname:"空鸣",lable:"生活摄影",lookcounts:"3750",userimg:"https://image3.fengniao.com/head/10744/source/10743469_2.jpg?imageView2/1/w/180/h/180/q/75/ignore-error/1/"},
{img:"https://th.bing.com/th/id/OIP.TzQLuNduoUdcyFrGP5Z2oAHaE6?w=266&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7",title:"秋日江南风景异",nickname:"空鸣",lable:"生活摄影",lookcounts:"3750",userimg:"https://image3.fengniao.com/head/10744/source/10743469_2.jpg?imageView2/1/w/180/h/180/q/75/ignore-error/1/"},
{img:"https://th.bing.com/th/id/OIP.TzQLuNduoUdcyFrGP5Z2oAHaE6?w=266&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7",title:"秋日江南风景异",nickname:"空鸣",lable:"生活摄影",lookcounts:"3750",userimg:"https://image3.fengniao.com/head/10744/source/10743469_2.jpg?imageView2/1/w/180/h/180/q/75/ignore-error/1/"},
{img:"https://th.bing.com/th/id/OIP.TzQLuNduoUdcyFrGP5Z2oAHaE6?w=266&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7",title:"秋日江南风景异",nickname:"空鸣",lable:"生活摄影",lookcounts:"3750",userimg:"https://image3.fengniao.com/head/10744/source/10743469_2.jpg?imageView2/1/w/180/h/180/q/75/ignore-error/1/"},
{img:"https://th.bing.com/th/id/OIP.TzQLuNduoUdcyFrGP5Z2oAHaE6?w=266&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7",title:"秋日江南风景异",nickname:"空鸣",lable:"生活摄影",lookcounts:"3750",userimg:"https://image3.fengniao.com/head/10744/source/10743469_2.jpg?imageView2/1/w/180/h/180/q/75/ignore-error/1/"},
{img:"https://th.bing.com/th/id/OIP.TzQLuNduoUdcyFrGP5Z2oAHaE6?w=266&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7",title:"秋日江南风景异",nickname:"空鸣",lable:"生活摄影",lookcounts:"3750",userimg:"https://image3.fengniao.com/head/10744/source/10743469_2.jpg?imageView2/1/w/180/h/180/q/75/ignore-error/1/"},
{img:"https://th.bing.com/th/id/OIP.TzQLuNduoUdcyFrGP5Z2oAHaE6?w=266&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7",title:"秋日江南风景异",nickname:"空鸣",lable:"生活摄影",lookcounts:"3750",userimg:"https://image3.fengniao.com/head/10744/source/10743469_2.jpg?imageView2/1/w/180/h/180/q/75/ignore-error/1/"},
{img:"https://th.bing.com/th/id/OIP.TzQLuNduoUdcyFrGP5Z2oAHaE6?w=266&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7",title:"秋日江南风景异",nickname:"空鸣",lable:"生活摄影",lookcounts:"3750",userimg:"https://image3.fengniao.com/head/10744/source/10743469_2.jpg?imageView2/1/w/180/h/180/q/75/ignore-error/1/"},
{img:"https://th.bing.com/th/id/OIP.TzQLuNduoUdcyFrGP5Z2oAHaE6?w=266&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7",title:"秋日江南风景异",nickname:"空鸣",lable:"生活摄影",lookcounts:"3750",userimg:"https://image3.fengniao.com/head/10744/source/10743469_2.jpg?imageView2/1/w/180/h/180/q/75/ignore-error/1/"},
{img:"https://th.bing.com/th/id/OIP.TzQLuNduoUdcyFrGP5Z2oAHaE6?w=266&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7",title:"秋日江南风景异",nickname:"空鸣",lable:"生活摄影",lookcounts:"3750",userimg:"https://image3.fengniao.com/head/10744/source/10743469_2.jpg?imageView2/1/w/180/h/180/q/75/ignore-error/1/"},
{img:"https://th.bing.com/th/id/OIP.TzQLuNduoUdcyFrGP5Z2oAHaE6?w=266&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7",title:"秋日江南风景异",nickname:"空鸣",lable:"生活摄影",lookcounts:"3750",userimg:"https://image3.fengniao.com/head/10744/source/10743469_2.jpg?imageView2/1/w/180/h/180/q/75/ignore-error/1/"},
{img:"https://th.bing.com/th/id/OIP.TzQLuNduoUdcyFrGP5Z2oAHaE6?w=266&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7",title:"秋日江南风景异",nickname:"空鸣",lable:"生活摄影",lookcounts:"3750",userimg:"https://image3.fengniao.com/head/10744/source/10743469_2.jpg?imageView2/1/w/180/h/180/q/75/ignore-error/1/"}]
</script>
<style scoped>
.box-top{
display: flex;
flex-wrap: wrap;
justify-content: space-around;
font-size: 17px;
height: 45px;
}
.box1{
line-height: 45px;
}
.box1:nth-child(1){
font-size: 30px;
}
.box-bottom{
display: flex;
flex-wrap: wrap;
justify-content: space-left;
background-color: rgb(245, 245, 245);
}
.box{
display: flex;
flex-wrap: wrap;
justify-content: flex-start;
height: 50px;
}
.boxs{
margin:30px;
background-color: white;
height: 380px;
width: 380px;
border-radius: 10px;
}
.userimg{
width: 30px;
height: 30px;
border-radius: 50%;
margin-top: 15px;
margin-left: 10px;
}
.title{
margin-left: 10px;
}
.nickname{
margin-left: 10px;
}
.lookcounts{
line-height: 25px;
color: rgb(181,181, 181);
margin-left: 180px;
}
.lable{
margin-left: 80px;
color: rgb(181,181, 181);
margin-left: 220px;
}
.img{
width: 100%;
}
</style>
结果图展示:
3.组件
通过组件可以实现同一页面分工成不同文件,再将这些文件引用到同一文件可以实现一个页面的完整功能。
引用方式如下图所示,其中Hqyj为自行命名,Nav1.vue是其中一个分工文件:
使用方式如下图所示,直接以标签的形式进行使用:
当每一个分工文件的style标签加上scoped标签才会避免不同分工文件使用同一选择器引起的冲突,如下图所示: