代码格式化 & 去除异步

i18n
枋柚 5 years ago
parent b6754389b5
commit 36354ce71c
  1. 2
      layout/_partial/scripts.ejs
  2. 192
      source/js/app.js

@ -23,7 +23,7 @@
<script src="//instant.page/1.2.2" type="module" integrity="sha384-2xV8M5griQmzyiY3CDqh1dn4z3llDVqZDqzjzcY+jCBCk/a5fXJmuZ/40JJAPeoU"></script>
<% if (theme.scrollreveal == true) { %>
<script async src="https://cdn.jsdelivr.net/npm/scrollreveal@4.0.5/dist/scrollreveal.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/scrollreveal@4.0.5/dist/scrollreveal.min.js"></script>
<script type="text/javascript">
$(function() {
const $reveal = $('.reveal');

@ -12,10 +12,12 @@ var customSearch;
function scrolltoElement(elem, correction) {
correction = correction || scrollCorrection;
const $elem = elem.href ? $(elem.getAttribute('href')) : $(elem);
$('html, body').animate({ 'scrollTop': $elem.offset().top - correction }, 400);
};
$('html, body').animate({
'scrollTop': $elem.offset().top - correction
}, 400);
}
function setScrollAnchor(){
function setScrollAnchor() {
// button
const $postsBtn = $('.menu .active');
const $topBtn = $('.s-top');
@ -24,13 +26,25 @@ var customSearch;
const $bodyAnchor = $('.l_body');
// action
if ($postsBtn.length && $bodyAnchor) {
$postsBtn.click(e => { e.preventDefault(); e.stopPropagation(); scrolltoElement($bodyAnchor); });
$postsBtn.click(e => {
e.preventDefault();
e.stopPropagation();
scrolltoElement($bodyAnchor);
});
}
if ($titleBtn.length && $bodyAnchor) {
$titleBtn.click(e => { e.preventDefault(); e.stopPropagation(); scrolltoElement($bodyAnchor); });
$titleBtn.click(e => {
e.preventDefault();
e.stopPropagation();
scrolltoElement($bodyAnchor);
});
}
if ($topBtn.length && $bodyAnchor) {
$topBtn.click(e => { e.preventDefault(); e.stopPropagation(); scrolltoElement($bodyAnchor); });
$topBtn.click(e => {
e.preventDefault();
e.stopPropagation();
scrolltoElement($bodyAnchor);
});
}
const $coverAnchor = $('.cover-wrapper');
@ -45,11 +59,11 @@ var customSearch;
pos = scrollTop;
if (scrollTop > 150) {
$topBtn.addClass('show');
if (del > 0) {
$topBtn.removeClass('hl');
} else {
$topBtn.addClass('hl');
}
if (del > 0) {
$topBtn.removeClass('hl');
} else {
$topBtn.addClass('hl');
}
} else {
$topBtn.removeClass('show').removeClass('hl');
}
@ -59,7 +73,7 @@ var customSearch;
$headerAnchor.removeClass('show');
}
});
}
}
function setHeader() {
if (!window.subData) return;
@ -84,12 +98,19 @@ var customSearch;
// bind events to every btn
const $commentTarget = $('#comments');
if ($commentTarget.length) {
$comment.click(e => { e.preventDefault(); e.stopPropagation(); scrolltoElement($commentTarget); });
$comment.click(e => {
e.preventDefault();
e.stopPropagation();
scrolltoElement($commentTarget);
});
} else $comment.remove();
const $tocTarget = $('.toc-wrapper');
if ($tocTarget.length && $tocTarget.children().length) {
$toc.click((e) => { e.stopPropagation(); $tocTarget.toggleClass('active'); });
$toc.click((e) => {
e.stopPropagation();
$tocTarget.toggleClass('active');
});
} else $toc.remove();
@ -97,9 +118,9 @@ var customSearch;
}
function setHeaderMenuSelection() {
var $headerMenu = $('body .navgation');
// 先把已经激活的取消激活
$headerMenu.find('li a.active').removeClass('active');
var $headerMenu = $('body .navgation');
// 先把已经激活的取消激活
$headerMenu.find('li a.active').removeClass('active');
// var $underline = $headerMenu.find('.underline');
function setUnderline($item) {
// if (!transition) $underline.addClass('disable-trans');
@ -109,20 +130,20 @@ var customSearch;
}
//set current active nav
var $active_link = null;
var idname = location.pathname.replace(/\/|%/g, "");
if (idname.length == 0) {
idname = "home";
}
var idname = location.pathname.replace(/\/|%/g, "");
if (idname.length == 0) {
idname = "home";
}
var page = idname.match(/page\d{0,}$/g);
if (page) {
page = page[0];
idname = idname.split(page)[0];
}
var index = idname.match(/index.html/);
if (index) {
index = index[0];
idname = idname.split(index)[0];
}
var index = idname.match(/index.html/);
if (index) {
index = index[0];
idname = idname.split(index)[0];
}
if (idname && $headerMenu) {
$active_link = $('#' + idname, $headerMenu);
setUnderline($active_link);
@ -158,53 +179,53 @@ var customSearch;
$search.click(function (e) {
e.stopPropagation();
});
$header.ready(function () {
$header.bind('keydown', function (event) {
if (event.keyCode == 9) {
return false;
} else {
var isie = (document.all) ? true: false;
var key;
var ev;
if (isie) { //IE浏览器
key = window.event.keyCode;
ev = window.event;
} else { //火狐浏览器
key = event.which;
ev = event;
}
if (key == 9) { //IE浏览器
if (isie) {
ev.keyCode = 0;
ev.returnValue = false;
} else { //火狐浏览器
ev.which = 0;
ev.preventDefault();
}
}
}
});
});
$header.ready(function () {
$header.bind('keydown', function (event) {
if (event.keyCode == 9) {
return false;
} else {
var isie = (document.all) ? true : false;
var key;
var ev;
if (isie) { //IE浏览器
key = window.event.keyCode;
ev = window.event;
} else { //火狐浏览器
key = event.which;
ev = event;
}
if (key == 9) { //IE浏览器
if (isie) {
ev.keyCode = 0;
ev.returnValue = false;
} else { //火狐浏览器
ev.which = 0;
ev.preventDefault();
}
}
}
});
});
}
function setTocToggle() {
const $toc = $('.toc-wrapper');
if ($toc.length === 0) return;
// $toc.click((e) => {
// e.stopPropagation();
// $toc.addClass('active');
// });
// e.stopPropagation();
// $toc.addClass('active');
// });
$(document).click(() => $toc.removeClass('active'));
$toc.on('click', 'a', (e) => {
e.preventDefault();
e.stopPropagation();
if (e.target.tagName === 'A') {
scrolltoElement(e.target);
} else if (e.target.tagName === 'SPAN') {
scrolltoElement(e.target.parentElement);
}
$toc.removeClass('active');
scrolltoElement(e.target);
} else if (e.target.tagName === 'SPAN') {
scrolltoElement(e.target.parentElement);
}
$toc.removeClass('active');
});
const liElements = Array.from($toc.find('li a'));
@ -216,7 +237,9 @@ var customSearch;
const scrollTop = $('html').scrollTop() || $('body').scrollTop();
if (!anchor) return;
//binary search.
let l = 0, r = anchor.length - 1, mid;
let l = 0,
r = anchor.length - 1,
mid;
while (l < r) {
mid = (l + r + 1) >> 1;
if (anchor[mid] === scrollTop) l = r = mid;
@ -236,57 +259,52 @@ var customSearch;
scrollListener();
}
$(function () {
//set header
setHeader();
setHeaderMenuSelection();
setHeaderMenuPhone();
setHeaderSearch();
setTocToggle();
setScrollAnchor();
// $(".article .video-container").fitVids();
setTimeout(function () {
$('#loading-bar-wrapper').fadeOut(500);
}, 300);
function setSearchService() {
if (SEARCH_SERVICE === 'google') {
customSearch = new GoogleCustomSearch({
apiKey: GOOGLE_CUSTOM_SEARCH_API_KEY,
engineId: GOOGLE_CUSTOM_SEARCH_ENGINE_ID,
imagePath: "/img/"
});
}
else if (SEARCH_SERVICE === 'algolia') {
} else if (SEARCH_SERVICE === 'algolia') {
customSearch = new AlgoliaSearch({
apiKey: ALGOLIA_API_KEY,
appId: ALGOLIA_APP_ID,
indexName: ALGOLIA_INDEX_NAME,
imagePath: "/img/"
});
}
else if (SEARCH_SERVICE === 'hexo') {
} else if (SEARCH_SERVICE === 'hexo') {
customSearch = new HexoSearch({
imagePath: "/img/"
});
}
else if (SEARCH_SERVICE === 'azure') {
} else if (SEARCH_SERVICE === 'azure') {
customSearch = new AzureSearch({
serviceName: AZURE_SERVICE_NAME,
indexName: AZURE_INDEX_NAME,
queryKey: AZURE_QUERY_KEY,
imagePath: "/img/"
});
}
else if (SEARCH_SERVICE === 'baidu') {
} else if (SEARCH_SERVICE === 'baidu') {
customSearch = new BaiduSearch({
apiId: BAIDU_API_ID,
imagePath: "/img/"
});
}
}
$(function () {
setHeader();
setHeaderMenuSelection();
setHeaderMenuPhone();
setHeaderSearch();
setTocToggle();
setScrollAnchor();
setSearchService();
// $(".article .video-container").fitVids();
setTimeout(function () {
$('#loading-bar-wrapper').fadeOut(500);
}, 300);
});
})(jQuery);
})(jQuery);
Loading…
Cancel
Save