1. 程式人生 > 其它 >搜尋框實時搜尋效果

搜尋框實時搜尋效果

<template>
  <div class="search-input">
    <i class="icon-search"></i>
    <input
      class="input-inner"
      v-model="query"
      :placeholder="placeholder"
    />
    <i
      v-show="query"
      class="icon-dismiss"
      @click="clear"
    ></i>
  </div>
</template>

<script>
  import { debounce } 
from 'throttle-debounce' export default { name: 'search-input', props: { modelValue: String, placeholder: { type: String, default: '搜尋歌曲、歌手' } }, data() { return { query: this.modelValue } }, created() { this.$watch('
query', debounce(300, (newQuery) => { this.$emit('update:modelValue', newQuery.trim()) })) this.$watch('modelValue', (newVal) => { this.query = newVal }) }, methods: { clear() { this.query = '' } } } </script> <style lang="
scss" scoped> .search-input { display: flex; align-items: center; box-sizing: border-box; width: 100%; padding: 0 6px; height: 32px; background: $color-highlight-background; border-radius: 6px; .icon-search { font-size: 24px; color: $color-text-d; } .input-inner { flex: 1; margin: 0 5px; line-height: 18px; background: $color-highlight-background; color: $color-text; font-size: $font-size-medium; outline: 0; &::placeholder { color: $color-text-d; } } .icon-dismiss { font-size: 16px; color: $color-text-d; } } </style>
<template>
  <div class="search-input">
    <i class="icon-search"></i>
    <input
      class="input-inner"
      v-model="query"
      :placeholder="placeholder"
    />
    <i
      v-show="query"
      class="icon-dismiss"
      @click="clear"
    ></i>
  </div>
</template>

<script>
  import { debounce } from 'throttle-debounce'

  export default {
    name: 'search-input',
    props: {
      modelValue: String,
      placeholder: {
        type: String,
        default: '搜尋歌曲、歌手'
      }
    },
    data() {
      return {
        query: this.modelValue
      }
    },
    created() {
      this.$watch('query', debounce(300, (newQuery) => {
        this.$emit('update:modelValue', newQuery.trim())
      }))

      this.$watch('modelValue', (newVal) => {
        this.query = newVal
      })
    },
    methods: {
      clear() {
        this.query = ''
      }
    }
  }
</script>

<style lang="scss" scoped>
  .search-input {
    display: flex;
    align-items: center;
    box-sizing: border-box;
    width: 100%;
    padding: 0 6px;
    height: 32px;
    background: $color-highlight-background;
    border-radius: 6px;
    .icon-search {
      font-size: 24px;
      color: $color-text-d;
    }
    .input-inner {
      flex: 1;
      margin: 0 5px;
      line-height: 18px;
      background: $color-highlight-background;
      color: $color-text;
      font-size: $font-size-medium;
      outline: 0;
      &::placeholder {
        color: $color-text-d;
      }
    }
    .icon-dismiss {
      font-size: 16px;
      color: $color-text-d;
    }
  }
</style>

主要用的是 debounce 這個庫, 類似於 定時器的定時功能

debounce 實現

由於debouncen 只是往後推延函式的執行時間,並不具有throttle每隔一段時間一定會執行的能力,所以其實現起來更加簡單:

function debounce(delay, callback) {
  let timeoutID;
​
  function wrapper() {
    const self = this;
    const args = arguments;
​
    function exec() {
      callback.apply(self, args);
    }
​
    clearTimeout(timeoutID);
​
    timeoutID = setTimeout(exec, delay);
  }
​
  return wrapper;
}