Skip to content

Commit

Permalink
新增目录导航smooth滑动效果
Browse files Browse the repository at this point in the history
  • Loading branch information
hsxyhao committed Apr 22, 2020
1 parent 85b3884 commit f1a0c4b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 75 deletions.
Binary file removed assets/media/images/custom-bgImg.png
Binary file not shown.
154 changes: 81 additions & 73 deletions templates/_blocks/after-body.ejs
Original file line number Diff line number Diff line change
@@ -1,92 +1,100 @@
<input hidden id="copy" />
<script>
//拿来主义(真香)^_^,Clipboard 实现摘自掘金 https://juejin.im/post/5aefeb6e6fb9a07aa43c20af
window.Clipboard = (function(window, document, navigator) {
var textArea,
//拿来主义(真香)^_^,Clipboard 实现摘自掘金 https://juejin.im/post/5aefeb6e6fb9a07aa43c20af
window.Clipboard = (function (window, document, navigator) {
var textArea,
copy;
// 判断是不是ios端
function isOS() {
return navigator.userAgent.match(/ipad|iphone/i);
}
//创建文本元素
function createTextArea(text) {
textArea = document.createElement('textArea');
textArea.value = text;
textArea.style.width = 0;
textArea.style.height = 0;
textArea.clientHeight = 0;
textArea.clientWidth = 0;
document.body.appendChild(textArea);
}
//选择内容
function selectText() {
var range,
// 判断是不是ios端
function isOS() {
return navigator.userAgent.match(/ipad|iphone/i);
}
//创建文本元素
function createTextArea(text) {
textArea = document.createElement('textArea');
textArea.value = text;
textArea.style.width = 0;
textArea.style.height = 0;
textArea.clientHeight = 0;
textArea.clientWidth = 0;
document.body.appendChild(textArea);
}
//选择内容
function selectText() {
var range,
selection;
if (isOS()) {
range = document.createRange();
range.selectNodeContents(textArea);
selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
textArea.setSelectionRange(0, 999999);
} else {
textArea.select();
if (isOS()) {
range = document.createRange();
range.selectNodeContents(textArea);
selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
textArea.setSelectionRange(0, 999999);
} else {
textArea.select();
}
}
}
//复制到剪贴板
function copyToClipboard() {
try{
document.execCommand("Copy")
}catch(err){
alert("复制错误!请手动复制!")
//复制到剪贴板
function copyToClipboard() {
try {
document.execCommand("Copy")
} catch (err) {
alert("复制错误!请手动复制!")
}
document.body.removeChild(textArea);
}
document.body.removeChild(textArea);
}
copy = function(text) {
createTextArea(text);
selectText();
copyToClipboard();
};
copy = function (text) {
createTextArea(text);
selectText();
copyToClipboard();
};
return {
copy: copy
};
})(window, document, navigator);
return {
copy: copy
};
})(window, document, navigator);
function copyCode (e) {
if (e.srcElement.tagName === 'SPAN' && e.srcElement.classList.contains('copy-code')) {
let code = e.currentTarget.querySelector('code');
var text = code.innerText;
if (e.srcElement.textContent === '复制成功') {
console.log('复制操作频率过高');
return;
function copyCode(e) {
if (e.srcElement.tagName === 'SPAN' && e.srcElement.classList.contains('copy-code')) {
let code = e.currentTarget.querySelector('code');
var text = code.innerText;
if (e.srcElement.textContent === '复制成功') {
console.log('复制操作频率过高');
return;
}
e.srcElement.textContent = '复制成功';
(function (elem) {
setTimeout(() => {
if (elem.textContent === '复制成功') {
elem.textContent = '复制代码'
}
}, 1000);
})(e.srcElement)
Clipboard.copy(text);
}
e.srcElement.textContent = '复制成功';
(function(elem){
setTimeout(() => {
if (elem.textContent === '复制成功') {
elem.textContent = '复制代码'
}
}, 1000);
})(e.srcElement)
Clipboard.copy(text);
}
}
let pres = document.querySelectorAll('pre');
pres.forEach(pre => {
let code = pre.querySelector('code');
let copyElem = document.createElement('span');
copyElem.classList.add('copy-code');
copyElem.textContent = '复制代码';
pre.appendChild(copyElem);
pre.onclick = copyCode
})
let pres = document.querySelectorAll('pre');
pres.forEach(pre => {
let code = pre.querySelector('code');
let copyElem = document.createElement('span');
copyElem.classList.add('copy-code');
copyElem.textContent = '复制代码';
pre.appendChild(copyElem);
pre.onclick = copyCode
})
</script>
<script src="<%= site.customConfig.cdn %>/media/js/motion.js"></script>
<% if (site.customConfig.bgMusic !== 'aplayer') { %>
<script src="https://cdn.jsdelivr.net/gh/cferdinandi/smooth-scroll/dist/smooth-scroll.polyfills.min.js"></script>
<script>
var scroll = new SmoothScroll('a[href*="#"]', {
speed: 500
});
</script>
<% } %>
<!-- <%- include('./search') %> -->
<%- include('./mouse-click') %>
3 changes: 1 addition & 2 deletions templates/_blocks/aplayer.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@
window.addEventListener('load', function() {
let postBody = document.querySelector('#post_body');
let hs = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
hs.forEach(h => {
postBody && hs.forEach(h => {
let helems = postBody.querySelectorAll(h);
if (helems.length > 0) {
helems.forEach(elem => {
elem.id = encodeURI(elem.id);
console.log(elem.id);
})
}
})
Expand Down

0 comments on commit f1a0c4b

Please sign in to comment.