s-menu-button.vue 8.6 KB

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