vue实现搜索文章关键字,滑到指定位置并且高亮
vue
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
项目地址:https://gitcode.com/gh_mirrors/vu/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>
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:2 个月前 )
9e887079
[skip ci] 1 年前
73486cb5
* chore: fix link broken
Signed-off-by: snoppy <michaleli@foxmail.com>
* Update packages/template-compiler/README.md [skip ci]
---------
Signed-off-by: snoppy <michaleli@foxmail.com>
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 1 年前
新一代开源开发者平台 GitCode,通过集成代码托管服务、代码仓库以及可信赖的开源组件库,让开发者可以在云端进行代码托管和开发。旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。
更多推荐



所有评论(0)