s-menu-button.vue 8.6 KB

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