index.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. width: 560px;
  130. .select-group {
  131. width: 100%;
  132. display: flex;
  133. justify-content: space-between;
  134. align-items: center;
  135. .select {
  136. position: relative;
  137. width: 160px;
  138. height: 40px;
  139. background: rgba(255, 255, 255, 0.39);
  140. border-radius: 4px;
  141. line-height: 40px;
  142. box-sizing: border-box;
  143. padding: 0 14px;
  144. padding-right: 40px;
  145. color: #fff;
  146. cursor: pointer;
  147. text-overflow: ellipsis;
  148. overflow: hidden;
  149. white-space: nowrap;
  150. &::after {
  151. position: absolute;
  152. top: 50%;
  153. right: 14px;
  154. transform: translateY(-50%);
  155. content: '';
  156. display: block;
  157. width: 0;
  158. height: 0;
  159. border-style: solid;
  160. border-width: 10px 7px 0 7px;
  161. border-color: white transparent transparent transparent;
  162. opacity: 0.8;
  163. }
  164. }
  165. }
  166. .option-group {
  167. margin-top: 4px;
  168. position: relative;
  169. width: 100%;
  170. .options {
  171. display: inline-block;
  172. min-width: 160px;
  173. max-width: 400px;
  174. max-height: 240px;
  175. position: relative;
  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. justify-content: space-between;
  243. align-items: center;
  244. .select {
  245. position: relative;
  246. width: 28vw;
  247. height: 8vw;
  248. background: rgba(255, 255, 255, 0.39);
  249. line-height: 8vw;
  250. box-sizing: border-box;
  251. padding: 0 1.8vw;
  252. padding-right: 6vw;
  253. color: #000;
  254. cursor: pointer;
  255. text-overflow: ellipsis;
  256. overflow: hidden;
  257. white-space: nowrap;
  258. border: 0.1vw solid #ddd;
  259. border-radius: 1vw;
  260. font-size: 3.2vw;
  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. .options {
  281. position: absolute;
  282. top: 1.2vw;
  283. display: inline-block;
  284. min-width: 28vw;
  285. max-width: 60vw;
  286. &.offset-0 {
  287. left: 4vw;
  288. }
  289. &.offset-1 {
  290. transform: translateX(36vw);
  291. }
  292. &.offset-2 {
  293. right: 4vw;
  294. }
  295. .scroll-box {
  296. max-height: 50vw;
  297. overflow-y: scroll;
  298. background: #fff;
  299. border-radius: 1.2vw;
  300. border-radius: 1.2vw;
  301. border: 0.1vw solid #ddd;
  302. &::-webkit-scrollbar {
  303. width: 0.2vw;
  304. height: 0.2vw;
  305. background-color: #eee;
  306. }
  307. &::-webkit-scrollbar-thumb {
  308. border-radius: 0.1vw;
  309. background-color: #ccc;
  310. }
  311. }
  312. .option {
  313. font-size: 3.2vw;
  314. background: #fff;
  315. padding: 0 1.2vw;
  316. line-height: 7vw;
  317. color: #626262;
  318. box-sizing: border-box;
  319. cursor: pointer;
  320. text-overflow: ellipsis;
  321. overflow: hidden;
  322. white-space: nowrap;
  323. }
  324. }
  325. }
  326. }
  327. }
  328. </style>