console.log(top) 为什么总是0??获取不到高度
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>JS实战</title> <link rel="stylesheet" type="text/css" href="./css/index.css" /> <link rel="stylesheet" type="text/css" href="./css/base.css" /> <link rel="stylesheet" type="text/css" href="./css/animate.css" /> </head> <body> <!-- 头部 --> <header> <div class="header__logo">H5实战页面</div> <div class="header__nava"> <!-- 导航项 --> <a href="javascript:;" class="header__nava-item header__nava-item_custom_ative">实战课程</a> <a href="javascript:;" class="header__nava-item">商业案例</a> <a href="javascript:;" class="header__nava-item">课程体系</a> <a href="javascript:;" class="header__nava-item">集成环境</a> <a href="javascript:;" class="header__nava-item">云端学习</a> <!-- 导航项按钮 --> <a href="javascript:;" class="header__nava-item header__nava-item_custom_buttom">即刻学习</a> </div> </header> <!-- 屏幕1 --> <div class="screen1"> <!-- 屏幕1标题1 --> <h2 class="screen1__heading">实战课程重磅上线</h2> <!-- 屏幕2标题2 --> <h4 class="screen1__subheading">一键云学习,还在等什么?</h4> </div> <!-- 屏幕2 --> <div class="screen2"> <!-- 网页内容容器 --> <div class="screen__wrap"> <!-- 屏幕2标题1 --> <h2 class="screen2__wrap__heading">每门课都是真实商业案例</h2> <div class="screen2__wrap__heading__line"></div> <!-- 屏幕2标题2 --> <h5 class="screen2__wrap__subheading">真实案例,真实场景,在实战中实践、操作调试<br>大牛带你体验BAT真实开发流程,所有开发过程一一为你呈现</h5> <!-- 屏幕2图片项 --> <div class="screen2__warp_img-item"> <div class="screen2__wrap__img-item__bg-1"></div> <div class="screen2__wrap__img-item__bg-2"></div> <div class="screen2__wrap__img-item__bg-3"></div> </div> </div> </div> <!-- 屏幕3 --> <div class="screen3"> <!-- 容器 --> <div class="screen__wrap"> <!-- 屏幕3图片 --> <div class="screen3__img__bg-1"></div> <!-- 屏幕3标题1 --> <h2 class="screen3__heding">强大的语言课程体系支持</h2> <div class="screen3__heading__line"></div> <!-- 屏幕3标题2 --> <h3 class="screen3__subheding">学习环境与课程轻松对接,安装、调试、写入、部署、运行,一站式解决<br>,让你体验开发流程</h3> <div class="screen3__inner"> <div class="screen3__inner-1">HTML5</div> <div class="screen3__inner-2">PHP</div> <div class="screen3__inner-3">JAVA</div> <div class="screen3__inner-4">Python</div> <div class="screen3__inner-5">Node.JS</div> </div> </div> </div> <!-- 屏幕4 --> <div class="screen4"> <div class="screen__wrap"> <h2 class="screen4__wrap__heading">省去本地复杂的环境搭建</h2> <div class="screen4__wrap__heading__line"></div> <h3 class="screen4__wrap__subheading">你可以告别在虚拟机中调试开发了</h3> <div class="screen4__wrap__img"> <div class="screen4__wrap__img__bg-1"></div> <div class="screen4__wrap__img__bg-2"></div> <div class="screen4__wrap__img__bg-3"></div> <div class="screen4__wrap__img__bg-4"></div> <h4 class="screen4__wrap__img__bg-1__inner-1">实战课程集成开发环境</h4> <h4 class="screen4__wrap__img__bg-2__inner-2">内置终端命令行</h4> <h4 class="screen4__wrap__img__bg-3__inner-3">编译你的应用程序</h4> <h4 class="screen4__wrap__img__bg-4__inner-4">通过云端服务输出效果</h4> </div> </div> </div> <!-- 屏幕5 --> <div class="screen5"> <div class="screen__wrap"> <div class="screen5__wrap__img__bg-1"></div> <h2 class="screen5__wrap__heading">云端学习可以这样简单</h2> <div class="screen5__wrap__heading__line"></div> <h3 class="screen5__wrap__subheading">看视频,敲代码一气呵成。结合慕课网为你提供的云端学习工具,所见即所得。从此学习不一样</h3> </div> </div> <!-- 屏幕6 --> <div class="screen6"> <a href="javascript:;" class="screen6__bottom">继续了解学习体验</a> </div> </div> <!-- 页脚 --> <footer> <div class="screen__wrap"> <div class="footer__heading"> <a href="javascript:;">首页网站</a> <a href="javascript:;">人才招聘</a> <a href="javascript:;">联系我们</a> <a href="javascript:;">高校联盟</a> <a href="javascript:;">关于我们</a> <a href="javascript:;">讲师招募</a> <a href="javascript:;">意见反馈</a> <a href="javascript:;">友情链接</a> </div> <div class="footer__subheading"> <p>copyright ©2015 imooc.com All Rights Reserved | 京ICP备 13046642号-2</p> </div> </div> </footer> <div class="outline"> <a href="javascript:;" class="outline__item">实</a> <a href="javascript:;" class="outline__item">商</a> <a href="javascript:;" class="outline__item">课</a> <a href="javascript:;" class="outline__item">集</a> <a href="javascript:;" class="outline__item">云</a> </div> <script src="./JS/index.js"></script> </body> </html>
/* 头部 */ header{ height:60px; width: 100%; position: fixed; top: 0; left: 0; z-index: 999; } /* 头部logo */ .header__logo{ height: 40px; background-color:rgba(128,128,128, 0.1); font-size: 18px; background: url(../img/logo.png)no-repeat; font-weight: bold; color: white; z-index: 99; text-indent: 60px; line-height: 40px; float: left; margin-left:10px; margin-top: 10px; } /* 网页内容容器 */ .screen__wrap{ width: 1200px; height: 640px; margin: 0 auto; } /* 头部导航 */ .header__nava{ float: right; display: block; line-height: 60px; height: 60px; } /* 头部导航项 */ .header__nava-item{ color: #efefff; margin-right: 40px; cursor:pointer; } .header__nava-item_custom_ative{ margin-top: 14px; border-bottom:#ff0000 2px solid; padding-bottom: 14px; } /* 头部导航按钮 */ .header__nava-item_custom_buttom{ border-radius: 5px; height:40px; width: 96px; background-color: #f01400; color: #efefff; display: block; float:right; margin-top: 10px; line-height: 40px; margin-right: 30px; text-align: center; } /* 屏幕1 */ .screen1{ height: 640px; width: 100%; background:url(../img/sc1.jpg)no-repeat; background-size: cover; display: block; position: relative; } /* 屏幕1标题1 */ .screen1__heading{ position: absolute; font-size:40px; font-weight: bold; color:#ffff; display: block; padding-top:240px; text-align: center; float: left; width: 100%; } /* 屏幕1标题2 */ .screen1__subheading{ position: absolute; color:#ffff; display: block; padding-top:315px; text-align: center; float: left; width: 100%; } /* 屏幕2 */ .screen2{ width: 100%; height:640px; height: 640px; width: 100%; background-size: cover; display: block; position: relative; overflow: hidden; } /* 屏幕2标题1 */ .screen2__wrap__heading{ color:#07111b; font-size: 40px; text-align: center; margin: 0; line-height: 40px; padding-top: 90px; } .screen2__wrap__heading__line{ border-bottom: #ff0000 2px solid; width: 50px; text-align: center; margin-top: 30px; margin: auto; margin-top: 37px; } /* 屏幕2标题2 */ .screen2__wrap__subheading{ line-height: 16px; color:#07111b; font-size: 16px; text-align: center; margin: 0; line-height: 28px; padding-top: 24px; } /* 屏幕2 图片项 */ .screen2__warp__img-item{ width: 100%; height:391px; display: block; float: left; overflow: hidden; margin: 0 auto; } /* 屏幕2 图片项1 */ .screen2__wrap__img-item__bg-1{ display: block; position: absolute; float: left; background: url(../img/sc2.png)no-repeat; height: 361px; width: 750px; z-index: 1; color:red; margin-left: 225px; margin-top: 35px; } /* 屏幕2 图片项2 */ .screen2__wrap__img-item__bg-2{ display: block; position: absolute; float: left; background: url(../img/sc2-1.png)no-repeat; height: 380px; width: 275px; z-index: 99; margin-left: 462.5px; margin-top: 20px; } /* 屏幕2 图片项3 */ .screen2__wrap__img-item__bg-3{ display: block; position: absolute; float: left; background: url(../img/sc2-2.png)no-repeat; height: 305px; width: 266px; margin-left: 580px; margin-top: 103px; z-index: 3; } /* 屏幕3 */ .screen3{ float: left; width: 100%; height:640px; height: 640px; width: 100%; background-size: cover; display: block; background-color:#2b333b; overflow: hidden; } /* 屏幕3图片 */ .screen3__img__bg-1{ display: block; float: left; background: url(../img/sc3.png)no-repeat; height: 340px; width: 308px; margin-left: -80px; margin-top: 150px; z-index: 1; transform: scale(1.85); } /* 屏幕3标题1 */ .screen3__heding{ font-size:35px; color: #ffff; padding-left: 665px; padding-top:230px; } .screen3__heading__line{ border-bottom: #ff0000 2px solid; width: 50px; margin-left: 665px; margin-top: 30px; } /* 屏幕3标题2 */ .screen3__subheding{ font-size:15px; color: #ffff; margin-left: 665px; line-height: 25px; padding-top:20px; } .screen3__inner{ margin-left: 677px; display: block; } .screen3__inner-1{ float: left; width: 65px; height: 65px; border: #1f5975 4px solid; border-radius: 50%; color:#2294ff; text-align: center; line-height: 65px; margin-top: 140px; margin-right:35px; } .screen3__inner-2{ float: left; width: 65px; height: 65px; border: #424d76 4px solid; border-radius: 50%; color:#7888fd; text-align: center; line-height: 65px; margin-top: 140px; margin-right:35px; } .screen3__inner-3{ float: left; width: 65px; height: 65px; border: #6b4146 4px solid; border-radius: 50%; color:#dd413b; text-align: center; line-height: 65px; margin-top: 140px; margin-right:35px; } .screen3__inner-4{ float: left; width: 65px; height: 65px; border: #29535f 4px solid; border-radius: 50%; color:#27bde5; text-align: center; line-height: 65px; margin-top: 140px; margin-right:35px; } .screen3__inner-5{ float: left; width: 65px; height: 65px; border: #3e4e40 4px solid; border-radius: 50%; color:#909e41; text-align: center; line-height: 65px; margin-top: 140px; } /* 屏幕4 */ .screen4{ float: left; width: 100%; height:640px; height: 640px; width: 100%; background-size: cover; display: block; position: relative; background-color:#f3f5f7; overflow: hidden; } .screen4__wrap__heading{ color: #07111b; font-size: 40px; text-align: center; padding-top:90px; } .screen4__wrap__heading__line{ border-bottom: #ff0000 4px solid; width: 50px; padding-top: 37px; text-align: center; margin: 0 auto; } .screen4__wrap__subheading{ color: #07111b; font-size: 16px; text-align: center; padding-top:63px; } .screen4__wrap__img{ display: block; height:100%; width: 100%; margin: 0 auto; } .screen4__wrap__img__bg-1{ background: url(../img/sc4-1.png)no-repeat; margin-top: 80px; margin-left: 100px; margin-right: 220px; width: 80px; height: 70px; display: block; float: left; } .screen4__wrap__img__bg-2{ background: url(../img/sc4-2.png)no-repeat; margin-right: 220px; margin-top: 80px; width: 80px; height:75px; display: block; float: left; } .screen4__wrap__img__bg-3{ background: url(../img/sc4-3.png)no-repeat; margin-right: 220px; margin-top: 80px; width: 80px; height:75px; display: block; float: left; } .screen4__wrap__img__bg-4{ background: url(../img/sc4-4.png)no-repeat; margin-top: 80px; width: 80px; height:80px; display: block; float: left; } .screen4__wrap__img__bg-1__inner-1{ height: 16px; margin-top: 30px; padding-left:60px; font-size: 16px; line-height: 16px; float: left; color: #07111b; } .screen4__wrap__img__bg-2__inner-2{ height: 16px; margin-top: 30px; padding-left:160px; font-size: 16px; line-height: 16px; float: left; color: #07111b; } .screen4__wrap__img__bg-3__inner-3{ height: 16px; margin-top: 30px; padding-left:180px; font-size: 16px; line-height: 16px; float: left; color: #07111b; } .screen4__wrap__img__bg-4__inner-4{ height: 16px; margin-top: 30px; padding-left:160px; font-size: 16px; line-height: 16px; float: left; color: #07111b; } /* 屏幕5 */ .screen5{ position: absolute; float: left; width: 100%; height:640px; height: 640px; width: 100%; display: block; position: relative; background: url(../img/sc5.jpg)no-repeat; background-size: cover; overflow: hidden; } .screen5__wrap__img__bg-1{ background: url(../img/sc5-1.png)no-repeat; width: 200px; height: 200px; margin-left: 496px; margin-top: 100px; } .screen5__wrap__heading{ color: #ffffff; font-size: 40px; text-align: center; padding-top:90px; } .screen5__wrap__heading__line{ border-bottom: #ffffff 4px solid; width: 50px; padding-top: 30px; text-align: center; margin: 0 auto; } .screen5__wrap__subheading{ color: #ffffff; font-size: 16px; text-align: center; padding-top:30px; } .screen6__bottom{ width: 240px; height: 60px; border: #707070 1px solid; border-radius: 5px; margin: 0 auto; margin-top: 70px; text-align: center; line-height: 60px; font-weight: bold; color:#14191e; display: block; } .screen6__bottom:hover{ color: #ff0000; } /* 屏幕6 */ .screen6{ position: absolute; float: left; width: 100%; height:200px; width: 100%; background-size: cover; display: block; position: relative; background-color:#ffff; overflow: hidden; } /* 页脚 */ footer{ float: left; height: 105px; width: 100%; background-color:#000000; overflow: hidden; position: relative; overflow: hidden; } footer a{ margin-right: 40px; color: #ffffff; } .footer__heading{ margin-top: 33px; width: 100%; height:15px; text-align: center; } .footer__subheading{ padding-top: 13px; height:20px; margin:0 auto; text-align: center; } .footer__subheading p{ font-size: 12px; color: #ffffff; text-align: center; } .outline{ position: fixed; bottom: 120px; right: 0; box-shadow: 0px 4px 12px 0px rgba(7, 17, 27, 0.1); border-radius: 5px; } .outline__item{ display: block; width: 40px; height: 30px; color: #93999f; text-align: center; line-height: 30px; border-top: 1px solid #eee; background-color: #ffffff; } .outline__item:first-child{ border:none; border-radius:5px 0 0 0; } .outline__item:last-child{ border-radius:0 0 0 5px; } .outline__item:hover,.outline__item__ative{ color: #ff0000; } /* .outline { position: fixed; padding: 5px 10px; bottom: 120px; right: 0; z-index: 1; background-color: #fff; box-shadow: 0px 4px 12px 0px rgba(7, 17, 27, 0.1); z-index: 3; } .outline__item { display: block; width: 40px; height: 30px; line-height: 30px; padding: 5px 0; background: rgb(255,255,255); margin: 5px 0px 0px; border-top: 1px solid #eee; text-align: center; color: #93999f; } .outline__item:first-child{ border:none; } .outline__item:hover, .outline__item_status_active{ color: #f00; } */
/* 屏幕1动画 */ .screen1__heading{ transition: all .8s; } .screen1__subheading{ transition: all 1.1s; } .screen1__heading_animate_init, .screen1__subheading_animate_init{ transform: translate(0,100%); opacity: 0; } .screen1__heading_animate_done, .screen1__subheading_animate_done{ transform: translate(0,0); opacity: 1; } /* 屏幕2动画 */ .screen2__wrap__img-item__bg-3{ transition: all 1s .4s; } .screen2__wrap__heading__line{ transition: all 1s .5s; } .screen2__wrap__heading, .screen2__wrap__subheading, .screen2__wrap__img-item__bg-2{ transition: all 1s; } .screen2__wrap__heading_animate_init, .screen2__wrap__heading__line_animate_init, .screen2__wrap__subheading_animate_init, .screen2__wrap__img-item__bg-2_animate_init{ transform: translate(0,100%); opacity: 0; } .screen2__wrap__img-item__bg-3_animate_init{ transform: translate(-100%,100%); opacity: 0; } .screen2__wrap__heading_animate_done, .screen2__wrap__heading__line_animate_done, .screen2__wrap__subheading_animate_done, .screen2__wrap__img-item__bg-2_animate_done, .screen2__wrap__img-item__bg-3_animate_done{ transform: translate(0,0); opacity: 1; } /* 屏幕3动画 */ .screen3__img__bg-1, .screen3__heding{ transition: all .7s; } .screen3__inner{ transition: all .4s .3s ease-in; } .screen3__subheding{ transition: all .7s .2s; } .screen3__heading__line{ transition: all 1s .2s; } .screen3__img__bg-1_animate_init, .screen3__heding_animate_init, .screen3__heading__line_animate_init{ transform: translate(0,40%); opacity: 0; } .screen3__inner_animate_init{ transform: translate(0,100px); opacity: 0; } .screen3__subheding_animate_init{ transform: translate(0,100%); opacity: 0; } .screen3__img__bg-1_animate_done, .screen3__heding_animate_done, .screen3__heading__line_animate_done, .screen3__subheding_animate_done, .screen3__inner_animate_done{ transform: translate(0,0); opacity: 1; } /* 屏幕4动画 */ .screen4__wrap__heading{ transition: all 1s; } .screen4__wrap__subheading, .screen4__wrap__heading__line{ transition: all 1s .1s; } .screen4__wrap__heading_animate_init, .screen4__wrap__subheading_animate_init, .screen4__wrap__heading__line_animate_init{ transform: translate(0,100px); opacity: 0; } .screen4__wrap__heading_animate_done, .screen4__wrap__subheading_animate_done, .screen4__wrap__heading__line_animate_done{ transform: translate(0,0); opacity: 1; } .screen4__wrap__img__bg-1, .screen4__wrap__img__bg-2, .screen4__wrap__img__bg-3, .screen4__wrap__img__bg-4{ transition: all 1s; } .screen4__wrap__img__bg-1_animate_init, .screen4__wrap__img__bg-2_animate_init, .screen4__wrap__img__bg-3_animate_init, .screen4__wrap__img__bg-4_animate_init{ transform: scale(0); opacity: 0; } .screen4__wrap__img__bg-1_animate_done, .screen4__wrap__img__bg-2_animate_done, .screen4__wrap__img__bg-3_animate_done, .screen4__wrap__img__bg-4_animate_done{ transform: scale(1); opacity: 1; } .screen4__wrap__img__bg-1__inner-1, .screen4__wrap__img__bg-2__inner-2, .screen4__wrap__img__bg-3__inner-3, .screen4__wrap__img__bg-4__inner-4{ transition: all 1.2s .3s; } .screen4__wrap__img__bg-1__inner-1_animate_init, .screen4__wrap__img__bg-2__inner-2_animate_init, .screen4__wrap__img__bg-3__inner-3_animate_init, .screen4__wrap__img__bg-4__inner-4_animate_init{ transform: translate(0,100%); opacity: 0; } .screen4__wrap__img__bg-1__inner-1_animate_done, .screen4__wrap__img__bg-2__inner-2_animate_done, .screen4__wrap__img__bg-3__inner-3_animate_done, .screen4__wrap__img__bg-4__inner-4_animate_done{ transform: translate(0,0); opacity: 1; } .screen5__wrap__img__bg-1{ transition: all 1s ; } .screen5__wrap__heading{ transition: all 1.2s; } .screen5__wrap__heading__line, .screen5__wrap__subheading{ transition: all 1.2s .3s; } .screen5__wrap__heading_animate_init, .screen5__wrap__heading__line_animate_init, .screen5__wrap__subheading_animate_init{ transform: translate(0,100%); opacity: 0; } .screen5__wrap__heading_animate_done, .screen5__wrap__heading__line_animate_done, .screen5__wrap__subheading_animate_done{ transform: translate(0,0); opacity: 1; } .screen5__wrap__img__bg-1{ transition: all .7s; } .screen5__wrap__img__bg-1_animate_init{ transform: scale(0); opacity: 0; } .screen5__wrap__img__bg-1_animate_done{ transform: scale(1); opacity: 1; }
*{ margin:0; padding:0; font-size:12px; font-family: "Microsoft Yahei"; } a{ text-decoration: none; }
// 获取元素 var getElement =function(selector){ return document.querySelector(selector); } // 获取元素素组 var getAllElement =function(selector){ return document.querySelectorAll(selector); } // 获取元素样式 var getAttribute =function(element){ return element.getAttribute('class'); } // 设置元素样式 var setAttribute =function(element,cls){ return element.setAttribute('class',cls); } // 添加元素样式 var addcls = function(element,cls){ var basecls=element.getAttribute('class') if(basecls.indexOf(cls)>-1){ setAttribute('class','basecls'+" "+"cls"); } } // 删除元素样式 var delcls= function(element,cls){ var basecls=element.getAttribute('class'); if(basecls.indexOf(cls)>-1){ setAttribute('class',basecls.split(cls).join(' ').replace(/\s+/g,' ')); } return } var screenAnimateElements ={ '.screen1':[ '.screen1__heading', '.screen1__subheading' ], '.screen2':[ '.screen2__wrap__heading', '.screen2__wrap__heading__line', '.screen2__wrap__subheading', '.screen2__wrap__img-item__bg-2', '.screen2__wrap__img-item__bg-3' ], '.screen3':[ '.screen3__img__bg-1', '.screen3__heding', '.screen3__heading__line', '.screen3__subheding', '.screen3__inner' ], '.screen4':[ '.screen4__wrap__heading', '.screen4__wrap__heading__line', '.screen4__wrap__subheading', '.screen4__wrap__img__bg-1', '.screen4__wrap__img__bg-2', '.screen4__wrap__img__bg-3', '.screen4__wrap__img__bg-4', '.screen4__wrap__img__bg-1__inner-1', '.screen4__wrap__img__bg-2__inner-2', '.screen4__wrap__img__bg-3__inner-3', '.screen4__wrap__img__bg-4__inner-4' ], '.screen5':[ '.screen5__wrap__img__bg-1', '.screen5__wrap__heading', '.screen5__wrap__heading__line', '.screen5__wrap__subheading' ] }; var setScreenAnimateInit =function(screencls){ var animateElements=screenAnimateElements[screencls];//获取需要设置动画的元素组 for(var i=0;i<animateElements.length;i++){ var element = document.querySelector(animateElements[i]);//获取需要设置动画的元素 var baseCls = element.getAttribute('class');//获取基础样式名称 element.setAttribute('class',baseCls +' '+animateElements[i].substr(1)+'_animate_init'); } } var playsreenAnimateDone =function(screencls){ for(var i=0;i<animateElements.length;i++){ var element = document.querySelector(animateElements[i]); var baseCls = element.getAttribute('class'); element.setAttribute('class',baseCls.replace('_animate_init','_animate_done')); } } window.onload=function(){ console.log('onload') for(k in screenAnimateElements){ setScreenAnimateInit(k); } } window.onscroll=function(){ var top=document.body.scrollTop; console.log(top); }
10
收起
正在回答
2回答
同学你好,主要是由于浏览器解析的问题:如下:
(1)对于没有使用doctype声明的页面可以使用document.body.scrollTop获取scollTop的高度。
源码中没有使用doctype声明,所以直接使用document.body.scrollTop即可
(2)对于有使用doctype声明的页面可以使用document.documentElement.scrollTop获取scollTop的高度。
同学的代码中使用了doctype声明,需要使用document.documentElement.scrollTop
推荐同学使用兼容性的语法获取scrollTop的高度。
如果我的回答帮助到了你,欢迎采纳,祝学习愉快~
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星