左侧导航栏吸顶并实现滑动指定内容时对应导航高亮

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            body{
                background: #ddd;
            }
            ul{
                list-style: none;
            }
            a{
                text-decoration: none;
            }
            .nav{
                background: #fff;
                width: 100%;
                height: 100px;
                font-size: 20px;
                line-height: 100px;
                text-align: center;
                border-bottom: 1px solid #f60;
            }
            .box{
                margin: 0 auto;
                width: 1200px;
            }
            .fl_l{
                width: 200px;
                float: left;
                border: 1px solid #f4f4f4;
                background: #fff;
            }
            .fl_l li a{
                border-bottom: 1px solid #eee;
                text-align: center;
                display: block;
                color: #333;
                font-size: 14px;
                line-height: 60px;
            }
            .fl_l li.active a{
                background: #f60;
                color: #fff;
            }
            .fl_r{
                float: right;
                width: 960px;
            }
            .fl_r li{
                margin-bottom: 30px;
                background: #fff;
                font-size: 50px;
                line-height: 300px;
                display: block;
                text-align: center;
            }
            .banner{
                background: #fff;
                width: 100%;
                height: 600px;
                font-size: 40px;
                line-height: 600px;
                text-align: center;
                margin-bottom: 30px;
            }
        </style>
    </head>
    <body>
        <!--辅助理解的线条<div style="position: fixed;top: 316px;height: 1px;background: #000;width: 100%;"></div>-->
        <div class="nav">nav</div>
        <div class="banner">banner</div>
        <div class="box">
            <ul class="fl_l">
                <li class="active"><a href="##">菜单1</a></li>
                <li><a href="##">菜单2</a></li>
                <li><a href="##">菜单3</a></li>
                <li><a href="##">菜单4</a></li>
                <li><a href="##">菜单5</a></li>
            </ul>
            <ul class="fl_r">
                <li style="height: 600px;">菜单1内容</li>
                <li style="height: 600px;">菜单2内容</li>
                <li style="height: 400px;">菜单3内容</li>
                <li style="height: 500px;">菜单4内容</li>
                <li style="height: 800px;">菜单5内容</li>
            </ul>
            <div style="clear: both;"></div>
        </div>
        <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.js"></script>
        <script type="text/javascript">
            $(function(){
                //设置标杆
                var _line=parseInt($(window).height()/3);
                console.log(_line);
                $(window).scroll(function(){
                    //滚动730px,左侧导航固定定位
                    if ($(window).scrollTop()>730) {
                        $('.fl_l').css({'position':'fixed','top':100})
                    }else{
                        $('.fl_l').css({'position':'','top':''})
                    };
                    $('.fl_l li').eq(0).addClass('active');
                    //滚动到标杆位置,左侧导航加active
                    $('.fl_r li').each(function(){
                        var _target=parseInt($(this).offset().top-$(window).scrollTop()-_line);
                        var _i=$(this).index();
                        if (_target<=0) {
                            $('.fl_l li').removeClass('active');
                            $('.fl_l li').eq(_i).addClass('active');
                        }
                        //如果到达页面底部,给左侧导航最后一个加active
                        else if($(document).height()==$(window).scrollTop()+$(window).height()){
                            $('.fl_l li').removeClass('active');
                            $('.fl_l li').eq($('.fl_r li').length-1).addClass('active');
                        }
                    });
                });
                $('.fl_l li').click(function(){
                    $(this).addClass('active').siblings().removeClass('active');
                    var _i=$(this).index();
                     $('body, html').animate({scrollTop:$('.fl_r li').eq(_i).offset().top-_line},500);
                });
            });
        </script>
    </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现吸顶导航高亮显示当前模块需要进行以下步骤: 1. 在页面中添加吸顶导航对应的模块内容,可以使用`<header>`和`<section>`标签。 2. 在导航中绑定点击事件,使用`ref`获取对应的模块节点,然后使用`scrollIntoView()`方法实现滚动指定模块的效果。 3. 使用`IntersectionObserver`监听每个模块的位置,当某个模块进入视口,将对应导航高亮显示。 下面是一个示例代码: ``` <template> <div class="container"> <header ref="header"> <ul> <li v-for="(item, index) in navList" :key="index" @click="scrollToSection(index)" :class="{ active: activeIndex === index }">{{ item }}</li> </ul> </header> <section v-for="(item, index) in sectionList" :key="index" ref="sections"> <h2>{{ item }}</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla nec sapien id dolor commodo accumsan. Nullam auctor, tortor eu elementum lacinia, odio sem eleifend sapien, vel efficitur velit lacus a nisi. Pellentesque sit amet nisi vitae enim pretium feugiat. Maecenas euismod vestibulum nisi, a semper ipsum sollicitudin eu.</p> </section> </div> </template> <script lang="ts"> import { defineComponent, onMounted, ref } from 'vue'; export default defineComponent({ setup() { const navList = ['Section 1', 'Section 2', 'Section 3', 'Section 4']; const sectionList = ['Section 1', 'Section 2', 'Section 3', 'Section 4']; const activeIndex = ref(0); const scrollToSection = (index: number) => { const sections = document.querySelectorAll('section'); sections[index].scrollIntoView({ behavior: 'smooth' }); }; const observer = new IntersectionObserver( (entries) => { entries.forEach((entry) => { if (entry.intersectionRatio > 0.5) { activeIndex.value = Number(entry.target.dataset.index); } }); }, { threshold: 0.5 } ); onMounted(() => { const header = document.querySelector('header'); const sections = document.querySelectorAll('section'); sections.forEach((section, index) => { section.setAttribute('data-index', String(index)); observer.observe(section); }); header?.classList.add('sticky'); }); return { navList, sectionList, activeIndex, scrollToSection, }; }, }); </script> <style scoped> .container { max-width: 800px; margin: 0 auto; padding: 20px; } header { position: relative; z-index: 1; background-color: #fff; padding: 10px; margin-bottom: 20px; border-bottom: 1px solid #e0e0e0; } header.sticky { position: sticky; top: 0; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1); } ul { display: flex; justify-content: space-between; list-style: none; margin: 0; padding: 0; } li { cursor: pointer; padding: 10px; } li.active { font-weight: bold; } section { margin-bottom: 20px; } h2 { margin-top: 0; } </style> ``` 在上面的代码中,我们使用了Vue 3的Composition API来管理状态和生命周期。在`setup`函数中,我们定义了`navList`和`sectionList`数组来存储导航和模块的内容,以及`activeIndex`变量来跟踪当前活动的模块。然后,我们定义了`scrollToSection`函数来实现点击导航滚动指定模块的效果。 在`onMounted`生命周期钩子中,我们使用`IntersectionObserver`来监听每个模块的位置,当某个模块进入视口,将对应导航高亮显示。我们还使用`classList`来动态添加/删除`sticky`类,实现吸顶导航的效果。 最后,我们将组件导出,可以在其他组件中使用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值