Skip to content

Commit

Permalink
fix don't switch current toc, #121
Browse files Browse the repository at this point in the history
  • Loading branch information
yscoder committed Nov 23, 2016
1 parent 9654c5a commit a84a3cd
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 67 deletions.
2 changes: 1 addition & 1 deletion _config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# hexo-theme-indigo
# https://github.com/yscoder/hexo-theme-indigo
version: 1.2.2
version: 1.2.3

#添加新菜单项遵循以下规则
# menu:
Expand Down
34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"name": "hexo-theme-material-indigo",
"version": "1.2.2",
"description": "Material Design theme for Hexo.",
"keywords": [
"hexo",
"theme",
"material design"
],
"author": {
"name": "yusen",
"email": "[email protected]"
},
"license": "MIT",
"repository": {
"url": "git+https://github.com/yscoder/hexo-theme-indigo.git",
"type": "git"
}
"name": "hexo-theme-material-indigo",
"version": "1.2.3",
"description": "Material Design theme for Hexo.",
"keywords": [
"hexo",
"theme",
"material design"
],
"author": {
"name": "yusen",
"email": "[email protected]"
},
"license": "MIT",
"repository": {
"url": "git+https://github.com/yscoder/hexo-theme-indigo.git",
"type": "git"
}
}
97 changes: 49 additions & 48 deletions source/js/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function(w, d) {
(function (w, d) {

var body = d.body,
$ = d.querySelector.bind(d),
Expand All @@ -12,11 +12,11 @@
menuOff = $('#menu-off'),
loading = $('#loading'),
animate = w.requestAnimationFrame,
scrollSpeed = 200 / ( 1000 / 60),
scrollSpeed = 200 / (1000 / 60),
forEach = Array.prototype.forEach,
even = ('ontouchstart' in w && /Mobile|Android|iOS|iPhone|iPad|iPod|Windows Phone|KFAPWI/i.test(navigator.userAgent)) ? 'touchstart' : 'click',
noop = function() {},
offset = function(el) {
noop = function () { },
offset = function (el) {
var x = el.offsetLeft,
y = el.offsetTop;

Expand All @@ -34,28 +34,28 @@
docEl = navigator.userAgent.indexOf('Firefox') !== -1 ? d.documentElement : body;

var Blog = {
goTop: function(end) {
goTop: function (end) {
var top = docEl.scrollTop;
var interval = arguments.length > 2 ? arguments[1] : Math.abs(top - end) / scrollSpeed;

if (top && top > end) {
docEl.scrollTop = Math.max(top - interval, 0);
animate(arguments.callee.bind(this, end, interval));
} else if(end && top < end) {
} else if (end && top < end) {
docEl.scrollTop = Math.min(top + interval, end);
animate(arguments.callee.bind(this, end, interval));
} else {
this.toc.actived(end);
}
},
toggleGotop: function(top) {
toggleGotop: function (top) {
if (top > w.innerHeight / 2) {
gotop.classList.add('in');
} else {
gotop.classList.remove('in');
}
},
toggleMenu: function(flag) {
toggleMenu: function (flag) {

if (flag) {
menu.classList.remove('hide');
Expand All @@ -72,14 +72,14 @@
root.classList.remove('lock');
}
},
fixedHeader: function(top) {
fixedHeader: function (top) {
if (top > header.clientHeight) {
header.classList.add('fixed');
} else {
header.classList.remove('fixed');
}
},
toc: (function() {
toc: (function () {
var toc = $('#post-toc');

if (!toc || !toc.children.length) {
Expand All @@ -96,24 +96,24 @@

toc.querySelector('a[href="#' + titles[0].id + '"]').parentNode.classList.add('active');

forEach.call($$('a[href^="#"]'), function(el) {
forEach.call($$('a[href^="#"]'), function (el) {

el.addEventListener('click', function(e) {
el.addEventListener('click', function (e) {
e.preventDefault();
var top = offset($('[id="' + decodeURIComponent(this.hash).substr(1) + '"]')).y - headerH;
animate(Blog.goTop.bind(Blog, top));
})
});

return {
fixed: function(top) {
fixed: function (top) {
if (top > tocTop - headerH) {
toc.classList.add('fixed');
} else {
toc.classList.remove('fixed');
}
},
actived: function(top) {
actived: function (top) {
for (i = 0, len = titles.length; i < len; i++) {
if (top > offset(titles[i]).y - headerH - 5) {
toc.querySelector('li.active').classList.remove('active');
Expand All @@ -130,7 +130,7 @@
}
}
})(),
modal: function(target) {
modal: function (target) {
this.$modal = $(target);
this.$off = this.$modal.querySelector('.close');

Expand All @@ -142,31 +142,31 @@
}
}

this.show = function() {
this.show = function () {
mask.classList.add('in');
_this.$modal.classList.add('ready');
setTimeout(function() {
setTimeout(function () {
_this.$modal.classList.add('in');
d.addEventListener(even, hideByBody);
}, 0)
}

this.hide = function() {
this.hide = function () {
mask.classList.remove('in');
_this.$modal.classList.remove('in');
setTimeout(function() {
setTimeout(function () {
_this.$modal.classList.remove('ready');
d.removeEventListener(even, hideByBody);
}, 300)
}

this.toggle = function() {
this.toggle = function () {
return _this.$modal.classList.contains('in') ? _this.hide() : _this.show();
}

this.$off && this.$off.addEventListener(even, this.hide);
},
share: function() {
share: function () {

var pageShare = $('#pageShare'),
fab = $('#shareFab');
Expand All @@ -175,24 +175,24 @@

$('#menuShare').addEventListener(even, shareModal.toggle);

if(fab) {
fab.addEventListener(even, function() {
if (fab) {
fab.addEventListener(even, function () {
pageShare.classList.toggle('in')
}, false)

d.addEventListener(even, function(e){
d.addEventListener(even, function (e) {
!fab.contains(e.target) && pageShare.classList.remove('in')
}, false)
}

var wxModal = new this.modal('#wxShare');

forEach.call($$('.wxFab'), function(el){
forEach.call($$('.wxFab'), function (el) {
el.addEventListener(even, wxModal.toggle)
})

},
search: function() {
search: function () {
var searchWrap = $('#search-wrap');

function toggleSearch() {
Expand All @@ -201,12 +201,12 @@

$('#search').addEventListener(even, toggleSearch);
},
reward: function() {
reward: function () {
var modal = new this.modal('#reward')

$('#rewardBtn').addEventListener(even, modal.toggle)
},
fixNavMinH: (function() {
fixNavMinH: (function () {
var nav = $('.nav');

function calcH() {
Expand All @@ -215,15 +215,15 @@

return calcH;
})(),
waterfall: function() {
waterfall: function () {

if (w.innerWidth < 760) return;

forEach.call($$('.waterfall'), function(el) {
forEach.call($$('.waterfall'), function (el) {
var childs = el.querySelectorAll('.waterfall-item');
var columns = [0, 0];

forEach.call(childs, function(item) {
forEach.call(childs, function (item) {
var i = columns[0] <= columns[1] ? 0 : 1;
item.style.cssText = 'top:' + columns[i] + 'px;left:' + (i > 0 ? '50%' : 0);
columns[i] += item.offsetHeight;
Expand All @@ -234,20 +234,20 @@
})

},
tabBar: function(el) {
tabBar: function (el) {
el.parentNode.parentNode.classList.toggle('expand')
},
page: (function(){
page: (function () {
var $elements = $$('.fade, .fade-scale');

return {
loaded: function() {
forEach.call($elements, function(el) {
loaded: function () {
forEach.call($elements, function (el) {
el.classList.add('in')
})
},
unload: function() {
forEach.call($elements, function(el) {
unload: function () {
forEach.call($elements, function (el) {
el.classList.remove('in')
})
}
Expand All @@ -256,7 +256,7 @@
})()
};

w.addEventListener('load', function() {
w.addEventListener('load', function () {
Blog.fixNavMinH();
Blog.waterfall();
var top = docEl.scrollTop;
Expand All @@ -266,39 +266,40 @@
Blog.page.loaded();
});

w.addEventListener('beforeunload', function() {
w.addEventListener('beforeunload', function () {
Blog.page.unload();
});

w.addEventListener('resize', function() {
w.addEventListener('resize', function () {
w.BLOG.even = even = 'ontouchstart' in w ? 'touchstart' : 'click';
Blog.fixNavMinH();
Blog.toggleMenu();
Blog.waterfall();
});

gotop.addEventListener(even, function() {
gotop.addEventListener(even, function () {
animate(Blog.goTop.bind(Blog, 0));
}, false);

menuToggle.addEventListener(even, function(e) {
menuToggle.addEventListener(even, function (e) {
Blog.toggleMenu(true);
e.preventDefault();
}, false);

menuOff.addEventListener(even, function() {
menuOff.addEventListener(even, function () {
menu.classList.add('hide');
}, false);

mask.addEventListener(even, function() {
mask.addEventListener(even, function () {
Blog.toggleMenu();
}, false);

d.addEventListener('scroll', function() {
d.addEventListener('scroll', function () {
var top = docEl.scrollTop;
Blog.toggleGotop(top);
Blog.fixedHeader(top);
Blog.toc.fixed(top);
Blog.toc.actived(top);
}, false);

if (w.BLOG.SHARE) {
Expand All @@ -315,9 +316,9 @@
Blog.$ = $;
Blog.$$ = $$;

Object.keys(Blog).reduce(function(g, e) {
g[e] = Blog[e];
return g
Object.keys(Blog).reduce(function (g, e) {
g[e] = Blog[e];
return g
}, w.BLOG);

Waves.init();
Expand Down
Loading

0 comments on commit a84a3cd

Please sign in to comment.