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

Vue2.0 實現(xiàn)歌手列表滾動及右側(cè)快速入口功能

 更新時間:2018年08月08日 11:24:33   作者:Nian糕  
這篇文章主要介紹了Vue2.0實現(xiàn)歌手列表滾動及右側(cè)快速入口功能,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

1 歌手列表

歌手列表頁類似于手機通訊錄,我們也將其作為一個基礎(chǔ)組件獨立出來,這部分的邏輯比較簡單,這里不做過多的講解

// base/listview/listview.vue
<template>
  <scroll class="listview" :data="data">
    <ul>
      <li v-for="(group, index) in data" :key="index" class="list-group">
        <h2 class="list-group-title">{{group.title}}</h2>
        <uL>
          <li v-for="(item, index) in group.items" :key="index" class="list-group-item">
            <img class="avatar" v-lazy="item.avatar">
            <span class="name">{{item.name}}</span>
          </li>
        </uL>
      </li>
    </ul>
  </scroll>
</template>
<script type="text/ecmascript-6">
  import Scroll from 'base/scroll/scroll'
  export default {
    props: {
      data: {
        type: Array,
        default: () => []
      }
    },
    components: {
      Scroll
    }
  }
</script>
<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable"
  .listview
    position: relative
    width: 100%
    height: 100%
    overflow: hidden
    background: $color-background
    .list-group
      padding-bottom: 30px
      .list-group-title
        height: 30px
        line-height: 30px
        padding-left: 20px
        font-size: $font-size-small
        color: $color-text-l
        background: $color-highlight-background
      .list-group-item
        display: flex
        align-items: center
        padding: 20px 0 0 30px
        .avatar
          width: 50px
          height: 50px
          border-radius: 50%
        .name
          margin-left: 20px
          color: $color-text-l
          font-size: $font-size-medium
    .list-shortcut
      position: absolute
      z-index: 30
      right: 0
      top: 50%
      transform: translateY(-50%)
      width: 20px
      padding: 20px 0
      border-radius: 10px
      text-align: center
      background: $color-background-d
      font-family: Helvetica
      .item
        padding: 3px
        line-height: 1
        color: $color-text-l
        font-size: $font-size-small
        &.current
          color: $color-theme
          font-weight: bolder
    .list-fixed
      position: absolute
      top: -1px
      left: 0
      width: 100%
      .fixed-title
        height: 30px
        line-height: 30px
        padding-left: 20px
        font-size: $font-size-small
        color: $color-text-l
        background: $color-highlight-background
    .loading-container
      position: absolute
      width: 100%
      top: 50%
      transform: translateY(-50%)
</style>
// singer.vue
<template>
 <div class="singer">
  <list-view :data="singerList"></list-view>
 </div>
</template>
<script type="text/ecmascript-6">
 import ListView from 'base/listview/listview'
 export default {
  ...
  components: {
   ListView
  }
 }
</script>

 

運行結(jié)果

2 右側(cè)快速入口_點擊滾動

同樣是類比于手機通訊錄,懸浮于屏幕右側(cè)的 A-Z 可以幫助我們快速找到對應(yīng)的歌手,為此,我們需要獲取 title 的集合數(shù)組

// listview.vue
<div class="list-shortcut">
  <ul>
    <li v-for="(item, index) in shortcutList" :key="index" class="item">{{item}}</li>
  </ul>
</div>
<script type="text/ecmascript-6">
  export default {
    ...
    computed: {
      shortcutList() {
        return this.data.map((group) => {
          return group.title.substr(0, 1)
        })
      }
    }
  }
</script>

 

運行結(jié)果

快速入口出現(xiàn)了之后,我們接下來就為其添加點擊事件,當我們點擊對應(yīng)字母時,需要獲取其索引,這里我們直接獲取 v-for 提供的 index 即可

// listview.vue
<ul>
  <li v-for="(item, index) in shortcutList" :key="index" @touchstart="onShortcutTouchStart($even, index)" class="item">{{item}}</li>
</ul>
export default {
  ...
  methods: {
    onShortcutTouchStart(e, index) {
      console.log(index)
    }
  }
}

點擊之后,我們需要頁面滾動到相應(yīng)位置,這里需要擴展 scroll 組件的方法,這里擴展的方法都是來自 better-scroll 組件所封裝的方法,這里提一下 scrollToElement 方法的第二個參數(shù)是動畫時間,可根據(jù)自身需求進行設(shè)置

// scroll.vue
methods: {
 ...
 scrollTo() {
  this.scroll && this.scroll.scrollTo.apply(this.scroll, arguments)
 },
 scrollToElement() {
  this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments)
 }
}

隨后給 scroll 組件添加 ref="listview" 以及歌手列表添加 ref="listGroup" 方便我們調(diào)用

// listview.vue
export default {
  ...
  methods: {
    onShortcutTouchStart(e, index) {
      this.$refs.listview.scrollToElement(this.$refs.listGroup[index], 0)
    }
  }
}

 

