码迷,mamicode.com
首页 > 其他好文 > 详细

blog

时间:2018-07-08 10:42:10      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:scroll   ons   false   function   bre   name   for   lis   text   

var hs = $(".cnblogs-markdown h2,.cnblogs-markdown h3,.cnblogs-markdown h4");
if (hs.length) {
  addList()
}

function addList() {
  $("body").append($('<div id="hlist"></div>'));
  hs.each(function (i, v) {
    var txt = $(this).text();
    var idd = v.localName + i;
    v.id = idd;
    var item = $("<a></a>");
    item.text(txt);
    item.attr("href", ("#" + idd));
    $("#hlist").append(item)
  })
}
window.onscroll = function () {
  var nowTop = window.scrollY;
  nowTop < 600 ? $('a[href="#top"]').hide() : $('a[href="#top"]').show();
  if (!hs.length) {
    return false
  }
  $("#hlist>.active").removeClass();
  for (var k = 0; k < hs.length; k++) {
    if (nowTop < hs[k].offsetTop) {
      break
    }
  }
  if (k !== 0) {
    $("#hlist>a")[k - 1].setAttribute("class", "active")
  } else {
    return false
  }
};

blog

标签:scroll   ons   false   function   bre   name   for   lis   text   

原文地址:https://www.cnblogs.com/ak-b/p/9279097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!