HTML粘性滑块导航页面Demo - 可自由填充内容
25-04-07 04:05
784
0
HTML 代码
<!doctype html> <html> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>jQuery粘性滑块导航(响应式)</title> <link rel="stylesheet" href="./style.css" /> </head> <body> <section> <h1>粘性滑块导航</h1> <h3>滑动内容与粘性选项卡导航</h3> <div> <a href="#tab-es6">ES6</a> <a href="#tab-flexbox">Flexbox</a> <a href="#tab-react">React</a> <a href="#tab-angular">Angular</a> <a href="#tab-other">Other</a> <span></span> </div> </section> <!-- Main --> <main> <section id="tab-es6"> <h1>ES6</h1> <h3>something about es6</h3> </section> <section id="tab-flexbox"> <h1>Flexbox</h1> <h3>something about flexbox</h3> </section> <section id="tab-react"> <h1>React</h1> <h3>something about react</h3> </section> <section id="tab-angular"> <h1>Angular</h1> <h3>something about angular</h3> </section> <section id="tab-other"> <h1>Other</h1> <h3>something about other</h3> </section> </main> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js"></script> <script src="./script.js"></script> </body> </html>
CSS 代码
a { text-decoration: none; } .et-hero-tabs, .et-slide { display: flex; flex-direction: column; justify-content: center; align-items: center; height: 100vh; position: relative; background: #eee; text-align: center; padding: 0 2em; } .et-hero-tabs h1, .et-slide h1 { font-size: 2rem; margin: 0; letter-spacing: 1rem; } .et-hero-tabs h3, .et-slide h3 { font-size: 1rem; letter-spacing: 0.3rem; opacity: 0.6; } .et-hero-tabs-container { display: flex; flex-direction: row; position: absolute; bottom: 0; width: 100%; height: 70px; box-shadow: 0 0 20px rgba(0, 0, 0, 0.1); background: #fff; z-index: 10; } .et-hero-tabs-container--top { position: fixed; top: 0; } .et-hero-tab { display: flex; justify-content: center; align-items: center; flex: 1; color: #000; letter-spacing: 0.1rem; transition: all 0.5s ease; font-size: 0.8rem; } .et-hero-tab:hover { color: white; background: rgba(102, 177, 241, 0.8); transition: all 0.5s ease; } .et-hero-tab-slider { position: absolute; bottom: 0; width: 0; height: 6px; background: #66b1f1; transition: left 0.3s ease; } @media (min-width: 800px) { .et-hero-tabs h1, .et-slide h1 { font-size: 3rem; } .et-hero-tabs h3, .et-slide h3 { font-size: 1rem; } .et-hero-tab { font-size: 1rem; } }
JS 代码
class StickyNavigation { constructor() { this.currentId = null; this.currentTab = null; this.tabContainerHeight = 70; let self = this; $(".et-hero-tab").click(function () { self.onTabClick(event, $(this)); }); $(window).scroll(() => { this.onScroll(); }); $(window).resize(() => { this.onResize(); }); } onTabClick(event, element) { event.preventDefault(); let scrollTop = $(element.attr("href")).offset().top - this.tabContainerHeight + 1; $("html, body").animate({ scrollTop: scrollTop }, 600); } onScroll() { this.checkTabContainerPosition(); this.findCurrentTabSelector(); } onResize() { if (this.currentId) { this.setSliderCss(); } } checkTabContainerPosition() { let offset = $(".et-hero-tabs").offset().top + $(".et-hero-tabs").height() - this.tabContainerHeight; if ($(window).scrollTop() > offset) { $(".et-hero-tabs-container").addClass("et-hero-tabs-container--top"); } else { $(".et-hero-tabs-container").removeClass("et-hero-tabs-container--top"); } } findCurrentTabSelector(element) { let newCurrentId; let newCurrentTab; let self = this; $(".et-hero-tab").each(function () { let id = $(this).attr("href"); let offsetTop = $(id).offset().top - self.tabContainerHeight; let offsetBottom = $(id).offset().top + $(id).height() - self.tabContainerHeight; if ($(window).scrollTop() > offsetTop && $(window).scrollTop() < offsetBottom) { newCurrentId = id; newCurrentTab = $(this); } }); if (this.currentId != newCurrentId || this.currentId === null) { this.currentId = newCurrentId; this.currentTab = newCurrentTab; this.setSliderCss(); } } setSliderCss() { let width = 0; let left = 0; if (this.currentTab) { width = this.currentTab.css("width"); left = this.currentTab.offset().left; } $(".et-hero-tab-slider").css("width", width); $(".et-hero-tab-slider").css("left", left); } } new StickyNavigation();
-
微信小程序登陆封装
index.wxml<!-- 登陆组件弹窗 --> <van-popup show="{{ loginBtn }}" round position="bottom" custom-style="height: 24%" bind:close="on... 761 0 25-04-07 -
会议管理表格 - 企业管理表格
年度会议计划表.doc年度例行事务会议安排表.doc会议室使用申请表.doc会议登记簿.doc会议通告单.doc会议通知.doc会议议程表.doc会议记录表(... 873 0 24-07-30 -
DeepSeek最强使用攻略,放弃复杂提示词,直接提问效果反而更好?
DeepSeek如何使用?DeepSeek网页版:https://chat.deepseek.com/DeepSeek客户端:https://download.deepseek.com/app/必学的三个对话模板由... 832 0 25-04-30 -
kindle图书商城小程序
"kindle图书商城小程序-微信小程序源码" 这个标题揭示了我们关注的项目是一个基于Kindle图书的微信小程序。它不仅是一个应用,更是一个可... 1369 0 24-10-16 -
2025年8款免费且好用的图片翻译工具
本文介绍了多种翻译工具及其功能。搜狗翻译、讯飞图片翻译、Google翻译和Yandex Translate等提供多语言文本和图像翻译服务,支持多达100多... 919 0 25-03-04 -
系统稳定,如何监控和保护进程安全
在开始本讲之前,我们先了解一个特点,在前端因为某些用户的特殊性,导致的逻辑 Bug 会造成这个用户服务异常,但是在服务端如果没有做好... 1128 0 24-05-24 -
Redi系统架构中各个处理模块是干什么的?
Redis 系统架构通过前面的学习,相信你已经掌握了 Redis 的原理、数据类型及访问协议等内容。本课时,我将进一步分析 Redis 的系统架... 836 0 24-05-24 -
五洲到家小程序
“五洲到家小程序源码,类似58到家”这是一款与58到家服务模式相仿的小程序源代码,旨在为用户提供类似于58到家的生活服务平台。58到家作为... 918 0 24-11-04
发表我的评论
共0条评论
- 这篇文章还没有收到评论,赶紧来抢沙发吧~