index.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. <keep-alive>
  18. <div class="option-group">
  19. <div class="options" v-show="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. </div>
  32. </keep-alive>
  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. },
  124. }
  125. </script>
  126. <style scoped lang="scss">
  127. @media screen and (min-width: 768px) {
  128. .simple-city {
  129. .select-group {
  130. .select {
  131. display: inline-block;
  132. position: relative;
  133. margin-right: 48px;
  134. height: 40px;
  135. border-radius: 4px;
  136. line-height: 40px;
  137. box-sizing: border-box;
  138. padding: 0 14px;
  139. padding-right: 40px;
  140. color: #000;
  141. font-size: 21px;
  142. cursor: pointer;
  143. text-overflow: ellipsis;
  144. overflow: hidden;
  145. white-space: nowrap;
  146. &::after {
  147. position: absolute;
  148. top: 50%;
  149. right: 14px;
  150. transform: translateY(-50%);
  151. content: '';
  152. display: block;
  153. width: 0;
  154. height: 0;
  155. border-style: solid;
  156. border-width: 10px 7px 0 7px;
  157. border-color: #bababa transparent transparent transparent;
  158. opacity: 0.8;
  159. }
  160. }
  161. }
  162. .option-group {
  163. margin-top: 4px;
  164. position: relative;
  165. width: 100%;
  166. .options {
  167. display: inline-block;
  168. min-width: 160px;
  169. max-width: 400px;
  170. max-height: 240px;
  171. position: absolute;
  172. z-index: 99;
  173. padding-top: 4px;
  174. border-radius: 4px;
  175. box-shadow: 0 8px 8px rgba(0, 0, 0, 0.1);
  176. &.offset-0 {
  177. transform: translateX(0);
  178. }
  179. &.offset-1 {
  180. transform: translateX(200px);
  181. }
  182. &.offset-2 {
  183. transform: translateX(400px);
  184. }
  185. .scroll-box {
  186. max-height: 240px;
  187. overflow-y: scroll;
  188. padding: 8px 0;
  189. background: #fff;
  190. border-radius: 4px;
  191. &::-webkit-scrollbar {
  192. width: 4px;
  193. height: 4px;
  194. background-color: #eee;
  195. }
  196. &::-webkit-scrollbar-thumb {
  197. border-radius: 2px;
  198. background-color: #ccc;
  199. }
  200. }
  201. &::after {
  202. position: absolute;
  203. top: 0;
  204. left: 14px;
  205. transform: translateY(-50%);
  206. content: '';
  207. display: block;
  208. width: 0;
  209. height: 0;
  210. border-style: solid;
  211. border-width: 0 7px 10px 7px;
  212. border-color: transparent transparent white transparent;
  213. }
  214. .option {
  215. font-size: 16px;
  216. background: #fff;
  217. padding: 0 14px;
  218. line-height: 40px;
  219. color: #626262;
  220. box-sizing: border-box;
  221. cursor: pointer;
  222. text-overflow: ellipsis;
  223. overflow: hidden;
  224. white-space: nowrap;
  225. &:hover {
  226. background: #f6f6f6;
  227. }
  228. }
  229. }
  230. }
  231. }
  232. }
  233. @media screen and (max-width: 768px) {
  234. .simple-city {
  235. .select-group {
  236. width: 92vw;
  237. margin: 0 auto;
  238. display: flex;
  239. align-items: center;
  240. .select {
  241. position: relative;
  242. height: 8vw;
  243. line-height: 8vw;
  244. box-sizing: border-box;
  245. padding: 0 1.8vw;
  246. padding-right: 6vw;
  247. color: #000;
  248. cursor: pointer;
  249. text-overflow: ellipsis;
  250. overflow: hidden;
  251. white-space: nowrap;
  252. border-radius: 1vw;
  253. font-size: 3.5vw;
  254. margin-right: 8.4vw;
  255. &:last-child {
  256. margin-right: 0;
  257. }
  258. &::after {
  259. position: absolute;
  260. top: 50%;
  261. right: 1.8vw;
  262. transform: translateY(-50%);
  263. content: '';
  264. display: block;
  265. width: 0;
  266. height: 0;
  267. border-style: solid;
  268. border-width: 1.8vw 1.2vw 0 1.2vw;
  269. border-color: #474747 transparent transparent transparent;
  270. opacity: 0.8;
  271. }
  272. }
  273. }
  274. .option-group {
  275. position: relative;
  276. width: 100%;
  277. z-index: 9;
  278. .options {
  279. position: absolute;
  280. top: 1.2vw;
  281. display: inline-block;
  282. min-width: 28vw;
  283. max-width: 60vw;
  284. &.offset-0 {
  285. left: 4vw;
  286. }
  287. &.offset-1 {
  288. transform: translateX(36vw);
  289. }
  290. &.offset-2 {
  291. right: 4vw;
  292. }
  293. .scroll-box {
  294. max-height: 50vw;
  295. overflow-y: scroll;
  296. background: #fff;
  297. border-radius: 1.2vw;
  298. border-radius: 1.2vw;
  299. border: 0.1vw solid #ddd;
  300. &::-webkit-scrollbar {
  301. width: 0.2vw;
  302. height: 0.2vw;
  303. background-color: #eee;
  304. }
  305. &::-webkit-scrollbar-thumb {
  306. border-radius: 0.1vw;
  307. background-color: #ccc;
  308. }
  309. }
  310. .option {
  311. font-size: 3.2vw;
  312. background: #fff;
  313. padding: 0 1.2vw;
  314. line-height: 7vw;
  315. color: #626262;
  316. box-sizing: border-box;
  317. cursor: pointer;
  318. text-overflow: ellipsis;
  319. overflow: hidden;
  320. white-space: nowrap;
  321. }
  322. }
  323. }
  324. }
  325. }
  326. </style>