vue3 实现浏览器ctrl+f功能

vue3 实现浏览器ctrl+f功能

安装

npm install search-bar-vue3 --save

插件GitHub地址

WenyaoL/search-bar-vue3: Recurrence of browser ctrl+F function (github.com)

Use

全局注册

import SearchBar from 'search-bar-vue3'
Vue.use(SearchBar)

局部注册

<template>
  <div>
    <search-bar 
    :root="'#document'" 
    :highlightClass="'myHighLight'" 
    :selectedClass="'selected-highlight'" 
    v-model:hidden="showSearchBar"/>
    <button @click="searchClick()">搜索按钮</button>
    <div id="document">
      <document/>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue';
import Document from './Document.vue'
import {SearchBar} from 'search-bar-vue3'

export default defineComponent({
  name: 'App',
  components: {
    Document,
    SearchBar
  },
  data(){
    return{
      showSearchBar:false
    }
  },
  methods:{
    searchClick(){
      
      this.showSearchBar = !this.showSearchBar
      console.log("切换showSearchBar",this.showSearchBar);
    }
  }
});
</script>

<style>
.myHighLight{
  background-color: yellow;
}
.selected-highlight{
  background-color: yellowgreen;
}
</style>

属性配置

propdescriptiontypedefault
rootSelector for element(will be put into docment.querySelector(root))stringMust provide
hiddenA bidirectional binding attribute to control the display and disappearance of the search bar(Please use v-model:hidden in Vue3 version)booleantrue
highlightClassThe className assigned by the highlighted blockstring“__highLight”
selectedClassThe className assigned by the selected blockstring“selected-highlight”

插件search-bar-vue2(vue2版本)

Vue2 versionWenyaoL/search-bar-vue2: Recurrence of browser ctrl+F function (github.com)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue实现浏览器的 `ctrl+f` 查找功能,可以通过监听键盘事件来实现。 首先,在需要监听键盘事件的组件中,添加 `@keydown.ctrl.f` 监听事件,例如: ```html <template> <div> <input v-model="searchText" placeholder="搜索"> <ul> <li v-for="(item, index) in list" :key="index">{{ item }}</li> </ul> </div> </template> <script> export default { data() { return { searchText: '', list: ['apple', 'banana', 'orange', 'pear'] } }, methods: { handleKeyDown(event) { if (event.ctrlKey && event.keyCode === 70) { // 当按下 ctrl+f 时 event.preventDefault(); // 阻止默认事件 this.$refs.searchInput.focus(); // 让输入框获取焦点 } } }, mounted() { this.$refs.searchInput.addEventListener('keydown', this.handleKeyDown); // 监听键盘事件 }, beforeDestroy() { this.$refs.searchInput.removeEventListener('keydown', this.handleKeyDown); // 移除键盘事件监听 } } </script> ``` 上述代码中,我们在 `mounted` 钩子函数中监听了键盘事件,并在 `beforeDestroy` 钩子函数中移除了键盘事件监听,以防止内存泄漏。 在 `handleKeyDown` 方法中,我们判断是否按下了 `ctrl+f` 快捷键,如果是,则阻止默认事件,并让输入框获取焦点,以便用户可以直接在输入框中输入要查找的内容。 需要注意的是,在监听键盘事件时,我们使用了 `addEventListener` 和 `removeEventListener` 方法来添加和移除事件监听,而不是在模板中直接绑定键盘事件。这是因为在 Vue 中,模板中绑定的事件监听会在组件销毁时自动移除,但是在本例中,我们需要手动移除键盘事件监听,以防止内存泄漏。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值