【vue】跨页面锚点跳转

跨页面锚点跳转

  1. 首页 跳转到 搜索页

一. 首页

  1. 通过路由跳转
bash 复制代码
query:{
   id:id
}

handleClick(){
    this.$router.push({
    	path:'/search',
    	query:this.query
    })
}

二. 搜索页

  1. 处理
bash 复制代码
<template>
    <div>
        <div class="industry_area">
            <div class="industry_single" v-for="(menu, index) in industryList" :key="index"
                @click="handleClick(menu.id)">
                <div :id="`id${menu.id}`" class="industry_title "
                    :class="activeIndustryId == menu.id ? 'industry_active' : ''">
                    {{
                        menu.dictValue
                    }}
                </div>
                <div :id="`id${menuChild.id}`" class="industry_child"
                    :class="activeIndustryId == menuChild.id ? 'industry_active' : ''" v-if="menu.children.length > 0"
                    v-for="(menuChild, index) in menu.children" :key="index" @click.stop="handleClick(menuChild.id, 2)">
                    <span>{{ menuChild.dictValue }}</span>
                </div>
            </div>
        </div>
    </div>
</template>

<script>

import { getTree} from '@/api/xxx'
export default {
    data() {
        return {
            industryList: [],
            activeIndustryId :'' // 当前选中项
        }
    },
    mounted() {
        this.fetchData()
    },
    methods: {

        /**
         * 获取行业列表数据
         */
        fetchData() {
            getTree('industry').then(res => {
                this.industryList = res.data
                if (res.code == 200 && this.activeIndustryId != "") {
                    this.$nextTick(() => {
                        this.anchorScrollIntoTarget()
                    })
                }
            })
        },
        
        /**
         * 根据行业id获取文档列表
         * @param {Int} id 
         */
        handleClick(id, level) {
            this.activeIndustryId = id
            this.$emit('handleClickSearch', id, level)
        },

        /**
         * 锚点跳转
         */
        anchorScrollIntoTarget() {  
            document.querySelector(`#id${this.activeIndustryId}`).scrollIntoView({
                behavior: 'smooth',
                block: 'center'
            })
        }
    }
}
</script>
相关推荐
有味道的男人11 分钟前
如何使用招标网API获取项目详情?
java·服务器·前端
qq_4061761412 分钟前
深入剖析JS中的XSS与CSRF漏洞:原理、攻击与防御全指南
服务器·开发语言·前端·javascript
RFCEO13 分钟前
HTML编程 课程六、:HTML5 新增多媒体标签
前端·html·html5·多媒体标签·嵌入音频、视频、动画
yanyu-yaya21 分钟前
速学兼复习之vue3章节4
前端·vue.js·前端框架
Mr-Wanter25 分钟前
vue 数据反显时数字/字母不换行导致的样式问题
前端·javascript·vue.js
梁萌32 分钟前
vue项目从npm升级为pnpm
前端·npm·node.js
修己xj33 分钟前
CSS魔法:对话生成器与奔驰骏马的创意实现
前端·css
qq_124987075333 分钟前
基于Java的心理测试系统的设计与实现(源码+论文+部署+安装)
java·开发语言·vue.js·spring boot·计算机毕设·计算机毕业设计
daols881 小时前
vue表格 vxe-table 如何实现键盘导航时,按回车健向右移动,并到最后一行时按回车自动新增一行
vue.js·vxe-table
琹箐1 小时前
Cursor 无法使用prettier格式化
前端