当前位置:   article > 正文

vue 实现菜单栏 PC 移动端自动转换_vue导航栏移动端pc端

vue导航栏移动端pc端

vue 实现菜单栏 PC 移动端自动转换

效果图

在这里插入图片描述
在这里插入图片描述

完整代码

App.vue

<!--
 * @Description: 
 * @Version: 1.0
 * @Autor: solid
 * @Date: 2021-11-29 18:28:41
 * @LastEditors: solid
 * @LastEditTime: 2022-07-04 17:16:57
-->
<template>
  <div id="app">
    <HelloWorld />
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";

export default {
  name: "App",
  components: {
    HelloWorld,
  },
  created() {},
  data() {
    return {
      screenWidth: document.body.clientWidth,
      timer: false,
      html: "",
    };
  },
  methods: {
    isMobile() {
      const ua = navigator.userAgent.toLowerCase();
      const t1 =
        /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(
          ua
        );
      const t2 = !ua.match("iphone") && navigator.maxTouchPoints > 1;
      return t1 || t2;
    },
  },
  mounted() {
    this.html = document.getElementsByTagName("html")[0];
    if (!this.isMobile()) {
      this.html.classList.add("PC");
    }else{
      this.html.classList.add("mobile");
    }
    window.onresize = () => {
      return (() => {
        window.screenWidth = document.body.clientWidth;
        this.screenWidth = window.screenWidth;
      })();
    };
  },
  watch: {
    screenWidth(val) {
      // 为了避免频繁触发resize函数导致页面卡顿,使用定时器
      if (!this.timer) {
        if (!this.isMobile()) {
          this.html.classList.remove("PC");
          this.html.classList.add("mobile");
          return;
        }
        // 一旦监听到的screenWidth值改变,就将其重新赋给data里的screenWidth
        this.screenWidth = val;
        this.timer = true;
        setTimeout(() => {
          this.html.classList.remove("mobile");
          this.html.classList.add("PC");
          this.timer = false;
        }, 50);
      }
    },
  },
};
</script>

<style>
html,
body {
  padding: 0;
  margin: 0;
}
#app {
  padding: 0;
  margin: 0;
}
</style>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90

HelloWorld.vue

<!--
 * @Description: 
 * @Version: 1.0
 * @Autor: solid
 * @Date: 2021-11-29 18:28:41
 * @LastEditors: solid
 * @LastEditTime: 2022-07-04 17:49:19
-->
<template>
  <div class="header">
    <div class="header-fixed">
      <div class="header-inner">
        <div class="header-left">
          <router-link
            class="header-left-logo"
            to="/index"
            tag="div"
          ></router-link>
          <div class="header-left-menu">
            <div class="menu-icon" @click="openMenu">
              <img :src="'https://www.ibox.art/zh-cn/static/img/5de48f4.v1656490708425.svg'" alt="" style="width:30px;height:30px">
            </div>
            <div class="menu-logo" />
          </div>
        </div>
        <div class="header-right">
          <router-link
            class="header-right-text"
            :to="item.href"
            tag="div"
            v-for="item in menuList"
            :key="item.title"
            >{{ item.title }}</router-link
          >
          <div class="header-right-login">登录</div>
        </div>
      </div>
    </div>
    <div class="popup">
      <div class="popup-mask-layer" @click="show = false" v-show="show"></div>
      <div class="popup-body" :class="show ? 'popup-show' : 'popup-hidden'">
        <div class="popup-content">
          <div class="menu-sidebar">
            <div class="menu-item" v-for="item in menuList" :key="item.title">
              <router-link :to="item.href" tag="span" @click.native="toLink">{{
                item.title
              }}</router-link>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      show: false,
      menuList: [
        {
          title: "主页",
          href: "/index",
        },
        {
          title: "我的",
          href: "/mine",
        },
        {
          title: "公告",
          href: "/notice",
        },
        {
          title: "帮助",
          href: "/help",
        },
        {
          title: "用户协议",
          href: "/UserInstructions",
        },
        {
          title: "隐私协议",
          href: "/privacyPolicy",
        },
      ],
    };
  },
  methods: {
    openMenu(){
      this.show = true;
    }
  },
  mounted() {},
};
</script>

<style scoped>
.header {
  width: 100%;
  height: 60px;
}
.header-fixed {
  height: 80px;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  padding: 0 15px;
  border-bottom: 1px solid rgba(199, 187, 237, 0.5);
  z-index: 10071;
  background: #fff;
  box-sizing: border-box;
}
.PC .header-inner {
  width: 1200px;
  height: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 auto;
}
.PC .header-left-logo {
  width: 200px;
  height: 100px;
  background: url("https://www.ibox.art/zh-cn/static/img/8bfeda1.v1656490708425.png")
    no-repeat;
  background-size: cover;
  cursor: pointer;
}
.PC .header-right {
  width: 70%;
  display: flex;
  justify-content: space-between;
  font-weight: 700;
  font-size: 14px;
  align-items: center;
}
.PC .header-right-text {
  color: #2c2c34;
  cursor: pointer;
  font-size: 14px;
}
.PC .header-right-login {
  background: #0066ed;
  border: none;
  padding: 10px 15px;
  -webkit-border-radius: 20px;
  -moz-border-radius: 20px;
  border-radius: 20px;
  font-size: 14px;
  color: #fff;
  white-space: nowrap;
  cursor: pointer;
}
.PC .router-link-active {
  color: #0066ed;
}

.PC .popup {
  display: none;
}
.PC .header-left-menu{
  display: none;
}
</style>
<style scoped>
.mobile .header-right-text{
  display: none;
}
.mobile .header-inner{
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 80px;
  background-color: #fff;
}
.mobile .header-left-menu{
  display: flex;
  align-items: center;
}
.mobile .menu-logo {
  margin-left: 10px;
  width: 120px;
  height: 60px;
  background: url("https://www.ibox.art/zh-cn/static/img/8bfeda1.v1656490708425.png") no-repeat;
  background-size: contain;
}
 .popup {
  flex: 1;
  transition: all 350ms;
}
.popup-mask-layer {
  transition-property: opacity;
  transition-duration: 350ms;
  transition-timing-function: ease-out;
  position: fixed;
  inset: 0px;
  z-index: 10070;
  background-color: rgba(0, 0, 0, 0.5);
}
.popup-body {
  transition-duration: 300ms;
  transition-timing-function: ease-out;
  transition-property: transform, -webkit-transform;
  z-index: 10075;
  position: fixed;
  display: flex;
  left: 0px;
  bottom: 0px;
  top: 0px;
}
.popup-content {
  background-color: #fff;
  position: relative;
  margin-top: 64px;
  overflow: auto;
  flex: 1 1 0%;
}
.menu-sidebar {
  padding: 10px;
}
.menu-item {
  width: 150px;
  height: 60px;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
  border-bottom: 1px solid #ebebeb;
}
.menu-item > span {
  margin-left: 11px;
}
.popup-show {
  transform: translateX(0);
}

.popup-hidden {
  transform: translateX(-100%);
}
.router-link-active {
  color: #0066ed;
}
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/76940
推荐阅读
相关标签
  

闽ICP备14008679号