index.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. padding-top: 4px;
  173. border-radius: 4px;
  174. box-shadow: 0 8px 8px rgba(0, 0, 0, 0.1);
  175. &.offset-0 {
  176. transform: translateX(0);
  177. }
  178. &.offset-1 {
  179. transform: translateX(200px);
  180. }
  181. &.offset-2 {
  182. transform: translateX(400px);
  183. }
  184. .scroll-box {
  185. max-height: 240px;
  186. overflow-y: scroll;
  187. padding: 8px 0;
  188. background: #fff;
  189. border-radius: 4px;
  190. &::-webkit-scrollbar {
  191. width: 4px;
  192. height: 4px;
  193. background-color: #eee;
  194. }
  195. &::-webkit-scrollbar-thumb {
  196. border-radius: 2px;
  197. background-color: #ccc;
  198. }
  199. }
  200. &::after {
  201. position: absolute;
  202. top: 0;
  203. left: 14px;
  204. transform: translateY(-50%);
  205. content: '';
  206. display: block;
  207. width: 0;
  208. height: 0;
  209. border-style: solid;
  210. border-width: 0 7px 10px 7px;
  211. border-color: transparent transparent white transparent;
  212. }
  213. .option {
  214. font-size: 16px;
  215. background: #fff;
  216. padding: 0 14px;
  217. line-height: 40px;
  218. color: #626262;
  219. box-sizing: border-box;
  220. cursor: pointer;
  221. text-overflow: ellipsis;
  222. overflow: hidden;
  223. white-space: nowrap;
  224. &:hover {
  225. background: #f6f6f6;
  226. }
  227. }
  228. }
  229. }
  230. }
  231. }
  232. @media screen and (max-width: 768px) {
  233. .simple-city {
  234. .select-group {
  235. width: 92vw;
  236. margin: 0 auto;
  237. display: flex;
  238. align-items: center;
  239. .select {
  240. position: relative;
  241. height: 8vw;
  242. line-height: 8vw;
  243. box-sizing: border-box;
  244. padding: 0 1.8vw;
  245. padding-right: 6vw;
  246. color: #000;
  247. cursor: pointer;
  248. text-overflow: ellipsis;
  249. overflow: hidden;
  250. white-space: nowrap;
  251. border-radius: 1vw;
  252. font-size: 3.5vw;
  253. margin-right: 8.4vw;
  254. &:last-child {
  255. margin-right: 0;
  256. }
  257. &::after {
  258. position: absolute;
  259. top: 50%;
  260. right: 1.8vw;
  261. transform: translateY(-50%);
  262. content: '';
  263. display: block;
  264. width: 0;
  265. height: 0;
  266. border-style: solid;
  267. border-width: 1.8vw 1.2vw 0 1.2vw;
  268. border-color: #474747 transparent transparent transparent;
  269. opacity: 0.8;
  270. }
  271. }
  272. }
  273. .option-group {
  274. position: relative;
  275. width: 100%;
  276. .options {
  277. position: absolute;
  278. top: 1.2vw;
  279. display: inline-block;
  280. min-width: 28vw;
  281. max-width: 60vw;
  282. &.offset-0 {
  283. left: 4vw;
  284. }
  285. &.offset-1 {
  286. transform: translateX(36vw);
  287. }
  288. &.offset-2 {
  289. right: 4vw;
  290. }
  291. .scroll-box {
  292. max-height: 50vw;
  293. overflow-y: scroll;
  294. background: #fff;
  295. border-radius: 1.2vw;
  296. border-radius: 1.2vw;
  297. border: 0.1vw solid #ddd;
  298. &::-webkit-scrollbar {
  299. width: 0.2vw;
  300. height: 0.2vw;
  301. background-color: #eee;
  302. }
  303. &::-webkit-scrollbar-thumb {
  304. border-radius: 0.1vw;
  305. background-color: #ccc;
  306. }
  307. }
  308. .option {
  309. font-size: 3.2vw;
  310. background: #fff;
  311. padding: 0 1.2vw;
  312. line-height: 7vw;
  313. color: #626262;
  314. box-sizing: border-box;
  315. cursor: pointer;
  316. text-overflow: ellipsis;
  317. overflow: hidden;
  318. white-space: nowrap;
  319. }
  320. }
  321. }
  322. }
  323. }
  324. </style>