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>
GitHub 加速计划 / vu / vue
207.54 K
33.66 K
下载
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:2 个月前 )
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> 4 个月前
e428d891
Updated Browser Compatibility reference. The previous currently returns HTTP 404. 5 个月前
更多推荐
已为社区贡献3条内容
所有评论(0)