s-menu-button.vue 8.7 KB

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