可伸缩自适应的页面头部,屏幕适应范围更广泛
效果如下:
<template> <div class="site-header"> <div class="logo"><img src="@/assets/icons/logo.png" alt=""></div> <nav class="title"> <!--汉堡按钮图标--> <em class="iconfont icon-justify" @click.stop="toggle('title')"></em> <ul ref="title"> <li class="active"> <a href="#">{{ 首页}}</a> </li> <li> <a href="#">{{ 交易中心}}</a> </li> <li> <a href="#">{{ 用户中心}}</a> </li> <li> <a href="#">{{ 新闻 }}</a> </li> <li> <a href="#">{{ 关于我们 }}</a> </li> </ul> </nav> <div class="aboutlogin"> <!--用户头像图标--> <em class="iconfont icon-user" @click.stop="toggle('aboutlogin')"></em> <ul ref="aboutlogin"> <li class="active"> <a href="#">{{ 登录 }}</a> </li> <li> <a href="#">{{ 注册}}</a> </li> <li v-if="false"><a href="javascript:;">{{ 欢迎 }}</a> <span>{{ 555 }}</span></li> <li v-if="false"> <a href="javascript:;">{{退出}}</a> </li> </ul> </div> </div> </template> <script> export default { computed: { clickRootNum () { return this.$store.state.system.clickRootNum } }, methods: { toggle (ref) { if (ref === 'title') { this.$refs.aboutlogin.classList.remove('d-show') } else { this.$refs.title.classList.remove('d-show') } const ele = this.$refs[ref] const classname = ele.className if (classname.indexOf('d-show') > -1) { ele.classList.remove('d-show') } else { ele.classList.add('d-show') } } }, watch: { 'clickRootNum' () { // 点击页面任意位置关闭下拉列表,需要vuex的状态来配合 this.$refs.title.classList.remove('d-show') this.$refs.aboutlogin.classList.remove('d-show') } } } </script> <style scoped lang="scss"> /*定义的一些主题颜色*/ @import "../../assets/css/variate"; .d-show {display: block!important;} @media screen and (min- 781px) { .site-header{ height: 72px; display: flex; flex-direction: row; justify-content: space-between; border-bottom: 1px solid $themecolor; background-color: #fff; align-items: center; .logo{ margin-left: 10px; height: 52px; } .aboutlogin{ width: 250px; height: 100%; margin-right: 10px; .icon-user { display: none; } ul { display: flex!important; flex-direction: row; justify-content: space-around; li{ display: inline-block; line-height: 72px; font-size: 14px; cursor: pointer; color: #7a7a7a; &:hover>a { color: $themecolor; } a { color: #7a7a7a; display: inline-block; &:active{ transform: translateY(1px); } } &.active a{ color: $themehover; font-weight: bold; } } } } .title { width: 600px; margin: 0 20px; .icon-justify { display: none; } ul { display: flex!important; flex-direction: row; justify-content: space-between; li { display: inline-block; line-height: 72px; &:hover a{ background-color: $themecolor; padding: 2px 5px; color: #fff; } a{ color: $themecolor; font-size: 18px; padding: 2px 5px; &:active { background-color: $themehover; } } &.active a{ background-color: $themecolor; color: #fff; } } } } } } @media screen and (max- 780px) { .site-header{ height: 72px; border-bottom: 1px solid $themecolor; background-color: #fff; position: relative; .logo{ position: absolute; top: 0; bottom: 0; margin: auto 0; right: 10px; height: 52px; } .aboutlogin{ width: 60px; height: 100%; margin-right: 10px; position: absolute; left: 60px; .icon-user { font-size: 32px; color: #999; line-height: 72px; display: inline-block; cursor: pointer; } ul { display: none; background-color: #fff; position: absolute; left: -10px; top: 71px; width: 90px; li{ line-height: 36px; font-size: 14px; cursor: pointer; padding-left: 10px; color: #7a7a7a; &:hover>a { color: $themecolor; } a { color: #7a7a7a; display: inline-block; &:active{ transform: translateY(1px); } } } } } .title { width: 60px; position: absolute; left: 10px; .icon-justify { font-size: 32px; line-height: 72px; display: inline-block; cursor: pointer; } ul { display: none; position: absolute; left: -10px; top: 71px; width: 120px; background-color: #fff; li { line-height: 36px; padding-left: 6px; &:hover a{ background-color: $themecolor; color: #fff; } a{ color: $themecolor; padding: 0 6px; font-size: 18px; &:active { background-color: $themehover; } } &.active a{ background-color: $themecolor; color: #fff; } } } } } } </style>
来源:https://www.cnblogs.com/qddyh/p/10386403.html