s-menu-button.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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(() => props.data.row * (props.data.layout === 'iconText' ? 200 : 180));
  204. const windowWidth = sheep.$platform.device.windowWidth;
  205. // current 改变时会触发 change 事件
  206. const swiperChange = (e) => {
  207. state.cur = e.detail.current;
  208. };
  209. // 重组数据
  210. const splitData = (oArr = [], length = 1) => {
  211. let arr = [];
  212. let minArr = [];
  213. oArr.forEach((c) => {
  214. if (minArr.length === length) {
  215. minArr = [];
  216. }
  217. if (minArr.length === 0) {
  218. arr.push(minArr);
  219. }
  220. minArr.push(c);
  221. });
  222. return arr;
  223. };
  224. </script>
  225. <style lang="scss" scoped>
  226. .grid-wrap {
  227. width: 100%;
  228. display: flex;
  229. position: relative;
  230. box-sizing: border-box;
  231. overflow: hidden;
  232. flex-wrap: wrap;
  233. align-items: center;
  234. }
  235. .menu-box {
  236. position: relative;
  237. z-index: 1;
  238. transform: translate(0, 0);
  239. .tag-box {
  240. position: absolute;
  241. z-index: 2;
  242. top: 0;
  243. right: -6rpx;
  244. font-size: 2em;
  245. line-height: 1;
  246. padding: 0.4em 0.6em 0.3em;
  247. transform: scale(0.4) translateX(0.5em) translatey(-0.6em);
  248. transform-origin: 100% 0;
  249. border-radius: 200rpx;
  250. white-space: nowrap;
  251. }
  252. .menu-icon {
  253. transform: translate(0, 0);
  254. width: 80rpx;
  255. height: 80rpx;
  256. padding-bottom: 10rpx;
  257. }
  258. .menu-title {
  259. font-size: 24rpx;
  260. color: #333;
  261. }
  262. }
  263. ::v-deep(.ui-swiper) {
  264. position: relative;
  265. z-index: 1;
  266. .ui-swiper-dot {
  267. position: absolute;
  268. width: 100%;
  269. bottom: 20rpx;
  270. height: 30rpx;
  271. display: flex;
  272. align-items: center;
  273. justify-content: center;
  274. z-index: 2;
  275. &.default .line-box {
  276. display: inline-flex;
  277. border-radius: 50rpx;
  278. width: 6px;
  279. height: 6px;
  280. border: 2px solid transparent;
  281. margin: 0 10rpx;
  282. opacity: 0.3;
  283. position: relative;
  284. justify-content: center;
  285. align-items: center;
  286. &.cur {
  287. width: 8px;
  288. height: 8px;
  289. opacity: 1;
  290. border: 0px solid transparent;
  291. }
  292. &.cur::after {
  293. content: '';
  294. border-radius: 50rpx;
  295. width: 4px;
  296. height: 4px;
  297. background-color: #fff;
  298. }
  299. }
  300. &.long .line-box {
  301. display: inline-block;
  302. border-radius: 100rpx;
  303. width: 6px;
  304. height: 6px;
  305. margin: 0 10rpx;
  306. opacity: 0.3;
  307. position: relative;
  308. &.cur {
  309. width: 24rpx;
  310. opacity: 1;
  311. }
  312. &.cur::after {
  313. }
  314. }
  315. &.line {
  316. bottom: 20rpx;
  317. .line-box {
  318. display: inline-block;
  319. width: 30px;
  320. height: 3px;
  321. opacity: 0.3;
  322. position: relative;
  323. &.cur {
  324. opacity: 1;
  325. }
  326. }
  327. }
  328. &.tag {
  329. justify-content: flex-end;
  330. position: absolute;
  331. bottom: 20rpx;
  332. right: 20rpx;
  333. }
  334. }
  335. }
  336. </style>