運行結(jié)果

3 右側(cè)快速入口_滑動滾動

當我們的手指在右側(cè)快速入口上滑動時,歌手列表也會同步進行滾動,當我們滾動右側(cè)快速入口時,我們需要阻止歌手列表滾動,以及瀏覽器原生滾動,所以要使用 @touchmove.stop.prevent 阻止冒泡,并且在 onShortcutTouchStart 事件中記錄觸碰點的初始位置,以及 onShortcutTouchMove 事件中觸碰點的位置,通過兩個位置的像素差,來滾動歌手列表

// listview.vue
<div class="list-shortcut" @touchmove.stop.prevent="onShortcutTouchMove">
  <ul>
    <li v-for="(item, index) in shortcutList" :key="index" @touchstart="onShortcutTouchStart($event, index)" class="item">{{item}}</li>
  </ul>
</div>
<script type="text/ecmascript-6">
  const ANCHOR_HEIGHT = 18
  export default {
    created() {
      this.touch = {}
    },
    ...
    methods: {
      onShortcutTouchStart(e, index) {
        let firstTouch = e.touches[0]
        this.touch.y1 = firstTouch.pageY
        this.touch.anchorIndex = index
        this._scrollTo(index)
      },
      onShortcutTouchMove(e) {
        let firstTouch = e.touches[0]
        this.touch.y2 = firstTouch.pageY
        let delta = (this.touch.y2 - this.touch.y1) / ANCHOR_HEIGHT | 0
        let anchorIndex = this.touch.anchorIndex + delta
        this._scrollTo(anchorIndex)
      },
      _scrollTo(index) {
        this.$refs.listview.scrollToElement(this.$refs.listGroup[index], 0)
      }
    },
    components: {
      Scroll
    }
  }
</script>

 

運行結(jié)果

4 右側(cè)快速入口_高亮設(shè)置

當歌手列表滾動時,我們想要在右側(cè)快速入口中,高亮當前顯示的 title ,這就需要我們監(jiān)聽 scroll 組件的滾動事件,來獲取當前滾動的位置

// scroll.vue
<script type="text/ecmascript-6">
 export default {
  props: {
   ...
   listenScroll: {
    type: Boolean,
    default: false
   }
  },
  methods: {
   _initScroll() {
    ...
    if (this.listenScroll) {
     let me = this
     this.scroll.on('scroll', (pos) => {
      me.$emit('scroll', pos)
     })
    }
   }
  }
 }
</script>

我們當初給參數(shù) probeType 設(shè)的默認值為 1,即會非實時(屏幕滑動超過一定時間后)派發(fā) scroll 事件,我們在屏幕滑動的過程中,需要實時派發(fā) scroll 事件,所以在 listview 中將 probeType 的值設(shè)為 3

// listview.vue
<template>
    <scroll class="listview"
            :data="data"
            ref="listview"
            :probe-type="probeType"
            :listenScroll="listenScroll"
            @scroll="scroll">
        <ul>
            ...
        </ul>
        <div class="list-shortcut" @touchmove.stop.prevent="onShortcutTouchMove">
            <ul>
                <li v-for="(item, index) in shortcutList"
                    :key="index"
                    :class="{'current':currentIndex===index}"
                    @touchstart="onShortcutTouchStart($event, index)"
                    class="item">{{item}}</li>
            </ul>
        </div>
    </scroll>
</template>
<script type="text/ecmascript-6">
    export default {
        created() {
            ...
            this.listHeight = []
            this.probeType = 3
        },
        data() {
            return {
                scrollY: -1,
                currentIndex: 0
            }
        },
        methods: {
            ...
            scroll(pos) {
                this.scrollY = pos.y
            },
            _scrollTo(index) {
                this.scrollY = -this.listHeight[index]
                this.$refs.listview.scrollToElement(this.$refs.listGroup[index], 0)
            },
            _calculateHeight() {
                this.listHeight = []
                const list = this.$refs.listGroup
                let height = 0
                this.listHeight.push(height)
                for (let i = 0; i < list.length; i++) {
                    let item = list[i]
                    height += item.clientHeight
                    this.listHeight.push(height)
                }
            }
        },
        watch: {
            data() {
                this.$nextTick(() => {
                    this._calculateHeight()
                })
            },
             scrollY(newY) {
                const listHeight = this.listHeight
                // 當滾動到頂部,newY>0
                if (newY > 0) {
                    this.currentIndex = 0
                    return
                }
                // 在中間部分滾動
                for (let i = 0; i < listHeight.length - 1; i++) {
                    let height1 = listHeight[i]
                    let height2 = listHeight[i + 1]
                    if (-newY >= height1 && -newY < height2) {
                        this.currentIndex = i
                        return
                    }
                }
                // 當滾動到底部,且-newY大于最后一個元素的上限
                this.currentIndex = listHeight.length - 2
            }
        },
        components: {
            Scroll
        }
    }
</script>

 

運行結(jié)果

