【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>
相关推荐
顺丰同城前端技术团队7 分钟前
DeepSeek 国产大模型新标杆
前端·后端·程序员
Java水解8 分钟前
微前端架构:从单体到模块化的前端新革命
前端
Mr_汪10 分钟前
uniapp使用h5的map(已弃用)
前端
前端进阶者13 分钟前
vite调试node_modules下面插件
前端·vite
YaHuiLiang21 分钟前
小微互联网公司与互联网创业公司 -- 学历之殇
前端·后端·面试
用户261245834016123 分钟前
vue学习路线(11.watch对比computed)
前端·vue.js
CAD老兵30 分钟前
前端 Source Map 原理与结构详解
前端
gnip33 分钟前
markdown预览自定义扩展实现
前端·javascript
大猫会长1 小时前
mac中创建 .command 文件,执行node服务
前端·chrome
旧时光_1 小时前
Zustand 状态管理库完全指南 - 进阶篇
前端·react.js