index.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <template>
  2. <div class="simple-city" id="simple-city">
  3. <div class="select-group">
  4. <div
  5. class="select"
  6. v-for="(item, index) in placeholderValue"
  7. :key="index"
  8. @click.stop="onClick(item, index)"
  9. >
  10. <span
  11. v-if="selectedValue[index]"
  12. v-text="selectedValue[index][labelName]"
  13. ></span>
  14. <span v-text="item.label" v-else></span>
  15. </div>
  16. </div>
  17. <div class="option-group">
  18. <keep-alive>
  19. <div class="options" v-if="showOptions" :class="[translateX]">
  20. <div class="scroll-box">
  21. <div class="option" @click="onOptionsClick(emptyData)">全部</div>
  22. <div
  23. class="option"
  24. v-for="item in list"
  25. :key="item[valueName]"
  26. @click="onOptionsClick(item)"
  27. v-text="item[labelName]"
  28. ></div>
  29. </div>
  30. </div>
  31. </keep-alive>
  32. </div>
  33. </div>
  34. </template>
  35. <script>
  36. export default {
  37. props: {
  38. labelName: {
  39. type: String,
  40. default: 'label',
  41. },
  42. valueName: {
  43. type: String,
  44. default: 'value',
  45. },
  46. options: {
  47. type: Array,
  48. default: () => [],
  49. },
  50. },
  51. data() {
  52. return {
  53. selected: 0,
  54. showOptions: false,
  55. offset: 0,
  56. current: 0,
  57. emptyData: {
  58. label: '全部',
  59. value: '',
  60. },
  61. placeholderValue: [
  62. {
  63. label: '省份',
  64. },
  65. {
  66. label: '城市',
  67. },
  68. {
  69. label: '区县',
  70. },
  71. ],
  72. selectedValue: [],
  73. list: [],
  74. }
  75. },
  76. computed: {
  77. translateX() {
  78. return 'offset-' + this.current
  79. },
  80. },
  81. beforeDestroy() {
  82. window.removeEventListener('click', () => {})
  83. },
  84. mounted() {
  85. window.addEventListener('click', ($event) => {
  86. this.showOptions = false
  87. })
  88. },
  89. methods: {
  90. onClick(item, index) {
  91. if (index === 0 || this.selectedValue[index - 1]) {
  92. this.offset = item.offset
  93. this.current = index
  94. this.filterList()
  95. this.showOptions = true
  96. } else {
  97. this.showOptions = false
  98. }
  99. },
  100. onOptionsClick(item) {
  101. const valueName = this.valueName
  102. if (this.current === 0) {
  103. this.selectedValue = []
  104. } else if (this.current === 1) {
  105. this.$set(this.selectedValue, 2, undefined)
  106. }
  107. if (typeof item[valueName] === 'number' || item[valueName]) {
  108. this.$set(this.selectedValue, this.current, item)
  109. } else {
  110. this.$set(this.selectedValue, this.current, undefined)
  111. }
  112. this.selectedValue = this.selectedValue.filter((item) => item)
  113. this.$emit('change', this.selectedValue)
  114. },
  115. filterList() {
  116. if (this.current === 0) {
  117. this.list = this.options
  118. return
  119. }
  120. const parent = this.selectedValue[this.current - 1]
  121. this.list = parent.children
  122. },
  123. setSelectValue(value) {
  124. this.selectedValue = value
  125. },
  126. },
  127. }
  128. </script>
  129. <style scoped lang="scss">
  130. @media screen and (min-width: 768px) {
  131. .simple-city {
  132. .select-group {
  133. .select {
  134. display: inline-block;
  135. position: relative;
  136. margin-right: 48px;
  137. height: 40px;
  138. border-radius: 4px;
  139. line-height: 40px;
  140. box-sizing: border-box;
  141. padding: 0 14px;
  142. padding-right: 40px;
  143. color: #000;
  144. font-size: 21px;
  145. cursor: pointer;
  146. text-overflow: ellipsis;
  147. overflow: hidden;
  148. white-space: nowrap;
  149. &::after {
  150. position: absolute;
  151. top: 50%;
  152. right: 14px;
  153. transform: translateY(-50%);
  154. content: '';
  155. display: block;
  156. width: 0;
  157. height: 0;
  158. border-style: solid;
  159. border-width: 10px 7px 0 7px;
  160. border-color: #bababa transparent transparent transparent;
  161. opacity: 0.8;
  162. }
  163. }
  164. }
  165. .option-group {
  166. margin-top: 4px;
  167. position: relative;
  168. width: 100%;
  169. .options {
  170. display: inline-block;
  171. min-width: 160px;
  172. max-width: 400px;
  173. max-height: 240px;
  174. position: absolute;
  175. z-index: 99;
  176. padding-top: 4px;
  177. border-radius: 4px;
  178. box-shadow: 0 8px 8px rgba(0, 0, 0, 0.1);
  179. &.offset-0 {
  180. transform: translateX(0);
  181. }
  182. &.offset-1 {
  183. transform: translateX(200px);
  184. }
  185. &.offset-2 {
  186. transform: translateX(400px);
  187. }
  188. .scroll-box {
  189. max-height: 240px;
  190. overflow-y: scroll;
  191. padding: 8px 0;
  192. background: #fff;
  193. border-radius: 4px;
  194. &::-webkit-scrollbar {
  195. width: 4px;
  196. height: 4px;
  197. background-color: #eee;
  198. }
  199. &::-webkit-scrollbar-thumb {
  200. border-radius: 2px;
  201. background-color: #ccc;
  202. }
  203. }
  204. &::after {
  205. position: absolute;
  206. top: 0;
  207. left: 14px;
  208. transform: translateY(-50%);
  209. content: '';
  210. display: block;
  211. width: 0;
  212. height: 0;
  213. border-style: solid;
  214. border-width: 0 7px 10px 7px;
  215. border-color: transparent transparent white transparent;
  216. }
  217. .option {
  218. font-size: 16px;
  219. background: #fff;
  220. padding: 0 14px;
  221. line-height: 40px;
  222. color: #626262;
  223. box-sizing: border-box;
  224. cursor: pointer;
  225. text-overflow: ellipsis;
  226. overflow: hidden;
  227. white-space: nowrap;
  228. &:hover {
  229. background: #f6f6f6;
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
  236. @media screen and (max-width: 768px) {
  237. .simple-city {
  238. .select-group {
  239. width: 92vw;
  240. margin: 0 auto;
  241. display: flex;
  242. align-items: center;
  243. .select {
  244. position: relative;
  245. height: 8vw;
  246. line-height: 8vw;
  247. box-sizing: border-box;
  248. padding: 0 1.8vw;
  249. padding-right: 6vw;
  250. color: #000;
  251. cursor: pointer;
  252. text-overflow: ellipsis;
  253. overflow: hidden;
  254. white-space: nowrap;
  255. border-radius: 1vw;
  256. font-size: 3.5vw;
  257. margin-right: 8.4vw;
  258. &:last-child {
  259. margin-right: 0;
  260. }
  261. &::after {
  262. position: absolute;
  263. top: 50%;
  264. right: 1.8vw;
  265. transform: translateY(-50%);
  266. content: '';
  267. display: block;
  268. width: 0;
  269. height: 0;
  270. border-style: solid;
  271. border-width: 1.8vw 1.2vw 0 1.2vw;
  272. border-color: #474747 transparent transparent transparent;
  273. opacity: 0.8;
  274. }
  275. }
  276. }
  277. .option-group {
  278. position: relative;
  279. width: 100%;
  280. z-index: 9;
  281. .options {
  282. position: absolute;
  283. top: 1.2vw;
  284. display: inline-block;
  285. min-width: 28vw;
  286. max-width: 60vw;
  287. &.offset-0 {
  288. left: 4vw;
  289. }
  290. &.offset-1 {
  291. transform: translateX(32vw);
  292. }
  293. &.offset-2 {
  294. right: 12vw;
  295. }
  296. .scroll-box {
  297. max-height: 50vw;
  298. overflow-y: scroll;
  299. background: #fff;
  300. border-radius: 1.2vw;
  301. border-radius: 1.2vw;
  302. border: 0.1vw solid #ddd;
  303. &::-webkit-scrollbar {
  304. width: 0.2vw;
  305. height: 0.2vw;
  306. background-color: #eee;
  307. }
  308. &::-webkit-scrollbar-thumb {
  309. border-radius: 0.1vw;
  310. background-color: #ccc;
  311. }
  312. }
  313. .option {
  314. font-size: 3.2vw;
  315. background: #fff;
  316. padding: 0 1.2vw;
  317. line-height: 7vw;
  318. color: #626262;
  319. box-sizing: border-box;
  320. cursor: pointer;
  321. text-overflow: ellipsis;
  322. overflow: hidden;
  323. white-space: nowrap;
  324. }
  325. }
  326. }
  327. }
  328. }
  329. </style>