慕课网的一个小课程,练习了一遍,不足之处,欢迎指正(照片在本地,大家可以着重看代码哈):
<!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实现网页定位导航特效,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的,在此也非常感谢大家对脚本之间网站的支持!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com
暂无“基于JQuery和原生JavaScript实现网页定位导航特效”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。