topMenu.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <template>
  2. <div class="menu_select" :class="{'fixed-top': headOver && basicSet.set.set_top == 1,'padding0': basicSet.set.set_top == 1}">
  3. <div v-html="css"></div>
  4. <div class="select_content m-auto flex">
  5. <!--商品分类-->
  6. <div class="item item-200 flex flex-j-c flex-a-c" @click.stop="openClass" :class="{'showClass': showClass}" v-if="basicCategory.data">
  7. <span>全部商品分类</span><img src="~/assets/images/icon/select.png" class="down_arrow">
  8. <div class="hide_menu otherClass double_hide_menu" style="width:100%;" :class="{'show-all': basicSet.set.category_show == 1 && !headOver && isHome > -1}" v-if="!isClick">
  9. <div style="position: relative;width:100%;">
  10. <div class="hide_item flex flex-j-c flex-a-c" @click.stop="openChild(ind)" :class="{'showChild': showClass && showChild == ind}" v-for="(item,ind) in basicCategory.data.slice(0,10)"
  11. :key="ind"> <span>{{item.name}}</span>
  12. <div class="hide_item_item" @click.stop>
  13. <div class="title flex flex-a-c" @click.stop="toCategoryChild({id: item.id})"><span class="line"></span>{{item.name}}分类</div>
  14. <div class="no-goods" v-if="!item.children || item.children.length <= 0">
  15. 暂无更多
  16. </div>
  17. <div class="icon-box">
  18. <div class="icon" v-for="(obj,bidx) in item.children" :key="bidx"
  19. @click.stop="toCategoryChild({id: obj.id})">
  20. <div class="img flex flex-a-c flex-j-c">
  21. <img :src="obj.thumb_src">
  22. </div>
  23. <p class="icon-name">{{obj.name}}</p>
  24. </div>
  25. </div>
  26. </div>
  27. <div class="adv-img-box" @click.stop="toLink(item)">
  28. <img v-if="item.pc_adv_img_src" :src="item.pc_adv_img_src" alt="">
  29. </div>
  30. </div>
  31. <div class="hide_item flex flex-j-c flex-a-c" v-if="basicCategory.total > 10"
  32. @click.stop="toUrl('category')">查看更多分类
  33. </div>
  34. </div>
  35. </div>
  36. </div>
  37. <!--商品分类end-->
  38. <!--后台自定义链接-->
  39. <div class="item item-1 flex flex-j-c flex-a-c" v-for="(item,index) in basicSet.nav_top" :key="index"
  40. @click="toLink(item)">
  41. <img :src="item.img_src" v-if="item.img_src" style="margin: 3px 10px 0 0;" class="nav-icon"> <span>{{item.nav_name}}</span>
  42. <img src="~/assets/images/icon/select.png" v-if="item.sub_nav && item.sub_nav.length>0" class="down_arrow">
  43. <div class="hide_menu">
  44. <template v-if="item.sub_nav && item.sub_nav.length>0">
  45. <div class="hide_item flex flex-j-c flex-a-c" v-for="(list,ind) in item.sub_nav" :key="ind"
  46. @click="toLink(list)">{{list.nav_name}}</div>
  47. </template>
  48. </div>
  49. </div>
  50. <!--后台自定义链接end-->
  51. </div>
  52. </div>
  53. </template>
  54. <script>
  55. import {mapState} from "vuex";
  56. // 自定义样式
  57. const css = function () {
  58. return `
  59. .menu_select {
  60. --dark_color: ${this.dark_color}
  61. }
  62. .menu_select .select_content .hide_menu{
  63. background-color: ${this.dark_color};
  64. }
  65. `;
  66. };
  67. export default {
  68. props: {
  69. menu: {
  70. type: Array
  71. }
  72. },
  73. data() {
  74. return {
  75. showClass: false,
  76. showChild: -1,
  77. isClick: false,
  78. isHome: -1,
  79. headOver: false,
  80. dark_color: "#ffffff",
  81. category_template: '01'
  82. };
  83. },
  84. computed: {
  85. ...mapState(["basicSet", "basicCategory", "basic_info"]),
  86. css() {
  87. return "<style>" + css.call(this) + "</style>";
  88. }
  89. },
  90. watch: {
  91. '$route'(res) {
  92. if (res.name == 'home') {
  93. this.isHome = 1;
  94. }else {
  95. this.isHome = -1;
  96. if(res.name == 'catelist-id'){
  97. if(this.basicSet.set.pc_temp == 2){
  98. this.isHome = 1;
  99. }
  100. }
  101. }
  102. }
  103. },
  104. mounted() {
  105. let color =
  106. this.basicSet.set && this.basicSet.set.theme_color
  107. ? this.basicSet.set.theme_color
  108. : "#29ba9c";
  109. this.dark_color = this.changeColor(color, 0.15, true);
  110. // 获取分类模板
  111. if (this.basic_info.home && this.basic_info.home.item && this.basic_info.home.item.is_decorate === 1) {
  112. if (this.basic_info.home.item.ViewSet.category.is_default != 0) {
  113. this.category_template = this.basic_info.home.item.ViewSet.category.code.substring(8);
  114. } else {
  115. this.category_template = "01"
  116. }
  117. }
  118. this.isHome = window.location.href.indexOf('/home?') || window.location.href.indexOf('/home/?');
  119. window.addEventListener('scroll', this.handleScroll);
  120. if(this.$route.name == 'catelist-id'){
  121. // 分类模板2要求不显示下拉
  122. if(this.basicSet.set.pc_temp == 2){
  123. this.isHome = 1;
  124. }
  125. }
  126. },
  127. destroyed() {
  128. window.removeEventListener('scroll', this.handleScroll);
  129. },
  130. methods: {
  131. pad(num, totalChars) {
  132. var pad = "0";
  133. num = num + "";
  134. while (num.length < totalChars) {
  135. num = pad + num;
  136. }
  137. return num;
  138. },
  139. // Ratio is between 0 and 1
  140. changeColor(color, ratio, darker) {
  141. // Trim trailing/leading whitespace
  142. color = color.replace(/^\s*|\s*$/, "");
  143. // Expand three-digit hex
  144. color = color.replace(
  145. /^#?([a-f0-9])([a-f0-9])([a-f0-9])$/i,
  146. "#$1$1$2$2$3$3"
  147. );
  148. // Calculate ratio
  149. let difference = Math.round(ratio * 256) * (darker ? -1 : 1),
  150. // Determine if input is RGB(A)
  151. rgb = color.match(
  152. new RegExp(
  153. "^rgba?\\(\\s*" +
  154. "(\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])" +
  155. "\\s*,\\s*" +
  156. "(\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])" +
  157. "\\s*,\\s*" +
  158. "(\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])" +
  159. "(?:\\s*,\\s*" +
  160. "(0|1|0?\\.\\d+))?" +
  161. "\\s*\\)$",
  162. "i"
  163. )
  164. ),
  165. alpha = !!rgb && rgb[4] != null ? rgb[4] : null,
  166. // Convert hex to decimal
  167. decimal = !!rgb
  168. ? [rgb[1], rgb[2], rgb[3]]
  169. : color
  170. .replace(
  171. /^#?([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i,
  172. function () {
  173. return (
  174. parseInt(arguments[1], 16) +
  175. "," +
  176. parseInt(arguments[2], 16) +
  177. "," +
  178. parseInt(arguments[3], 16)
  179. );
  180. }
  181. )
  182. .split(/,/),
  183. returnValue;
  184. // Return RGB(A)
  185. return !!rgb
  186. ? "rgb" +
  187. (alpha !== null ? "a" : "") +
  188. "(" +
  189. Math[darker ? "max" : "min"](
  190. parseInt(decimal[0], 10) + difference,
  191. darker ? 0 : 255
  192. ) +
  193. ", " +
  194. Math[darker ? "max" : "min"](
  195. parseInt(decimal[1], 10) + difference,
  196. darker ? 0 : 255
  197. ) +
  198. ", " +
  199. Math[darker ? "max" : "min"](
  200. parseInt(decimal[2], 10) + difference,
  201. darker ? 0 : 255
  202. ) +
  203. (alpha !== null ? ", " + alpha : "") +
  204. ")"
  205. : // Return hex
  206. [
  207. "#",
  208. this.pad(
  209. Math[darker ? "max" : "min"](
  210. parseInt(decimal[0], 10) + difference,
  211. darker ? 0 : 255
  212. ).toString(16),
  213. 2
  214. ),
  215. this.pad(
  216. Math[darker ? "max" : "min"](
  217. parseInt(decimal[1], 10) + difference,
  218. darker ? 0 : 255
  219. ).toString(16),
  220. 2
  221. ),
  222. this.pad(
  223. Math[darker ? "max" : "min"](
  224. parseInt(decimal[2], 10) + difference,
  225. darker ? 0 : 255
  226. ).toString(16),
  227. 2
  228. )
  229. ].join("");
  230. },
  231. openClass() {
  232. this.showClass = !this.showClass;
  233. if(!this.showClass) {
  234. this.showChild = -1;
  235. }
  236. },
  237. openChild(ind) {
  238. if(this.showChild == ind) {
  239. this.showChild = -1;
  240. }else {
  241. this.showChild = ind;
  242. }
  243. },
  244. toCategoryChild(params) {
  245. this.isClick = true;
  246. this.showClass = false;
  247. this.showChild = -1;
  248. if (this.category_template === '03') {
  249. if(this.$baseURL) {
  250. // 静态部署环境下
  251. this.$router.push(this.fun.getUrl('category_child',{}, params));
  252. }else {
  253. this.$router.push(this.fun.getUrl('category_child-id', params));
  254. }
  255. }else {
  256. if(this.$baseURL) {
  257. // 静态部署环境下
  258. this.$router.push(this.fun.getUrl('catelist',{}, params));
  259. }else {
  260. this.$router.push(this.fun.getUrl('catelist-id', params));
  261. }
  262. }
  263. setTimeout(()=>{
  264. this.isClick = false;
  265. },500);
  266. },
  267. toUrl(url, params) {
  268. this.$router.push(this.fun.getUrl(url, params));
  269. },
  270. toLink(item) {
  271. this.showClass = false;
  272. this.showChild = -1;
  273. if(item.pc_adv_url) {
  274. window.open(item.pc_adv_url, "_blank");
  275. return;
  276. }
  277. if ((item.sub_nav && item.sub_nav.length > 0) || !item.url) {
  278. return;
  279. }
  280. if(item.url) {
  281. window.open(item.url, "_blank");
  282. }
  283. },
  284. handleScroll() {
  285. let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
  286. if (scrollTop > 132) {
  287. this.headOver = true;
  288. } else {
  289. this.headOver = false;
  290. }
  291. },
  292. }
  293. };
  294. </script>
  295. <style lang="scss" scoped>
  296. .menu_select {
  297. width: 100%;
  298. /*margin: 0 auto 10px auto;*/
  299. padding-top: 130px;
  300. height: 45px;
  301. background-color: var(--color);
  302. .select_content {
  303. width: 1200px;
  304. .item:last-child {
  305. margin-right: 0;
  306. }
  307. .item {
  308. color: #fff;
  309. font-size: 16px;
  310. height: 44px;
  311. cursor: pointer;
  312. position: relative;
  313. padding: 0 20px;
  314. /*margin-right: 50px;*/
  315. .nav-icon {
  316. width: 20px;
  317. height: 20px;
  318. }
  319. .down_arrow {
  320. margin-left: 8px;
  321. /*display: none;*/
  322. }
  323. .hide_menu {
  324. opacity: 0;
  325. position: absolute;
  326. /*background-color: #2c7a60;*/
  327. top: 43px;
  328. white-space: nowrap;
  329. z-index: 99;
  330. transition: all 0.5s;
  331. width: 100%;
  332. .hide_item {
  333. transition: all 0.5s;
  334. min-height: 0px;
  335. height: 0px;
  336. opacity: 0;
  337. width: 100%;
  338. display: flex;
  339. font-size: 0;
  340. padding: 0 20px;
  341. box-sizing: border-box;
  342. word-break: break-all;
  343. white-space: pre-line;
  344. .hide_item_item::-webkit-scrollbar {display:none}
  345. .adv-img-box {
  346. display: none;
  347. position: absolute;
  348. width: 260px;
  349. height: 100%;
  350. left: 940px;
  351. z-index: 99;
  352. top: 0;
  353. img {
  354. width: 260px;
  355. height: 100%;
  356. }
  357. }
  358. .hide_item_item {
  359. transition: all 0.3s;
  360. display: none;
  361. background-color: #fff;
  362. box-shadow: 0px 2px 10px 0px rgba(171, 171, 171, 0.5);
  363. width: 720px;
  364. height: 100%;
  365. min-height: 300px;
  366. padding: 15px 40px;
  367. overflow-y: scroll;
  368. box-sizing: border-box;
  369. position: absolute;
  370. left: 220px;
  371. z-index: 99;
  372. top: 0;
  373. color: #333;
  374. .line {
  375. width: 3px;
  376. height: 15px;
  377. background-color: var(--color);
  378. display: inline-block;
  379. margin-right: 4px;
  380. }
  381. .title {
  382. font-weight: bold;
  383. font-size: 15px;
  384. }
  385. .icon-box {
  386. display: flex;
  387. flex-wrap: wrap;
  388. }
  389. .icon {
  390. display: inline-block;
  391. margin: 8px 15px;
  392. text-align: center;
  393. width: 90px;
  394. .img {
  395. width: 90px;
  396. height: 90px;
  397. border-radius: 50%;
  398. margin-bottom: 14px;
  399. img {
  400. border-radius: 50%;
  401. width: 100%;
  402. height: 100%;
  403. }
  404. }
  405. }
  406. .icon-name {
  407. width: 90px;
  408. overflow: hidden;
  409. white-space: nowrap;
  410. text-overflow: ellipsis;
  411. }
  412. }
  413. &:hover {
  414. background-color: #fff;
  415. color: var(--color);
  416. .hide_item_item {
  417. display: block;
  418. }
  419. .adv-img-box {
  420. display: block;
  421. }
  422. }
  423. &.showChild {
  424. background-color: #fff;
  425. color: var(--color);
  426. .hide_item_item {
  427. display: block;
  428. }
  429. .adv-img-box {
  430. display: block;
  431. }
  432. }
  433. .no-goods {
  434. margin: 20px;
  435. font-size: 16px;
  436. color: var(--color);
  437. }
  438. }
  439. }
  440. }
  441. .item-200 {
  442. background-color: var(--dark_color);
  443. min-width: 200px;
  444. padding: 0 10px;
  445. }
  446. .item:hover {
  447. background-color: var(--dark_color);
  448. .hide_menu {
  449. opacity: 1;
  450. .hide_item {
  451. min-height: 47px;
  452. height: auto;
  453. font-size: 14px;
  454. opacity: 1;
  455. }
  456. }
  457. .otherClass {
  458. height: 470px;
  459. }
  460. .down_arrow {
  461. display: block;
  462. }
  463. .double_hide_menu {
  464. display: flex;
  465. min-width: 200px;
  466. }
  467. }
  468. .item.showClass {
  469. background-color: var(--dark_color);
  470. .hide_menu {
  471. opacity: 1;
  472. .hide_item {
  473. min-height: 47px;
  474. height: auto;
  475. font-size: 14px;
  476. opacity: 1;
  477. }
  478. }
  479. .otherClass {
  480. height: 470px;
  481. }
  482. .down_arrow {
  483. display: block;
  484. }
  485. .double_hide_menu {
  486. display: flex;
  487. min-width: 200px;
  488. }
  489. }
  490. }
  491. }
  492. .menu_select.fixed-top {
  493. position: fixed;
  494. left: 50%;
  495. top: 0;
  496. z-index: 100;
  497. transform: translateX(-50%);
  498. }
  499. .padding0 {
  500. padding-top: 0;
  501. }
  502. .double_hide_menu.show-all {
  503. opacity: 1!important;
  504. display: flex;
  505. min-width: 200px;
  506. height: 470px;
  507. .hide_item {
  508. min-height: 47px!important;
  509. height: auto;
  510. font-size: 14px!important;
  511. opacity: 1!important;
  512. }
  513. }
  514. </style>