5 滾動固定標題

當我們滾動歌手列表頁時,希望該歌手的 title 一直顯示在頂部,并且滾動到下一個 title 時,新的 title 將舊的 title 頂替掉,這里就需要我們計算一個 title 的高度

// listview.vue
<template>
    <scroll class="listview"
            :data="data"
            ref="listview"
            :probe-type="probeType"
            :listenScroll="listenScroll"
            @scroll="scroll">
        ...
        <div class="list-fixed" ref="fixed" v-show="fixedTitle">
            <div class="fixed-title">{{fixedTitle}}</div>
        </div>
    </scroll>
</template>
<script type="text/ecmascript-6">
    import Scroll from 'base/scroll/scroll'
    const TITLE_HEIGHT = 28
    const ANCHOR_HEIGHT = 18
    export default {
        ...
        data() {
            return {
                scrollY: -1,
                currentIndex: 0,
                diff: -1
            }
        },
        computed: {
            ...
            fixedTitle() {
                if (this.scrollY > 0) {
                    return ''
                }
                return this.data[this.currentIndex] ? this.data[this.currentIndex].title : ''
            }
        },
        watch: {
            ...
            scrollY(newY) {
                ...
                for (let i = 0; i < listHeight.length - 1; i++) {
                    ...
                    if (-newY >= height1 && -newY < height2) {
                        ...
                        this.diff = height2 + newY
                        return
                    }
                }
                ...
            },
            diff(newVal) {
                let fixedTop = (newVal > 0 && newVal < TITLE_HEIGHT) ? newVal - TITLE_HEIGHT : 0
                if (this.fixedTop === fixedTop) {
                    return
                }
                this.fixedTop = fixedTop
                this.$refs.fixed.style.transform = `translate3d(0,${fixedTop}px,0)`
            }
        }
    }
</script>

 

運行結(jié)果

該章節(jié)的內(nèi)容到這里就全部結(jié)束了,源碼我已經(jīng)發(fā)到了 GitHub Vue_Music_06 上了,有需要的同學(xué)可自行下載

總結(jié)

以上所述是小編給大家介紹的Vue2.0 實現(xiàn)歌手列表滾動及右側(cè)快速入口功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Vue.js實現(xiàn)文件上傳壓縮優(yōu)化處理技巧

    Vue.js實現(xiàn)文件上傳壓縮優(yōu)化處理技巧

    這篇文章主要介紹了Vue.js實現(xiàn)文件上傳壓縮優(yōu)化處理,本文給大家介紹兩種方法一種是借助canvas的封裝的文件壓縮上傳,二是使用compressorjs第三方插件實現(xiàn),本文給大家介紹的非常詳細需要的朋友可以參考下
    2022-11-11
  • vue隱藏路由的實現(xiàn)方法

    vue隱藏路由的實現(xiàn)方法

    這篇文章主要介紹了vue隱藏路由的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • vue中{__ob__: observer}對象轉(zhuǎn)化為數(shù)組進行遍歷方式

    vue中{__ob__: observer}對象轉(zhuǎn)化為數(shù)組進行遍歷方式

    這篇文章主要介紹了vue中{__ob__: observer}對象轉(zhuǎn)化為數(shù)組進行遍歷方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue中關(guān)于click.stop的用法

    vue中關(guān)于click.stop的用法

    這篇文章主要介紹了vue中關(guān)于click.stop的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • vue監(jiān)視器@Watch創(chuàng)建執(zhí)行immediate方式

    vue監(jiān)視器@Watch創(chuàng)建執(zhí)行immediate方式

    這篇文章主要介紹了vue監(jiān)視器@Watch創(chuàng)建執(zhí)行immediate方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 對vue事件的延遲執(zhí)行實例講解

    對vue事件的延遲執(zhí)行實例講解

    今天小編就為大家分享一篇對vue事件的延遲執(zhí)行實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • html2canvas使用文檔(vue舉例)

    html2canvas使用文檔(vue舉例)

    html2canvas.js是一款可以在網(wǎng)頁上實現(xiàn)頁面截圖的js,它使用了html5和css3的一些新功能特性,實現(xiàn)了在客戶端對網(wǎng)頁進行截圖的功能,這篇文章主要給大家介紹了關(guān)于html2canvas使用的相關(guān)資料,需要的朋友可以參考下
    2024-03-03
  • Vue方法與事件處理器詳解

    Vue方法與事件處理器詳解

    這篇文章主要為大家詳細介紹了Vue方法與事件處理器,,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • vue 系列——vue2-webpack2框架搭建踩坑之路

    vue 系列——vue2-webpack2框架搭建踩坑之路

    本文從零搭建vue項目,給大家分享了我的vue2-webpack2框架搭建踩坑之路,需要的朋友可以參考下
    2017-12-12
  • Element如何實現(xiàn)loading的方法示例

    Element如何實現(xiàn)loading的方法示例

    本文主要介紹了Element如何實現(xiàn)loading的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06

最新評論