欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS基于VUE組件實現(xiàn)城市列表效果

 更新時間:2021年08月20日 11:19:54   作者:駿骨銀蹄  
這篇文章主要為大家詳細介紹了JS基于VUE組件實現(xiàn)城市列表效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了基于VUE組件實現(xiàn)城市列表效果的具體代碼,供大家參考,具體內(nèi)容如下

  • 基本思想是,將城市列表數(shù)據(jù)緩存在本地
  • 然后在頁面上用JS實現(xiàn)即時模糊查詢和首字母定位查詢等
  • 為了保護項目,刪除了部分代碼

效果

實現(xiàn)

H5:

<template>
    <div id="city">
        <div class="search-city-back">
            <div class="search-city">
                <img src="../assets/img/Shop/search.png">
                <input type="text" placeholder="請輸入城市名稱" v-model="citySearch">
                <a @click="citySearch=''" href="javascript:;" class="img-del" v-show="citySearch"></a>
            </div>
        </div>
        <div class="city-content">
            <div id="showCityContent"></div>
            <div class="letter-item" v-if="showCity&&sourcePageType===1">
                <div></div>
                <div @click="cityChoose('*','全國')">全國</div>
            </div>
            <div v-for="(val,key) in showCity" class="letter-item">
                <div><a :id="key">{{key}}</a></div>
                <div v-for="i in val">
                    <div :class="{'city-hide': i.Code==='*'&&sourcePageType===3}" class="item-buttom"
                         @click="cityChoose(i.Code,i.Name)">{{i.Name}}


                    </div>
                </div>
            </div>
        </div>
        <aside class="letter-aside" :style="{color: config.letterColor}" v-if="showCity">
            <ul>
                <!--<li>定位</li>-->
                <!--<li>熱門</li>-->
                <li v-for="(val,key) in showCity" @click="naver(key)">{{key}} </li>
            </ul>
        </aside>
        <div id="tip">{{tipString}}</div>
    </div>
</template>

JS:

<script>
    import {GetCityList} from 'service'
    import {setTitle, setSessionStore, getSessionStore} from '../utils/method'

    export default{
        name: 'CityList',
        data () {
            return {
                citysAllSSKey: 'XMall-Component-AllCityList', // 所有城市的會話緩存
                citys: [],
                showCity: null,
                tipString: null,
                letter: null,
                citySearch: '',
                sourcePageType: 1
            }
        },
        props: {
            config: {
                type: Object,
                default: () => {
                    return {
                        letterColor: '#f44f0f',
                    }
                }
            },
            pageType: {
                type: Number,
                default: 1 // 1:全國城市列表 
            },
            shopId: {
                type: String, 
                default: null
            }
        },
        watch: {
            citySearch: function () {
                this.cityFilter()
            }
        },
        created: function () {
            setTitle('選擇城市')
        },
        mounted: function () {
            this.into()
        },
        methods: {
            into(){
                this.sourcePageType = parseInt(this.$props.pageType)
                if (this.sourcePageType === 1) {
                    this.citys = JSON.parse(getSessionStore(this.citysAllSSKey))
                    if (this.citys) {
                        this.showCity = this.citys
                    } else {
                        this.getAllCityList()
                    }
                }
            },
            // 獲取全國城市列表
            getAllCityList: function () {
                // 顯示loading
                this.$vux.loading.show({text: '加載中'})
                GetCityList(
                    {
                        keyword: ''
                    },
                    (res) => {
                        this.citys = res
                        this.showCity = res
                        // 隱藏loading
                        this.$vux.loading.hide()
                        setSessionStore(this.citysAllSSKey, res)
                    }, (err) => {
                        console.log(err)
                        // 隱藏loading
                        this.$vux.loading.hide()
                    })
            },
            // 側邊字母定位滾動到相應的位置
            naver: function (letter) {
                this.tipString = letter
                let obj = document.getElementById(letter)
                let tip = document.getElementById('tip')
                tip.setAttribute('class', 'tipAppear')
                setTimeout(function () {
                    tip.removeAttribute('class')
                }, 500)
                let oPos = obj.offsetTop
                return window.scrollTo(0, oPos - 36)
            },
            // 城市搜索
            cityFilter: function () {
                let nodata = true
                if (this.citySearch) {
                    // 遍歷對象,選出符合條件的值
                    let showCityNew = {}
                    for (let key in this.citys) {
                        let arrayNew = []
                        for (let value of this.citys[key]) {
                            if (value.Name.indexOf(this.citySearch) > -1) {
                                arrayNew.push(value)
                            }
                        }
                        if (arrayNew.length > 0) {
                            showCityNew[key] = arrayNew
                            nodata = false
                        }
                    }
                    this.showCity = showCityNew
                } else {
                    this.showCity = this.citys
                    nodata = false
                }
                if (nodata) {
                    this.showCity = null
                    let _showCityContent = document.getElementById('showCityContent')
                    _showCityContent.innerText = '查詢不到結果'
                    _showCityContent.setAttribute('class', 'tipShow')
                } else {
                    document.getElementById('showCityContent').innerText = ''
                }
            },
            // 城市選擇
            cityChoose: function (code, name) {
                this.$emit('chooseCity', {Code: code, Name: name})
            }
        }
    }
</script>

CSS:

