vue实现搜索文章关键字,滑到指定位置并且高亮

 1、输入搜索条件,点击搜索按钮

2、滑到定位到指定的搜索条件。 

 

<template>
  <div>
    <div class="search_form">
      <el-input
        v-model="searchVal"
        placeholder="请输入关键字查询"
        clearable
        size="small"
        style="width: 300px;"
      ></el-input>
      <el-button
        @click="searchFunc"
        type="primary"
        size="small"
        style=""
      >
        <i class="el-icon-search"></i> 查询
      </el-button>
    </div>
    
    <div class="editor" v-html="content"></div>
  </div>
</template>
<script>
import { listContraband } from '@/api/transCapacity/order'
export default{
  data() {
    return {
      searchVal: null,
      cacheContent: null,
      content: null,
      searchKey: '',
    }
  },
  created() {
    this.getContraband()
  },
  methods: {
    getContraband(){
      listContraband().then((response) => {
        this.$nextTick(()=>{
          this.content = response.data.content
          this.cacheContent = response.data.content
        })
      }).catch(()=>{})
    },
    searchFunc() {
      if (this.searchVal !== '') {
        const regex = new RegExp(this.searchVal, 'gi');
        this.content = this.cacheContent.replace(
          regex, 
          `<div class="targetElement"><mark style="background-color:#yellow;color:#FF6A29">`+ this.searchVal +`</mark></div>`
        )
        setTimeout(() => {
          this.scrollToElement();
        }, 100);
      }
      
    },
    scrollToElement() {
      this.$nextTick(() => {
        const element = document.querySelector('.targetElement')
        if (element) {
          element.scrollIntoView({ behavior: 'smooth' });
        }
      });
    },
  }
}
</script>
<style lang="scss" scoped>
  ::v-deep .search_form {
    display: flex;
    padding-bottom: 10px;
    .el-input__inner {
      border-top-right-radius: 0;
      border-bottom-right-radius: 0;
    }
    .el-button {
      border-top-left-radius: 0;
      border-bottom-left-radius: 0;
    }
  }

  ::v-deep table {
    border-top: 1px solid #ccc;
    border-left: 1px solid #ccc;
    border-spacing: 0;
  }

  ::v-deep table td {
    border-bottom: 1px solid #ccc;
    border-right: 1px solid #ccc;
    padding: 5px;
  }

  ::v-deep table th {
    border-bottom: 1px solid #ccc;
    border-right: 1px solid #ccc;
    padding: 5px;
  }

  ::v-deep table th {
    border-bottom: 2px solid #ccc;

  }
</style>

### UniApp 列表功能实现 在 UniApp 中,列表功能可以通过多种方式实现,具体取决于需求场景以及数据结构的设计。以下是基于 Vue.js 的模板语法和组件化设计来展示如何实现动态渲染列表。 #### 基本列表渲染 通过 `v-for` 指令可以轻松实现列表项的循环渲染: ```html <template> <view class="list-container"> <block v-for="(item, index) in listData" :key="index"> <view class="list-item">{{ item.name }}</view> </block> </view> </template> <script> export default { data() { return { listData: [ { name: 'Item 1' }, { name: 'Item 2' }, { name: 'Item 3' } ] }; } }; </script> <style> .list-container { padding: 20px; } .list-item { margin-bottom: 10px; font-size: 16px; } </style> ``` 上述代码展示了如何使用 `v-for` 渲染一个简单的静态列表[^4]。 --- #### 动态加载更多(分页) 对于大数据量的情况,通常会采用分页或者滚动加载的方式优化性能。以下是一个基本的滚动加载示例: ```javascript <template> <scroll-view scroll-y @scrolltolower="loadMore" class="scroll-list"> <view v-for="(item, index) in listData" :key="index" class="list-item"> {{ item.name }} </view> <view v-if="loading" class="loading-text">正在加载...</view> </scroll-view> </template> <script> export default { data() { return { listData: [], page: 1, pageSize: 10, loading: false }; }, onLoad() { this.fetchData(); }, methods: { fetchData() { if (this.loading) return; this.loading = true; setTimeout(() => { const newData = Array.from({ length: this.pageSize }, (_, i) => ({ name: `Item ${(this.page - 1) * this.pageSize + i}` })); this.listData.push(...newData); this.page++; this.loading = false; }, 1000); }, loadMore() { this.fetchData(); } } }; </script> <style> .scroll-list { height: calc(100vh - 100px); } .list-item { padding: 10px; border-bottom: 1px solid #ccc; } .loading-text { text-align: center; color: gray; } </style> ``` 此代码实现了当用户滑动到底部时自动触发加载更多的逻辑[^5]。 --- #### 结合模糊搜索高亮显示 如果需要实现在列表中支持模糊搜索高亮匹配关键字的功能,则可参考如下方法: ```html <template> <view> <input type="text" placeholder="请输入关键词..." v-model="searchKeyword" /> <view v-for="(item, index) in filteredList" :key="index" class="list-item"> <rich-text :nodes="highlight(item.name)"></rich-text> </view> </view> </template> <script> export default { data() { return { searchKeyword: '', originalList: ['Apple', 'Banana', 'Orange', 'Grapes'], highlightStyle: '<span style="color:red;">{{keyword}}</span>' }; }, computed: { filteredList() { if (!this.searchKeyword.trim()) return this.originalList; return this.originalList.filter((item) => item.toLowerCase().includes(this.searchKeyword.toLowerCase()) ); } }, methods: { highlight(text) { if (!this.searchKeyword) return text; const regex = new RegExp(`(${this.searchKeyword})`, 'gi'); return text.replace(regex, `<strong>$1</strong>`); // 自定义样式 } } }; </script> <style> .list-item { padding: 10px; border-bottom: 1px solid #eee; } </style> ``` 该部分结合了引用中的模糊搜索概念[^2],通过正则表达式对匹配到的关键字进行了特殊样式的处理。 --- #### 总结 以上分别介绍了 UniApp 中基础列表渲染、带分页效果的大规模数据加载以及带有模糊查询能力的高级列表应用案例。这些技术能够满足大多数实际项目的需求,可根据业务复杂度进一步扩展功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值