s-menu-button.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. // 直接从 props.styles 解构
  171. const {
  172. bgType,
  173. bgImg,
  174. bgColor
  175. } = props.styles;
  176. // 根据 bgType 返回相应的样式
  177. return {
  178. background: bgType === 'img' ? `url(${bgImg}) no-repeat top center / 100% 100%` : bgColor
  179. };
  180. });
  181. // 生成数据
  182. const menuList = computed(() => splitData(props.data.list, props.data.row * props.data.column));
  183. const swiperHeight = computed(() => props.data.row * (props.data.layout === 'iconText' ? 200 : 180));
  184. const windowWidth = sheep.$platform.device.windowWidth;
  185. // current 改变时会触发 change 事件
  186. const swiperChange = (e) => {
  187. state.cur = e.detail.current;
  188. };
  189. // 重组数据
  190. const splitData = (oArr = [], length = 1) => {
  191. let arr = [];
  192. let minArr = [];
  193. oArr.forEach((c) => {
  194. if (minArr.length === length) {
  195. minArr = [];
  196. }
  197. if (minArr.length === 0) {
  198. arr.push(minArr);
  199. }
  200. minArr.push(c);
  201. });
  202. return arr;
  203. };
  204. </script>
  205. <style lang="scss" scoped>
  206. .grid-wrap {
  207. width: 100%;
  208. display: flex;
  209. position: relative;
  210. box-sizing: border-box;
  211. overflow: hidden;
  212. flex-wrap: wrap;
  213. align-items: center;
  214. }
  215. .menu-box {
  216. position: relative;
  217. z-index: 1;
  218. transform: translate(0, 0);
  219. .tag-box {
  220. position: absolute;
  221. z-index: 2;
  222. top: 0;
  223. right: -6rpx;
  224. font-size: 2em;
  225. line-height: 1;
  226. padding: 0.4em 0.6em 0.3em;
  227. transform: scale(0.4) translateX(0.5em) translatey(-0.6em);
  228. transform-origin: 100% 0;
  229. border-radius: 200rpx;
  230. white-space: nowrap;
  231. }
  232. .menu-icon {
  233. transform: translate(0, 0);
  234. width: 80rpx;
  235. height: 80rpx;
  236. padding-bottom: 10rpx;
  237. }
  238. .menu-title {
  239. font-size: 24rpx;
  240. color: #333;
  241. }
  242. }
  243. ::v-deep(.ui-swiper) {
  244. position: relative;
  245. z-index: 1;
  246. .ui-swiper-dot {
  247. position: absolute;
  248. width: 100%;
  249. bottom: 20rpx;
  250. height: 30rpx;
  251. display: flex;
  252. align-items: center;
  253. justify-content: center;
  254. z-index: 2;
  255. &.default .line-box {
  256. display: inline-flex;
  257. border-radius: 50rpx;
  258. width: 6px;
  259. height: 6px;
  260. border: 2px solid transparent;
  261. margin: 0 10rpx;
  262. opacity: 0.3;
  263. position: relative;
  264. justify-content: center;
  265. align-items: center;
  266. &.cur {
  267. width: 8px;
  268. height: 8px;
  269. opacity: 1;
  270. border: 0px solid transparent;
  271. }
  272. &.cur::after {
  273. content: '';
  274. border-radius: 50rpx;
  275. width: 4px;
  276. height: 4px;
  277. background-color: #fff;
  278. }
  279. }
  280. &.long .line-box {
  281. display: inline-block;
  282. border-radius: 100rpx;
  283. width: 6px;
  284. height: 6px;
  285. margin: 0 10rpx;
  286. opacity: 0.3;
  287. position: relative;
  288. &.cur {
  289. width: 24rpx;
  290. opacity: 1;
  291. }
  292. &.cur::after {}
  293. }
  294. &.line {
  295. bottom: 20rpx;
  296. .line-box {
  297. display: inline-block;
  298. width: 30px;
  299. height: 3px;
  300. opacity: 0.3;
  301. position: relative;
  302. &.cur {
  303. opacity: 1;
  304. }
  305. }
  306. }
  307. &.tag {
  308. justify-content: flex-end;
  309. position: absolute;
  310. bottom: 20rpx;
  311. right: 20rpx;
  312. }
  313. }
  314. }
  315. </style>