【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>
相关推荐
只会cv的前端攻城狮17 分钟前
Elpis-Core — 融合 Koa 洋葱圈模型实现服务端引擎
前端·后端
Java小卷1 小时前
流程设计器为啥选择diagram-js
前端·低代码·工作流引擎
HelloReader1 小时前
Isolation Pattern(隔离模式)在前端与 Core 之间加一道“加密网关”,拦截与校验所有 IPC
前端
兆子龙2 小时前
从 float 到 Flex/Grid:CSS 左右布局简史与「刁钻」布局怎么搞
前端·架构
YukiMori232 小时前
一个有趣的原型继承实验:为什么“男人也会生孩子”?从对象赋值到构造函数继承的完整推演
前端·javascript
_哆啦A梦2 小时前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
百里静修2 小时前
一个 Hook 拦截所有 AJAX 请求:ajax-hooker 使用指南与原理
前端
摸鱼的春哥3 小时前
惊!黑客靠AI把墨西哥政府打穿了,海量数据被黑
前端·javascript·后端
小兵张健3 小时前
Playwright MCP 截图标注方案调研(推荐方案1)
前端·javascript·github
小兵张健3 小时前
AI 页面与交互迁移流程参考
前端·ai编程·mcp