mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-25 11:12:21 +00:00
edit js/ditto
This commit is contained in:
parent
ce893ab576
commit
1a6aa1ae42
@ -625,7 +625,7 @@ export function odd(n) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
上面代码中,`even.js`里面的函数`foo`有一个参数`n`,只要不等于0,就会减去1,传入加载的`odd()`。`odd.js`也会做类似操作。
|
上面代码中,`even.js`里面的函数`even`有一个参数`n`,只要不等于0,就会减去1,传入加载的`odd()`。`odd.js`也会做类似操作。
|
||||||
|
|
||||||
运行上面这段代码,结果如下。
|
运行上面这段代码,结果如下。
|
||||||
|
|
||||||
|
224
js/ditto.js
224
js/ditto.js
@ -21,22 +21,22 @@ var disqusCode = '<h3>留言</h3><div id="disqus_thread"></div>';
|
|||||||
var menu = new Array();
|
var menu = new Array();
|
||||||
|
|
||||||
function initialize() {
|
function initialize() {
|
||||||
// initialize sidebar and buttons
|
// initialize sidebar and buttons
|
||||||
if (ditto.sidebar) {
|
if (ditto.sidebar) {
|
||||||
init_sidebar_section();
|
init_sidebar_section();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ditto.back_to_top_button) {
|
if (ditto.back_to_top_button) {
|
||||||
init_back_to_top_button();
|
init_back_to_top_button();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ditto.edit_button) {
|
if (ditto.edit_button) {
|
||||||
init_edit_button();
|
init_edit_button();
|
||||||
}
|
}
|
||||||
|
|
||||||
// page router
|
// page router
|
||||||
router();
|
router();
|
||||||
$(window).on('hashchange', router);
|
$(window).on('hashchange', router);
|
||||||
}
|
}
|
||||||
|
|
||||||
function init_sidebar_section() {
|
function init_sidebar_section() {
|
||||||
@ -99,8 +99,8 @@ function init_edit_button() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function replace_symbols(text) {
|
function replace_symbols(text) {
|
||||||
// replace symbols with underscore
|
// replace symbols with underscore
|
||||||
return text.replace(/[&\/\\#,+=()$~%.'":*?<>{}\ \]\[]/g, "_");
|
return text.replace(/[&\/\\#,+=()$~%.'":*?<>{}\ \]\[]/g, "_");
|
||||||
}
|
}
|
||||||
|
|
||||||
function li_create_linkage(li_tag, header_level) {
|
function li_create_linkage(li_tag, header_level) {
|
||||||
@ -193,119 +193,117 @@ function show_loading() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function router() {
|
function router() {
|
||||||
|
var path = location.hash.replace(/#([^#]*)(#.*)?/, './$1');
|
||||||
|
|
||||||
var path = location.hash.replace(/#([^#]*)(#.*)?/, './$1');
|
if (ditto.save_progress && store.get('menu-progress') !== location.hash) {
|
||||||
|
store.set('menu-progress', location.hash);
|
||||||
|
store.set('page-progress', 0);
|
||||||
|
}
|
||||||
|
|
||||||
if (ditto.save_progress && store.get('menu-progress') !== location.hash) {
|
// default page if hash is empty
|
||||||
store.set('menu-progress', location.hash);
|
if (location.pathname === "/index.html") {
|
||||||
store.set('page-progress', 0);
|
path = location.pathname.replace("index.html", ditto.index);
|
||||||
}
|
normalize_paths();
|
||||||
|
} else if (path === "") {
|
||||||
|
path = window.location + ditto.index;
|
||||||
|
normalize_paths();
|
||||||
|
} else {
|
||||||
|
path = path + ".md";
|
||||||
|
}
|
||||||
|
|
||||||
// default page if hash is empty
|
// 取消scroll事件的监听函数
|
||||||
if (location.pathname === "/index.html") {
|
// 防止改变下面的变量perc的值
|
||||||
path = location.pathname.replace("index.html", ditto.index);
|
$(window).off('scroll');
|
||||||
normalize_paths();
|
|
||||||
} else if (path === "") {
|
// otherwise get the markdown and render it
|
||||||
path = window.location + ditto.index;
|
var loading = show_loading();
|
||||||
normalize_paths();
|
$.get(path, function(data) {
|
||||||
|
$(ditto.error_id).hide();
|
||||||
|
$(ditto.content_id).html(marked(data) + disqusCode);
|
||||||
|
if ($(ditto.content_id + " h1").text() === ditto.document_title) {
|
||||||
|
document.title = ditto.document_title;
|
||||||
} else {
|
} else {
|
||||||
path = path + ".md";
|
document.title = $(ditto.content_id + " h1").text() + " - " + ditto.document_title;
|
||||||
}
|
}
|
||||||
|
normalize_paths();
|
||||||
|
create_page_anchors();
|
||||||
|
|
||||||
// 取消scroll事件的监听函数
|
// 完成代码高亮
|
||||||
// 防止改变下面的变量perc的值
|
$('#content code').map(function() {
|
||||||
$(window).off('scroll');
|
Prism.highlightElement(this);
|
||||||
|
});
|
||||||
|
|
||||||
// otherwise get the markdown and render it
|
// 加载disqus
|
||||||
var loading = show_loading();
|
(function() {
|
||||||
$.get(path, function(data) {
|
// http://docs.disqus.com/help/2/
|
||||||
$(ditto.error_id).hide();
|
window.disqus_shortname = 'es6';
|
||||||
$(ditto.content_id).html(marked(data) + disqusCode);
|
window.disqus_identifier = (location.hash ? location.hash.replace("#", "") : 'READEME');
|
||||||
if ($(ditto.content_id + " h1").text() === ditto.document_title) {
|
window.disqus_title = $(ditto.content_id + " h1").text();
|
||||||
document.title = ditto.document_title;
|
window.disqus_url = 'http://es6.ruanyifeng.com/' + (location.hash ? location.hash.replace("#", "") : 'README');
|
||||||
} else {
|
|
||||||
document.title = $(ditto.content_id + " h1").text() + " - " + ditto.document_title;
|
|
||||||
}
|
|
||||||
normalize_paths();
|
|
||||||
create_page_anchors();
|
|
||||||
|
|
||||||
// 完成代码高亮
|
// http://docs.disqus.com/developers/universal/
|
||||||
$('#content code').map(function() {
|
(function() {
|
||||||
Prism.highlightElement(this);
|
var dsq = document.createElement('script');
|
||||||
});
|
dsq.type = 'text/javascript';
|
||||||
|
dsq.async = true;
|
||||||
// 加载disqus
|
dsq.src = 'http://' + window.disqus_shortname + '.disqus.com/embed.js';
|
||||||
(function() {
|
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
|
||||||
// http://docs.disqus.com/help/2/
|
})();
|
||||||
window.disqus_shortname = 'es6';
|
})();
|
||||||
window.disqus_identifier = (location.hash ? location.hash.replace("#", "") : 'READEME');
|
|
||||||
window.disqus_title = $(ditto.content_id + " h1").text();
|
|
||||||
window.disqus_url = 'http://es6.ruanyifeng.com/' + (location.hash ? location.hash.replace("#", "") : 'README');
|
|
||||||
|
|
||||||
// http://docs.disqus.com/developers/universal/
|
|
||||||
(function() {
|
|
||||||
var dsq = document.createElement('script');
|
|
||||||
dsq.type = 'text/javascript';
|
|
||||||
dsq.async = true;
|
|
||||||
dsq.src = 'http://' + window.disqus_shortname + '.disqus.com/embed.js';
|
|
||||||
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
|
|
||||||
})();
|
|
||||||
})();
|
|
||||||
|
|
||||||
var perc = ditto.save_progress ? store.get('page-progress') || 0 : 0;
|
var perc = ditto.save_progress ? store.get('page-progress') || 0 : 0;
|
||||||
|
|
||||||
if(location.hash !== '' || Boolean(perc)){
|
if (location.hash !== '' || Boolean(perc)) {
|
||||||
if (!Boolean(perc)){
|
if (!Boolean(perc)) {
|
||||||
$('html, body').animate({
|
$('html, body').animate({
|
||||||
scrollTop: ($('#content').offset().top + 10)
|
scrollTop: ($('#content').offset().top + 10)
|
||||||
}, 300);
|
}, 300);
|
||||||
$('html, body').animate({
|
$('html, body').animate({
|
||||||
scrollTop: ($('#content').offset().top)
|
scrollTop: ($('#content').offset().top)
|
||||||
}, 300);
|
}, 300);
|
||||||
} else {
|
} else {
|
||||||
$('html, body').animate({
|
$('html, body').animate({
|
||||||
scrollTop: ($('body').height()-$(window).height())*perc
|
scrollTop: ($('body').height()-$(window).height())*perc
|
||||||
}, 200);
|
}, 200);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (location.hash === '' || location.hash === menu[0]) {
|
if (location.hash === '' || location.hash === menu[0]) {
|
||||||
$('#pageup').css('display', 'none');
|
$('#pageup').css('display', 'none');
|
||||||
} else {
|
} else {
|
||||||
$('#pageup').css('display', 'inline-block');
|
$('#pageup').css('display', 'inline-block');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (location.hash === menu[(menu.length - 1)]) {
|
if (location.hash === menu[(menu.length - 1)]) {
|
||||||
$('#pagedown').css('display', 'none');
|
$('#pagedown').css('display', 'none');
|
||||||
} else {
|
} else {
|
||||||
$('#pagedown').css('display', 'inline-block');
|
$('#pagedown').css('display', 'inline-block');
|
||||||
}
|
}
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
var $w = $(window);
|
var $w = $(window);
|
||||||
var $prog2 = $('.progress-indicator-2');
|
var $prog2 = $('.progress-indicator-2');
|
||||||
var wh = $w.height();
|
var wh = $w.height();
|
||||||
var h = $('body').height();
|
var h = $('body').height();
|
||||||
var sHeight = h - wh;
|
var sHeight = h - wh;
|
||||||
|
$w.on('scroll', function() {
|
||||||
|
window.requestAnimationFrame(function(){
|
||||||
|
var perc = Math.max(0, Math.min(1, $w.scrollTop() / sHeight));
|
||||||
|
updateProgress(perc);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
$w.on('scroll', function() {
|
function updateProgress(perc) {
|
||||||
window.requestAnimationFrame(function(){
|
$prog2.css({width: perc * 100 + '%'});
|
||||||
var perc = Math.max(0, Math.min(1, $w.scrollTop() / sHeight));
|
ditto.save_progress && store.set('page-progress', perc);
|
||||||
updateProgress(perc);
|
}
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
function updateProgress(perc) {
|
}());
|
||||||
$prog2.css({width: perc * 100 + '%'});
|
|
||||||
ditto.save_progress && store.set('page-progress', perc);
|
|
||||||
}
|
|
||||||
|
|
||||||
}());
|
}).fail(function() {
|
||||||
|
show_error();
|
||||||
}).fail(function() {
|
}).always(function() {
|
||||||
show_error();
|
clearInterval(loading);
|
||||||
}).always(function() {
|
$(ditto.loading_id).hide();
|
||||||
clearInterval(loading);
|
});
|
||||||
$(ditto.loading_id).hide();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user