【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 分钟前
为什么给<a>标签设置了download属性, 浏览器没有下载而是打开新标签!!
前端·vue·html·下载·download
1024肥宅13 分钟前
面试和算法:常见面试题实现与深度解析
前端·javascript·面试
计算机程序设计小李同学15 分钟前
基于 Spring Boot 和 Vue.js 技术栈的网上订餐系统
vue.js·spring boot·后端
Ashley_Amanda21 分钟前
JavaScript 中数组的常用处理方法
开发语言·javascript·网络
BD_Marathon27 分钟前
Router_路由的基本使用
javascript
float_六七34 分钟前
行级与块级元素:核心区别与应用场景
开发语言·前端·javascript
毕设十刻1 小时前
基于Vue的家教预约系统7fisz(程序 + 源码 + 数据库 + 调试部署 + 开发环境配置),配套论文文档字数达万字以上,文末可获取,系统界面展示置于文末
前端·数据库·vue.js
前端无涯1 小时前
深度解析:fetch 与 Promise 结合实战及面试重点
前端·javascript
风舞红枫1 小时前
node代理vue打包后的文件,实现本地测试
前端·javascript·vue.js·node.js
helloCat1 小时前
记录CI/CD自动化上传AppGallery遇到的坑
android·前端·api