老师效果显示好像有点问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <meta http-equiv= "X-UA-Compatible" content= "IE=edge" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <title>Document</title> <style> * { margin: 0; padding: 0; } .content-part { width: 1000px; margin: 0 auto; margin-bottom: 0; background-color: #ccc; font-size: 50px; } .floornav { position: fixed; right: 20px; width: 100px; top: 50%; margin-top: -140px; height: 280px; background-color: orange; } .floornav ul { list-style: none; } .floornav ul li { width: 100px; height: 40px; line-height: 40px; text-align: center; font-size: 25px; cursor: pointer; } .floornav ul li.current { background: gold; color: white; } </style> </head> <body> <div class= "floornav" > <ul id= "list" > <li data-n= "科技" class= "current" >科技</li> <li data-n= "体育" >体育</li> <li data-n= "新闻" >新闻</li> <li data-n= "娱乐" >娱乐</li> <li data-n= "视频" >视频</li> <li data-n= "好看" >好看</li> <li data-n= "其他" >其他</li> </ul> </div> <section class= "content-part" style= "height:674px" data-n= "科技" >科技栏目</section> <section class= "content-part" style= "height:548px" data-n= "体育" >体育栏目</section> <section class= "content-part" style= "height:762px" data-n= "新闻" >新闻栏目</section> <section class= "content-part" style= "height:895px" data-n= "娱乐" >娱乐栏目</section> <section class= "content-part" style= "height:556px" data-n= "视频" >视频栏目</section> <section class= "content-part" style= "height:840px" data-n= "好看" >好看栏目</section> <section class= "content-part" style= "height:430px" data-n= "其他" >其他栏目</section> <script> // 使用事件委托给li添加监听 var list = document.getElementById( "list" ); var contentParts = document.querySelectorAll( ".content-part" ); var lis = document.querySelectorAll( "#list li" ); list.onclick = function (e) { if (e.target.tagName.toLowerCase() == "li" ) { // getAttribute表示得到标签身上的某个属性值 var n = e.target.getAttribute( "data-n" ); // 可以用属性选择器(就是方括号选择器)来寻找带有相同的data-n的content-part var contentPart = document.querySelector( ".content-part[data-n=" + n + "]" ); // 让页面的卷动自动成为这个盒子的offsetTop document.documentElement.scrollTop = contentPart.offsetTop; } } // 在页面加载好之后,将所有的content-part盒子的offsetTop值推入数组 var offsetTopArr = []; // 遍历所有的contentParts,将他们的净位置推入数组 for ( var i = 0; i < contentParts.length; i++) { offsetTopArr.push(contentParts[i].offsetTop); } // 为了最后一项可以方便比较,我们可以推入一个无穷大 offsetTopArr.push(Infinity); console.log(offsetTopArr); // 当前所在楼层 var nowdfloor = -1; // 窗口的卷动 window.onscroll = function () { var scrollTop = document.documentElement.scrollTop; // 遍历offsetTopArr数组,看看当前的acrollTop值在哪两个楼层之间 for ( var i = 0; i < offsetTopArr.length; i++) { if (scrollTop >= offsetTopArr[i] && scrollTop < offsetTopArr[i + 1]) { break ; } } // 退出循环的时候i是几 就表示当前楼层是几 // 如果当前所在楼层不是i 就表示换楼了 if (nowdfloor != i) { console.log(i); // 让全局变量改变为这个楼层号 nowdfloor = i; // 设置类名-下标为i的项有current for ( var j = 0; j < lis.length; j++) { if (j == i) { lis[j].className = "current" ; } else { lis[j].className = "" ; } } } } </script> </body> </html> |
搜索
复制
16
收起
正在回答
1回答
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