s-menu-button.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <!-- 装修基础组件:菜单导航(金刚区) -->
  2. <template>
  3. <!-- 包裹层 -->
  4. <view class="ui-swiper" :class="[props.mode, props.ui]"
  5. :style="[bgStyle, { height: swiperHeight + (menuList.length > 1 ? 50 : 0) + 'rpx' }]">
  6. <!-- 轮播 -->
  7. <swiper :circular="props.circular" :current="state.cur" :autoplay="props.autoplay" :interval="props.interval"
  8. :duration="props.duration" :style="[{ height: swiperHeight + 'rpx' }]" @change="swiperChange">
  9. <swiper-item v-for="(arr, index) in menuList" :key="index" :class="{ cur: state.cur == index }">
  10. <!-- 宫格 -->
  11. <view class="grid-wrap">
  12. <view v-for="(item, index) in arr" :key="index"
  13. class="grid-item ss-flex ss-flex-col ss-col-center ss-row-center"
  14. :style="[{ width: `${100 * (1 / data.column)}%`, height: '200rpx' }]" hover-class="ss-hover-btn"
  15. @tap="sheep.$router.go(item.url)">
  16. <view class="menu-box ss-flex ss-flex-col ss-col-center ss-row-center">
  17. <view v-if="item.badge.show" class="tag-box"
  18. :style="[{ background: item.badge.bgColor, color: item.badge.textColor }]">
  19. {{ item.badge.text }}
  20. </view>
  21. <image v-if="item.iconUrl" class="menu-icon" :style="[
  22. {
  23. width: props.iconSize + 'rpx',
  24. height: props.iconSize + 'rpx',
  25. },
  26. ]" :src="sheep.$url.cdn(item.iconUrl)" mode="aspectFill"></image>
  27. <view v-if="data.layout === 'iconText'" class="menu-title"
  28. :style="[{ color: item.titleColor }]">
  29. {{ item.title }}
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. </swiper-item>
  35. </swiper>
  36. <!-- 指示点 -->
  37. <template v-if="menuList.length > 1">
  38. <view class="ui-swiper-dot" :class="props.dotStyle" v-if="props.dotStyle != 'tag'">
  39. <view class="line-box" v-for="(item, index) in menuList.length" :key="index"
  40. :class="[state.cur == index ? 'cur' : '', props.dotCur]"></view>
  41. </view>
  42. <view class="ui-swiper-dot" :class="props.dotStyle" v-if="props.dotStyle == 'tag'">
  43. <view class="ui-tag radius" :class="[props.dotCur]" style="pointer-events: none">
  44. <view style="transform: scale(0.7)">{{ state.cur + 1 }} / {{ menuList.length }}</view>
  45. </view>
  46. </view>
  47. </template>
  48. </view>
  49. </template>
  50. <script setup>
  51. /**
  52. * 轮播menu
  53. *
  54. * @property {Boolean} circular = false - 是否采用衔接滑动,即播放到末尾后重新回到开头
  55. * @property {Boolean} autoplay = true - 是否自动切换
  56. * @property {Number} interval = 5000 - 自动切换时间间隔
  57. * @property {Number} duration = 500 - 滑动动画时长,app-nvue不支持
  58. * @property {Array} list = [] - 轮播数据
  59. * @property {String} ui = '' - 样式class
  60. * @property {String} mode - 模式
  61. * @property {String} dotStyle - 指示点样式
  62. * @property {String} dotCur= 'ui-BG-Main' - 当前指示点样式,默认主题色
  63. * @property {String} bg - 背景
  64. *
  65. * @property {String|Number} col = 4 - 一行数量
  66. * @property {String|Number} row = 1 - 几行
  67. * @property {String} hasBorder - 是否有边框
  68. * @property {String} borderColor - 边框颜色
  69. * @property {String} background - 背景
  70. * @property {String} hoverClass - 按压样式类
  71. * @property {String} hoverStayTime - 动画时间
  72. *
  73. * @property {Array} list - 导航列表
  74. * @property {Number} iconSize - 图标大小
  75. * @property {String} color - 标题颜色
  76. *
  77. */
  78. import {
  79. reactive,
  80. computed
  81. } from 'vue';
  82. import sheep from '@/sheep';
  83. // 数据
  84. const state = reactive({
  85. cur: 0,
  86. });
  87. // 接收参数
  88. const props = defineProps({
  89. // 装修数据
  90. data: {
  91. type: Object,
  92. default: () => ({}),
  93. },
  94. // 装修样式
  95. styles: {
  96. type: Object,
  97. default: () => ({}),
  98. },
  99. circular: {
  100. type: Boolean,
  101. default: true,
  102. },
  103. autoplay: {
  104. type: Boolean,
  105. default: false,
  106. },
  107. interval: {
  108. type: Number,
  109. default: 5000,
  110. },
  111. duration: {
  112. type: Number,
  113. default: 500,
  114. },
  115. ui: {
  116. type: String,
  117. default: '',
  118. },
  119. mode: {
  120. //default
  121. type: String,
  122. default: 'default',
  123. },
  124. dotStyle: {
  125. type: String,
  126. default: 'long', //default long tag
  127. },
  128. dotCur: {
  129. type: String,
  130. default: 'ui-BG-Main',
  131. },
  132. height: {
  133. type: Number,
  134. default: 300,
  135. },
  136. // 是否有边框
  137. hasBorder: {
  138. type: Boolean,
  139. default: true,
  140. },
  141. // 边框颜色
  142. borderColor: {
  143. type: String,
  144. default: 'red',
  145. },
  146. background: {
  147. type: String,
  148. default: 'blue',
  149. },
  150. hoverClass: {
  151. type: String,
  152. default: 'ss-hover-class', //'none'为没有hover效果
  153. },
  154. // 一排宫格数
  155. col: {
  156. type: [Number, String],
  157. default: 3,
  158. },
  159. iconSize: {
  160. type: Number,
  161. default: 80,
  162. },
  163. color: {
  164. type: String,
  165. default: '#000',
  166. },
  167. });
  168. // 设置背景样式
  169. const bgStyle = computed(() => {
  170. console.log(props.styles)
  171. // 直接从 props.styles 解构
  172. const {
  173. bgType,
  174. bgImg,
  175. bgColor
  176. } = props.styles;
  177. // 根据 bgType 返回相应的样式
  178. return {
  179. background: bgType === 'img' ? `url(${bgImg}) no-repeat top center / 100% 100%` : bgColor
  180. };
  181. });
  182. // 生成数据
  183. const menuList = computed(() => splitData(props.data.list, props.data.row * props.data.column));
  184. const swiperHeight = computed(() => props.data.row * (props.data.layout === 'iconText' ? 200 : 180));
  185. const windowWidth = sheep.$platform.device.windowWidth;
  186. // current 改变时会触发 change 事件
  187. const swiperChange = (e) => {
  188. state.cur = e.detail.current;
  189. };
  190. // 重组数据
  191. const splitData = (oArr = [], length = 1) => {
  192. let arr = [];
  193. let minArr = [];
  194. oArr.forEach((c) => {
  195. if (minArr.length === length) {
  196. minArr = [];
  197. }
  198. if (minArr.length === 0) {
  199. arr.push(minArr);
  200. }
  201. minArr.push(c);
  202. });
  203. return arr;
  204. };
  205. </script>
  206. <style lang="scss" scoped>
  207. .grid-wrap {
  208. width: 100%;
  209. display: flex;
  210. position: relative;
  211. box-sizing: border-box;
  212. overflow: hidden;
  213. flex-wrap: wrap;
  214. align-items: center;
  215. }
  216. .menu-box {
  217. position: relative;
  218. z-index: 1;
  219. transform: translate(0, 0);
  220. .tag-box {
  221. position: absolute;
  222. z-index: 2;
  223. top: 0;
  224. right: -6rpx;
  225. font-size: 2em;
  226. line-height: 1;
  227. padding: 0.4em 0.6em 0.3em;
  228. transform: scale(0.4) translateX(0.5em) translatey(-0.6em);
  229. transform-origin: 100% 0;
  230. border-radius: 200rpx;
  231. white-space: nowrap;
  232. }
  233. .menu-icon {
  234. transform: translate(0, 0);
  235. width: 80rpx;
  236. height: 80rpx;
  237. padding-bottom: 10rpx;
  238. }
  239. .menu-title {
  240. font-size: 24rpx;
  241. color: #333;
  242. }
  243. }
  244. ::v-deep(.ui-swiper) {
  245. position: relative;
  246. z-index: 1;
  247. .ui-swiper-dot {
  248. position: absolute;
  249. width: 100%;
  250. bottom: 20rpx;
  251. height: 30rpx;
  252. display: flex;
  253. align-items: center;
  254. justify-content: center;
  255. z-index: 2;
  256. &.default .line-box {
  257. display: inline-flex;
  258. border-radius: 50rpx;
  259. width: 6px;
  260. height: 6px;
  261. border: 2px solid transparent;
  262. margin: 0 10rpx;
  263. opacity: 0.3;
  264. position: relative;
  265. justify-content: center;
  266. align-items: center;
  267. &.cur {
  268. width: 8px;
  269. height: 8px;
  270. opacity: 1;
  271. border: 0px solid transparent;
  272. }
  273. &.cur::after {
  274. content: '';
  275. border-radius: 50rpx;
  276. width: 4px;
  277. height: 4px;
  278. background-color: #fff;
  279. }
  280. }
  281. &.long .line-box {
  282. display: inline-block;
  283. border-radius: 100rpx;
  284. width: 6px;
  285. height: 6px;
  286. margin: 0 10rpx;
  287. opacity: 0.3;
  288. position: relative;
  289. &.cur {
  290. width: 24rpx;
  291. opacity: 1;
  292. }
  293. &.cur::after {}
  294. }
  295. &.line {
  296. bottom: 20rpx;
  297. .line-box {
  298. display: inline-block;
  299. width: 30px;
  300. height: 3px;
  301. opacity: 0.3;
  302. position: relative;
  303. &.cur {
  304. opacity: 1;
  305. }
  306. }
  307. }
  308. &.tag {
  309. justify-content: flex-end;
  310. position: absolute;
  311. bottom: 20rpx;
  312. right: 20rpx;
  313. }
  314. }
  315. }
  316. </style>