@15013890200
2018-08-20T12:42:29.000000Z
字数 3383
阅读 759
css 布局 javascript
- 之前在整理vue组件框架的时候,发现自己对页面整体方面的布局上还是很薄弱,尤其是在页面整体高度自适应的时候,页面内容撑开,左边的导航栏并没有及时的随之撑开。当时落入了一个误区,以为设置导航栏div高度为auto或者为100%就可以自适应。其实并没有达到一丝效果。于是抽空就自己重新试着布局,终于试探出只用css就可以达到高度自适应。happy!
- 同时也做了滚动加载实验。(之前一直都是翻页请求加载数据,一直没有做过滚动加载)数据是模拟ajax请求得到的数据。最大加载数据写死为102,当为102后,显示没有更多。
- 也做了滚动到顶部
<!DOCTYPE html><html><head><meta charset="utf-8"><title>cssTest</title><style type="text/css">*{margin: 0;padding: 0;}html,body{height: 100%;}.head{width: 100%;height: 60px;background-color: #3896f8;line-height: 60px;font-size: 18px;color: #fff;text-align: center;}.body{width: 100%;min-height: 883px;box-sizing: content-box;overflow: hidden;background-color: #efefef;position: relative;}.nav{width: 200px;background-color: #222D43;float: left;overflow-y: auto;position: absolute;top: 0;left: 0;bottom: 0;}.crumb{position: absolute;top: 10px;left: 220px;color: #666}.contain{width: auto;background-color: #fff;box-sizing: content-box;padding: 20px;margin-top: 40px;margin-left: 220px;margin-right: 20px; min-height: 763px; }.footer{height: 40px;width: auto;line-height: 40px;text-align: center;margin-left: 220px;color: #666;}.div_item{width: 100%;height: 80px;margin-bottom: 20px;border:1px solid #ddd;transition: background 0.3s;}.div_top{padding:5px 14px;line-height: 50px;text-align: center;color: #666;box-shadow: 0 0 3px #ddd;position: fixed;bottom: 20px;right: 10px;background-color: #ddd;cursor: pointer;transition: box-shadow 0.3s,padding 0.3s,bottom 0.3s,right 0.3s,visibility 0.3s;visibility: hidden;}.div_top:hover{box-shadow: 0 0 5px #666;padding: 6px 15px!important;bottom: 19px;right: 9px;}</style></head><body><div class="head">页面布局测试</div><div class="body"><div class="nav"></div><div class="crumb">crumb</div><div class="contain"><div id="contain"></div></div><div class="div_top" onclick="goTop()">顶部</div><div class="footer">copyright@wangjun</div></div></body><script type="text/javascript">var index = 0;//数据索引var arr = [];//数据数组var bottom = 0;//contain底边偏移量var clientH = document.documentElement.clientHeight || document.body.clientHeight;//浏览器底边偏移量var timeout = null;//定时任务 滚动加载时用到var scrollT = document.body.scrollTop;var showTop = false;function add(){/** 模拟ajax获取数据 */for(var i = 0; i < 10; i++){index ++;append(index);arr.push(index);}}function append(i){/** 将节点注入到contain */if(i > 102){if(document.getElementById('contain').lastChild.nodeName !== 'P'){var p = document.createElement('p');p.innerText = '没有更多了';document.getElementById('contain').appendChild(p);}return;}var dom = document.createElement('div');dom.setAttribute('class','div_item');dom.innerText = i;document.getElementById('contain').appendChild(dom);}add();function getContainPosition(){/** 判断contain底边框距离浏览器底边的距离,以此判断是否需要发起请求 */var cont = document.getElementById('contain');bottom = cont.getBoundingClientRect().bottom;return clientH-bottom;}function goTop(){/** 滚动到顶部+滚动特效 */var top = document.documentElement.scrollTop || document.body.scrollTop;var height = top/80;var time = setInterval(function(){window.scrollBy(0,-height);if(document.documentElement.scrollTop <= 0){clearInterval(time);}},1);}window.onscroll = function(){/** 监听页面滚动事件 */timeout = setTimeout(function(){var dis = getContainPosition();var scrollT = document.documentElement.scrollTop || document.body.scrollTop;if(scrollT > 200 && !showTop){document.querySelector('.div_top').style.visibility = 'visible';showTop = true;}if(scrollT < 200 && showTop){document.querySelector('.div_top').style.visibility = 'hidden';showTop = false;}if(dis > -20){add();clearTimeout(timeout);}},1500);}</script></html>
页面刚开始效果

滚动加载几轮之后

加上滚动到顶部效果