<style lang="scss" scoped>
  #city {
    position: relative;
    background: #f6f4f5;
  }
  #city{
    .city-content {
      padding: 60px 0 0 0;
    }

    .letter-item{
      background-color: #fff;
    }

    .letter-item > div:first-child {
      color: #999;
      background: #f6f4f5;
      margin-right: 30px;
    }

    .letter-item > div {
      color: #333;
      line-height: 45px;
      font-size: 14px;
      padding: 0 30px 0 15px;
      background-color: #fff;
    }

    .letter-item .item-buttom {
      border-bottom: 1px solid #e6e6e6;
    }

    .letter-aside {
      position: fixed;
      right: 5px;
      top: 5.3rem;
    }

    .letter-aside ul {
      list-style: none;
      line-height: 1.4em;
      font-size: 14px;
      text-align: center;
    }

    #tip {
      position: fixed;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      margin: auto;
      border: 1px solid #333333;
      width: 100px;
      height: 100px;
      z-index: 10;
      text-align: center;
      line-height: 100px;
      font-size: 50px;
      opacity: 0;
    }

    .tipAppear {
      animation: appearTip 1 linear 0.5s;
    }

    @-webkit-keyframes appearTip {
      0% {
        opacity: 1;
      }
      80% {
        opacity: 0.5;
      }
      100% {
        opacity: 0;
      }
    }

    @keyframes appearTip {
      0% {
        opacity: 1;
      }
      80% {
        opacity: 0.5;
      }
      100% {
        opacity: 0;
      }
    }

    .search-city-back {
      width: 100%;
      position: fixed;
      background-color: #f6f4f5;
      max-width: 414px;
    }

    .search-city {
      height: 30px;
      line-height: 30px;
      padding: 0 15px;
      border-radius: 14px;
      margin: 12px 15px;
      background-color: #ffffff;
    }

    .search-city img {
      height: 18px;
      width: 18px;
    }

    .search-city input {
      width: 80%;
      margin-left: 5px;
      line-height: 24px;
      border-radius: 5px;
      outline: none;
      font-size: 15px;
    }

    .tipShow {
      text-align: center;
      line-height: 60px;
      font-size: 16px;
      color: #bbbbbb;
    }

    .city-hide {
      display: none;
    }

    .img-del {
      width: 16px;
      height: 16px;
      position: absolute;
      top: 19px;
      right: 30px;
      background-color: rgba(0, 0, 0, .2);
      border-radius: 8px;
    }

    .img-del::before, .img-del::after {
      content: ' ';
      width: 0;
      height: 50%;
      position: absolute;
      top: 4px;
      right: 7px;
      border-right: 1px solid #fff;
    }

    .img-del::before {
      transform: rotate(45deg);
    }

    .img-del::after {
      transform: rotate(-45deg);
    }
  }

</style>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 超強推薦的js編程中的簡潔寫法收集

    超強推薦的js編程中的簡潔寫法收集

    超強推薦的js編程中的簡潔寫法收集...
    2007-08-08
  • JS出現(xiàn)404錯誤原理及解決方案

    JS出現(xiàn)404錯誤原理及解決方案

    這篇文章主要介紹了JS出現(xiàn)404錯誤原理及解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • uniApp實現(xiàn)選擇時間功能

    uniApp實現(xiàn)選擇時間功能

    這篇文章主要介紹了uniApp實現(xiàn)選擇時間功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-03-03
  • ECharts數(shù)據(jù)可視化基本使用之常用圖表類型

    ECharts數(shù)據(jù)可視化基本使用之常用圖表類型

    這篇文章主要給大家介紹了關于ECharts數(shù)據(jù)可視化基本使用之常用圖表類型的相關資料,echarts是一款基于JavaScript的數(shù)據(jù)可視化圖表庫,提供直觀,生動,可交互,可個性化定制的數(shù)據(jù)可視化圖表,需要的朋友可以參考下
    2023-11-11
  • element中el-switch的v-model自定義值的實現(xiàn)

    element中el-switch的v-model自定義值的實現(xiàn)

    在el-switch中設置active-value和inactive-value屬性,接受Boolean, String或Number類型的值,本文就來介紹一下element中el-switch的v-model自定義值的實現(xiàn),感興趣的可以了解一下
    2023-11-11
  • js里取容器大小、定位、距離等屬性搜集整理

    js里取容器大小、定位、距離等屬性搜集整理

    取容器大小、定位、距離等在實際中使用的比較廣泛,下面與大家分享下小編整理的js相關代碼,有此需求的朋友可以參考下,希望對大家有所幫助
    2013-08-08
  • 創(chuàng)建基于Bootstrap的下拉菜單的DropDownList的JQuery插件

    創(chuàng)建基于Bootstrap的下拉菜單的DropDownList的JQuery插件

    這篇文章主要介紹了創(chuàng)建基于Bootstrap的下拉菜單的DropDownList的JQuery插件 的相關資料,需要的朋友可以參考下
    2016-06-06
  • apply和call方法定義及apply和call方法的區(qū)別

    apply和call方法定義及apply和call方法的區(qū)別

    apply和call功能一樣,只是傳入的參數(shù)列表形式不同,本文給大家介紹apply和call方法定義及apply和call方法的區(qū)別,感興趣的朋友一起學習吧
    2015-11-11
  • JS 跳轉頁面延遲2種方法

    JS 跳轉頁面延遲2種方法

    js跳轉頁面想必大家不會很陌生吧,在瀏覽頁面的時候經(jīng)常會碰到這種現(xiàn)象存在如果可以延遲的話是不是會更委婉一點呢,接下來為大家介紹下如何實現(xiàn),感興趣的朋友可以參考下哈
    2013-03-03
  • TypeScript學習之強制類型的轉換

    TypeScript學習之強制類型的轉換

    眾所周知TypeScript是一種由微軟開發(fā)的自由和開源的編程語言。它是JavaScript的一個超集,而且本質(zhì)上向這個語言添加了可選的靜態(tài)類型和基于類的面向?qū)ο缶幊?,下面這篇文章主要介紹了TypeScript中強制類型的轉換,需要的朋友可以參考借鑒下。
    2016-12-12

最新評論