index.vue 7.9 KB

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