当前位置: 首页 > news >正文

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

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

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

 

<template><div><div class="search_form"><el-inputv-model="searchVal"placeholder="请输入关键字查询"clearablesize="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.contentthis.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>

http://www.lryc.cn/news/392418.html

相关文章:

  • Stable Diffusion与AI艺术:探索人工智能的创造力
  • 华为HCIP Datacom H12-821 卷26
  • golang 获取系统的主机 CPU 内存 磁盘等信息
  • Infinitar链游新发展新机遇
  • Figma 被爆出它剽窃了苹果的设计后撤下了AI工具Make Designs
  • ERROR | Web server failed to start. Port 8080 was already in use.
  • C++ 类和对象 构造函数
  • 纯javascript实现图片批量压缩打包zip下载后端ThinkPHP多国语言切换国际站
  • 使用ChatGPT写论文,只需四步突破论文写作瓶颈!
  • 神领物流项目第一天
  • [作业]10 枚举-排列类
  • vue2(vue-cli3x[vue.config.js])使用cesium新版(1.117.0)配置过程
  • 【深度学习】常用命令行指令汇总
  • 谷粒商城学习-11-docker安装redis
  • C++:类继承是什么,怎么继承
  • 期权学习必看圣书:《3小时快学期权》要在哪里看?
  • Keepalived 双机热备
  • 基于React和TypeScript的开源白板项目(Github项目分享)
  • 1019记录
  • 详细设计与概要设计区别-慧哥充电桩开源系统
  • vue3 引入百度地图的三种方式
  • 鸿蒙开发设备管理:【@ohos.usb (USB管理)】
  • Golang | Leetcode Golang题解之第204题计数质数
  • ELK日志系统和Filebeat采集器的学习总结
  • QML-Grid和OpacityMask
  • MySQL的并发控制、事务、日志
  • CNN文献综述
  • python语句前面有一个$是什么意思
  • wsl安装Linux系统到指定位置
  • [笔记] 高等数学在各工程门类的典型应用场景