/* jshint asi:true */ /** * [fixsidebar description] * 滚轮滚到一定位置时,将 sidebar-wrap 添加 fixed 样式 * 反之,取消样式 */ (function() { window.onload= function() { if (window.innerwidth > 770) { var sidebarwrap = document.queryselector('.leftnav') if(sidebarwrap == null){ return false; } //fix 之后百分比宽度会失效,这里用js赋予宽度 sidebarwrap.style.width = sidebarwrap.offsetwidth + "px" window.onscroll = function() { // 页面顶部滚进去的距离 var scrolltop = math.max(document.documentelement.scrolltop, document.body.scrolltop) // 页面底部滚进去的距离 var htmlheight = math.max($(".wraper").height(), document.documentelement.clientheight) // console.log(htmlheight); var scrollbottom = htmlheight - window.innerheight - scrolltop var numberlen = 40; if($("body").find(".headcontetnimg").length != 0){ numberlen = 258 //301 } if (scrolltop < numberlen) { sidebarwrap.classlist.remove('fixed') sidebarwrap.classlist.remove('scroll-bottom') } else if (scrollbottom >= (190 - 88)) { sidebarwrap.classlist.remove('scroll-bottom') sidebarwrap.classlist.add('fixed') } //else if (ismaxheight()) { //content 达到maxheight //sidebarwrap.classlist.remove('fixed') //sidebarwrap.classlist.add('scroll-bottom') //} } //setcontentmaxheightinpc() //设置目录最大高度(pc端) } //movetoc() //将content内容转移 } }()); /** * 设置目录最大高度 */ function setcontentmaxheightinpc() { var windowheight = window.innerheight var contentul = document.queryselector('.content-ul') var contentmaxheight = windowheight - 77 - 60 contentul.style.maxheight = contentmaxheight + 'px' } /** * 达到最大高度 * @return {boolean} [description] */ function ismaxheight() { var windowheight = window.innerheight var contentul = document.queryselector('.content-ul') var contentmaxheight = windowheight - 77 - 60 var contentheight = contentul.offsetheight return contentmaxheight === contentheight // console.log(contentmaxheight); // console.log(contentheight); } //-------------mobile-------------- /** * 屏幕宽度小于770px时,点击锚点按钮,弹出目录框 * @param {[type]} function( [description] * @return {[type]} [description] */ (function() { if (window.innerwidth <= 770) { var anchorbtn = document.queryselector('.anchor') var rightdiv = document.queryselector('.right') /** * 监听锚点按钮 */ anchorbtn.onclick = function(e) { e.stoppropagation() rightdiv.classlist.add('right-show') anchorbtn.classlist.add('anchor-hide') } //监听body,点击body,隐藏content document.queryselector('body').addeventlistener('click', function() { rightdiv.classlist.remove('right-show') anchorbtn.classlist.remove('anchor-hide') }) ancherpostion(anchorbtn, rightdiv) //目录锚的位置固定 setcontentmaxheight() //设置目录最大高度 } }()); /** * 目录锚的位置固定 */ function ancherpostion(anchorbtn, rightdiv) { window.addeventlistener('scroll', function() { // console.log('scroll'); var top = anchorbtn.getboundingclientrect().top // console.log(top); var scrolltop = math.max(document.documentelement.scrolltop, document.body.scrolltop) if (scrolltop > 50) { anchorbtn.style.top = '20px' rightdiv.style.top = '20px' } else { anchorbtn.style.top = '76px' rightdiv.style.top = '76px' } }) } /** * 设置目录最大高度 */ function setcontentmaxheight() { var windowheight = window.innerheight var contentul = document.queryselector('.content-ul') var contentmaxheight = windowheight - 180 contentul.style.maxheight = contentmaxheight + 'px' } //-------------post content---------------------- //将content内容转移 function movetoc() { if (document.queryselector('#markdown-toc') !== null) { var tocstring = document.queryselector('#markdown-toc').innerhtml var contentul = document.queryselector('#content-side') contentul.insertadjacenthtml('afterbegin', tocstring) //插入字符串 // if (!isandroidwechatbrowser()) { //添加scroll样式,为了平滑滚动 //add class "scroll", for smooth scroll var atags = document.queryselectorall('#content-side a') //add class for everyone // atags.foreach(function () { // console.log(this); // }) for (var i = 0; i < atags.length; i++) { // if (!atags[i].classlist.contains('scroll')) { // atags[i].classlist.add('scroll') // } if (!atags[i].hasattribute('data-scroll')) { atags[i].setattribute('data-scroll',''); } } // } } } /** * 判断安卓版微信浏览器 * @return {boolean} [description] */ function isandroidwechatbrowser() { var ua = navigator.useragent.tolowercase() return /micromessenger/.test(ua) && /android/.test(ua2) }