极乐门资源网 Design By www.ioogu.com
慕课网的一个小课程,练习了一遍,不足之处,欢迎指正(照片在本地,大家可以着重看代码哈):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>爱淘气购物网-JQuery网页定位导航特效</title> <style> /*All tag*/ html, body, div, ul, li, img, h1, h2 { margin: 0; padding: 0; } ul { list-style: none; } a { text-decoration: none; color: #333; } /*menu*/ #menu { position: fixed; top: 100px; left: 50%; margin-left: 400px; } #menu > li > a { display: block; padding: 10px 15px; height: 30px; line-height: 30px; font-size: 14px; font-weight: bold; margin: 5px 0; } #menu > li > a:hover, #menu > li > a.current { color: #fff; background-color: #0088bb; } /*content*/ #content { width: 800px; margin: 30px auto; } h1 { color: #0088bb; font-size: 30px; margin-bottom: 10px; } .item { border: 1px dotted #0088bb; padding: 20px; margin-bottom: 20px; } .item > h2 { font-size: 16px; padding-bottom: 5px; border-bottom: 2px solid #0088bb; margin-bottom: 10px; } .item-list > li { display: inline-block; margin-right: 10px; } .item-list > li > a > img { width: 230px; height: 230px; border: none; /* IE6 hack*/ * html, * html body { background-image: url(about:blank); background-attachment: fixed; } * html #menu { /*position: fixed*/ position: absolute; top: expression((e = document.documentElement.scrollTop "menu"> <li><a class="current" href="#item1" rel="external nofollow" >1F 男装</a></li> <li><a href="#item2" rel="external nofollow" >2F 女装</a></li> <li><a href="#item3" rel="external nofollow" >3F 美妆</a></li> <li><a href="#item4" rel="external nofollow" >4F 数码</a></li> <li><a href="#item5" rel="external nofollow" >5F 母婴</a></li> </ul> <div id="content"> <h1>爱淘气购物网</h1> <div id="item1" class="item"> <h2>1F 男装</h2> <ul class="item-list"> <li> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/UploadFiles/2021-04-02/1F.jpg">下面再看下JS代码实现:
<script> //定义getByClassName函数,让函数实现根据className获取对象并返回 function getByClassName(obj, cls) { var elements = obj.getElementsByTagName("*"); var result = []; for (var i = 0; i < elements.length; i++) { if (elements[i].className == cls) { result.push(elements[i]); } } return result; } //定义hasClass函数,确认对象是否包含对应的class function hasClass(obj, cls) { return obj.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)")); } //定义removeClass函数,让函数实现删除对象的class function removeClass(obj, cls) { if (hasClass(obj, cls)) { //remove var reg = new RegExp("(\\s|^)" + cls + "(\\s|$)"); obj.className = obj.className.replace(reg, ""); } } //定义addClass函数,让函数实现给对象增加class function addClass(obj, cls) { if (!hasClass(obj, cls)) { obj.className += " " + cls; } } //页面加载完毕执行以下函数 window.onload = function() { //页面滚动时,执行以下函数 window.onscroll = function() { var top = document.documentElement.scrollTop || document.body.scrollTop; var menus = document.getElementById("menu").getElementsByTagName("a"); var items = getByClassName(document.getElementById("content"), "item"); var currentId = ""; for (var i = 0; i < items.length; i++) { var _item = items[i]; var _itemTop = _item.offsetTop; if (top > _itemTop - 200) { currentId = _item.id; } else { break; } } //给正确的menu下的a元素class赋值current if (currentId) { for (var j = 0; j < menus.length; j++) { var _menu = menus[j]; var _href = _menu.href.split("#"); if (_href[_href.length - 1] != currentId) { removeClass(_menu, "current"); } else { addClass(_menu, "current"); } } } } } </script>以上实例代码是小编给大家分享的JQuery和原生JavaScript实现网页定位导航特效,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的,在此也非常感谢大家对脚本之间网站的支持!
极乐门资源网 Design By www.ioogu.com
极乐门资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
极乐门资源网 Design By www.ioogu.com
暂无基于JQuery和原生JavaScript实现网页定位导航特效的评论...
更新日志
2025年01月13日
2025年01月13日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]