index.vue 8.0 KB

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