Navbar.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <script setup>
  2. import { ref, reactive, onMounted } from "vue";
  3. import { useRouter } from "vue-router";
  4. import { useMainStore } from "@/stores/store";
  5. import { useI18n } from "vue-i18n";
  6. import Login from "@/views/Login.vue";
  7. const router = useRouter();
  8. const store = useMainStore();
  9. const { t, locale } = useI18n();
  10. let menuShow = ref(false);
  11. let collegeMenuShow = ref(false);
  12. let otherMenuShow = ref(false);
  13. let loginMenuShow = ref(false);
  14. function toggleMenu() {
  15. collegeMenuShow.value = false;
  16. menuShow.value = !menuShow.value;
  17. }
  18. function handleClose() {
  19. store.loginDialog = false;
  20. }
  21. function handleMouseEvents(name, event) {
  22. const screenWidth = window.innerWidth;
  23. if (screenWidth >= 1280) {
  24. if (name === "college") {
  25. collegeMenuShow.value = event;
  26. } else if (name === "other") {
  27. otherMenuShow.value = event;
  28. } else {
  29. loginMenuShow.value = event;
  30. }
  31. } else {
  32. return;
  33. }
  34. }
  35. function handleClick(url) {
  36. menuShow.value = false;
  37. collegeMenuShow.value = false;
  38. otherMenuShow.value = false;
  39. router.push(url);
  40. }
  41. const collegeList = reactive([
  42. {
  43. title: "未來工藝",
  44. url: "/college-group/future",
  45. },
  46. {
  47. title: "技藝工藝",
  48. url: "/college-group/craft",
  49. },
  50. {
  51. title: "跨域增能",
  52. url: "/college-group/cross",
  53. },
  54. {
  55. title: "線上工藝",
  56. url: "/college-group/online",
  57. },
  58. {
  59. title: "希望工程",
  60. url: "/college-group/craft-for-all",
  61. },
  62. {
  63. title: "生活工藝",
  64. url: "/college-group/life",
  65. },
  66. // {
  67. // title: "修護工藝",
  68. // url: "/college-group/repair",
  69. // },
  70. // {
  71. // title: "世代工藝",
  72. // url: "/college-group/generation",
  73. // },
  74. // {
  75. // title: "青年工藝",
  76. // url: "/college-group/teenager",
  77. // },
  78. ]);
  79. const otherList = reactive([
  80. // {
  81. // title: "我要開課",
  82. // url: "/setup-courses",
  83. // },
  84. // {
  85. // title: "網站架構",
  86. // url: "/",
  87. // },
  88. ]);
  89. onMounted(() => {
  90. console.log("onMounted");
  91. localStorage.setItem("lang", "zh");
  92. });
  93. function changeLang() {
  94. let lang = localStorage.getItem("lang");
  95. console.log("lang", lang);
  96. if (lang === "zh") {
  97. locale.value = "en";
  98. localStorage.setItem("lang", "en");
  99. } else if (lang === "en") {
  100. locale.value = "zh";
  101. localStorage.setItem("lang", "zh");
  102. }
  103. }
  104. function handleLogout() {
  105. localStorage.removeItem("token");
  106. window.location.reload();
  107. }
  108. </script>
  109. <template>
  110. <div class="d-flex justify-space-between align-center navbar">
  111. <router-link :to="'/'">
  112. <img src="@/assets/img/logo.png" alt="" />
  113. </router-link>
  114. <ul class="menu align-center" :class="{ slider: menuShow }">
  115. <li>
  116. <router-link :to="'/crafts'">工藝學</router-link>
  117. </li>
  118. <li class="position-relative">
  119. <a
  120. href="javascript:;"
  121. @mouseover="collegeMenuShow = true"
  122. @mouseleave="collegeMenuShow = false"
  123. class="d-none d-lg-block"
  124. >工藝學群</a
  125. >
  126. <a
  127. href="javascript:;"
  128. @click="collegeMenuShow = !collegeMenuShow"
  129. class="d-block d-lg-none"
  130. >工藝學群
  131. <v-icon
  132. icon="mdi-chevron-down"
  133. class="toggle-icon"
  134. :class="{ slider: collegeMenuShow }"
  135. ></v-icon
  136. ></a>
  137. <div
  138. class="college-slider"
  139. :class="{ slider: collegeMenuShow }"
  140. @mouseover="handleMouseEvents('college', true)"
  141. @mouseleave="handleMouseEvents('college', false)"
  142. >
  143. <ul>
  144. <li v-for="(item, index) in collegeList" :key="index">
  145. <a href="javascript:;" @click="handleClick(item.url)">{{
  146. item.title
  147. }}</a>
  148. <!-- <router-link :to="item.url">{{ item.title }}</router-link> -->
  149. </li>
  150. </ul>
  151. </div>
  152. </li>
  153. <li>
  154. <router-link :to="'/news'">重要訊息</router-link>
  155. </li>
  156. <li>
  157. <router-link :to="'/course-list'">探索課程</router-link>
  158. </li>
  159. <!-- <li>
  160. <router-link :to="'/article'">知識文章</router-link>
  161. </li> -->
  162. <li class="position-relative">
  163. <v-dialog
  164. v-model="store.loginDialog"
  165. max-width="450"
  166. v-if="!store.loginState"
  167. >
  168. <template v-slot:activator="{ props }">
  169. <a href="javascript:;" v-bind="props">會員登入</a>
  170. </template>
  171. <Login @close="handleClose" />
  172. </v-dialog>
  173. <div v-else>
  174. <router-link
  175. :to="'/user/profile'"
  176. @mouseover="loginMenuShow = true"
  177. @mouseleave="loginMenuShow = false"
  178. class="d-none d-lg-block"
  179. >會員專區</router-link
  180. >
  181. <router-link :to="'/user/profile'" class="d-block d-lg-none"
  182. >會員專區</router-link
  183. >
  184. </div>
  185. <div
  186. v-if="store.loginState"
  187. class="college-slider"
  188. :class="{ slider: loginMenuShow }"
  189. @mouseover="handleMouseEvents('login', true)"
  190. @mouseleave="handleMouseEvents('login', false)"
  191. >
  192. <ul>
  193. <li>
  194. <a href="javascript:;" @click="handleLogout()"> 登出 </a>
  195. </li>
  196. </ul>
  197. </div>
  198. <!-- <router-link :to="'/user/profile'" v-else>會員專區</router-link> -->
  199. </li>
  200. <li>
  201. <router-link :to="'/setup-courses'">我要開課</router-link>
  202. </li>
  203. <li>
  204. <a href="javascript:;" @click="changeLang()">中/EN</a>
  205. </li>
  206. <li class="d-none d-lg-block">
  207. <v-icon icon="mdi-magnify"></v-icon>
  208. </li>
  209. <li class="d-none d-lg-block position-relative">
  210. <a
  211. href="javascript:;"
  212. @mouseover="otherMenuShow = true"
  213. @mouseleave="otherMenuShow = false"
  214. class="d-none d-lg-block"
  215. >
  216. <v-icon icon="mdi-menu"></v-icon
  217. ></a>
  218. <div
  219. class="college-slider"
  220. :class="{ slider: otherMenuShow }"
  221. @mouseover="handleMouseEvents('other', true)"
  222. @mouseleave="handleMouseEvents('other', false)"
  223. >
  224. <ul>
  225. <li v-for="(item, index) in otherList" :key="index">
  226. <a href="javascript:;" @click="handleClick(item.url)">{{
  227. item.title
  228. }}</a>
  229. </li>
  230. </ul>
  231. </div>
  232. </li>
  233. <li
  234. v-for="(item, index) in otherList"
  235. :key="index"
  236. class="d-block d-lg-none"
  237. >
  238. <a href="javascript:;" @click="handleClick(item.url)">{{
  239. item.title
  240. }}</a>
  241. <!-- <router-link :to="item.url">{{ item.title }}</router-link> -->
  242. </li>
  243. </ul>
  244. <a href="javascript:;" class="icon" @click="toggleMenu()">
  245. <v-icon icon="mdi-menu" class="mx-2"></v-icon>
  246. </a>
  247. </div>
  248. </template>
  249. <style lang="scss" scoped>
  250. .navbar {
  251. width: 90%;
  252. max-width: 1300px;
  253. margin: 0 auto 40px;
  254. padding: 0 35px 0 15px;
  255. border: 1px solid;
  256. position: relative;
  257. z-index: 1000;
  258. background: transparent;
  259. top: 40px;
  260. left: 0;
  261. right: 0;
  262. @media (max-width: 1280px) {
  263. padding: 10px 20px 10px 10px;
  264. }
  265. img {
  266. width: 100%;
  267. max-width: 370px;
  268. margin-top: 5px;
  269. @media (max-width: 600px) {
  270. max-width: 300px;
  271. }
  272. }
  273. .menu {
  274. display: flex;
  275. align-items: center;
  276. list-style: none;
  277. @media (max-width: 1280px) {
  278. position: absolute;
  279. top: 81px;
  280. left: 0;
  281. right: 0;
  282. z-index: 100;
  283. overflow: hidden;
  284. flex-direction: column;
  285. max-height: 0;
  286. transition: max-height 0.3s ease-in-out;
  287. box-shadow: 1px 1px 15px #c8c8c8;
  288. }
  289. @media (max-width: 600px) {
  290. top: 103%;
  291. }
  292. &.slider {
  293. max-height: 700px;
  294. overflow: initial;
  295. // border: 1px solid;
  296. // border-top: none;
  297. }
  298. & > li {
  299. margin-left: 27px;
  300. font-weight: 400;
  301. @media (max-width: 1280px) {
  302. width: 100%;
  303. margin-left: 0;
  304. background: #fff;
  305. border-bottom: 1px solid #f0f0f0;
  306. font-weight: 500;
  307. text-align: center;
  308. }
  309. a {
  310. width: 100%;
  311. display: block;
  312. padding: 40px 0;
  313. color: #000;
  314. text-decoration: none;
  315. transition: all 0.3s;
  316. &:hover {
  317. opacity: 0.8;
  318. }
  319. @media (max-width: 1280px) {
  320. padding: 20px 0;
  321. }
  322. }
  323. }
  324. .college-slider {
  325. display: none;
  326. position: absolute;
  327. top: 80px;
  328. left: -30px;
  329. width: 130px;
  330. background: #fff;
  331. text-align: center;
  332. box-shadow: 1px 1px 15px #c8c8c8;
  333. transition: all 0.3s;
  334. @media (max-width: 1280px) {
  335. position: initial;
  336. width: auto;
  337. max-height: 0;
  338. box-shadow: none;
  339. transition: none;
  340. overflow: hidden;
  341. }
  342. // @media (max-width: 1280px) {
  343. // top: 30px;
  344. // left: 50vw;
  345. // }
  346. // @media (max-width: 600px) {
  347. // left: 58vw;
  348. // width: 100px;
  349. // }
  350. &.slider {
  351. display: block;
  352. @media (max-width: 1280px) {
  353. max-height: 100%;
  354. }
  355. }
  356. ul {
  357. @media (max-width: 1280px) {
  358. padding-bottom: 15px;
  359. }
  360. li {
  361. transition: all 0.3s;
  362. &:last-child {
  363. border-bottom: none;
  364. }
  365. &:hover {
  366. background-color: #eee;
  367. @media (max-width: 1280px) {
  368. opacity: 0.8;
  369. background-color: #fff;
  370. }
  371. }
  372. a {
  373. display: block;
  374. width: 100%;
  375. padding: 15px 10px;
  376. @media (max-width: 1280px) {
  377. padding: 15px 10px 15px 40px;
  378. }
  379. }
  380. }
  381. }
  382. }
  383. }
  384. .toggle-icon {
  385. margin-left: 5px;
  386. position: absolute;
  387. top: 17px;
  388. transition: all 0.3s;
  389. &.slider {
  390. transform: rotate(180deg);
  391. }
  392. }
  393. .icon {
  394. display: none;
  395. transition: all 0.3s;
  396. @media (max-width: 1280px) {
  397. display: block;
  398. }
  399. &:hover {
  400. opacity: 0.8;
  401. }
  402. .v-icon {
  403. font-size: 2rem;
  404. }
  405. }
  406. &::before {
  407. content: "";
  408. position: absolute;
  409. top: 6px;
  410. left: 6px;
  411. width: 99%;
  412. height: 88%;
  413. background-color: rgba(255, 255, 255, 0.5);
  414. z-index: -1;
  415. @media (max-width: 960px) {
  416. top: 7px;
  417. left: 1.3vw;
  418. width: 97%;
  419. height: 83%;
  420. }
  421. }
  422. }
  423. </style>