Skip to content

Commit 318ebc7

Browse files
committed
fixed: 修复了一些情况下目录无法跳转的问题
1 parent 30cb9b4 commit 318ebc7

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

source/js/post.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,26 @@ $(function () {
126126
window.history.replaceState(null, null, baseUrl + anchorId);
127127
};
128128

129+
function generateID(str) {
130+
let hash = 0;
131+
for (let i = 0; i < str.length; i++) {
132+
hash = (hash << 5) - hash + str.charCodeAt(i);
133+
// Convert to 32-bit integer
134+
hash |= 0;
135+
}
136+
return 'id-' + hash;
137+
}
138+
129139
/**
130140
* 添加锚点
131141
* @param where 添加到哪里
132142
*/
133143
let addAnchorPoint = (where) => {
134144
// 获取所有的标题
135-
let titles = $(`${where} h1[id],h2[id],h3[id],h4[id],h5[id],h6[id]`);
145+
let titles = $(`${where} h1[id], h2[id], h3[id], h4[id], h5[id], h6[id]`);
136146
titles.each(function () {
137147
// 要添加到界面上的a标签
138-
let a = $(`<a id="user-content-${$(this).attr('id')}" class="anchor" aria-hidden="true" href="#${$(this).attr('id')}"><svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>`);
148+
let a = $(`<a id="user-content-${generateID($(this).attr('id'))}" class="anchor" aria-hidden="true" href="#${generateID($(this).attr('id'))}"><svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>`);
139149
// 在Title上添加a标签
140150
$(this).prepend(a);
141151
// 为a标签添加点击事件
@@ -149,7 +159,7 @@ $(function () {
149159
});
150160
// 为每个title绑定点击事件
151161
titles.on('click', function (event) {
152-
changeUrlAnchor(`#${$(this).attr("id")}`);
162+
changeUrlAnchor(`#${generateID($(this).attr("id"))}`);
153163
let offsetTop = $(this).offset().top - 20;
154164
$("html,body").animate({scrollTop: offsetTop}, 300);
155165
event.preventDefault();
@@ -165,7 +175,7 @@ $(function () {
165175
*/
166176
let generateCatalogs = (from, to, marginSpace, success) => {
167177
// 获取所有的标题
168-
let titles = $(`${from} h1[id],h2[id],h3[id],h4[id],h5[id],h6[id]`);
178+
let titles = $(`${from} h1[id], h2[id], h3[id], h4[id], h5[id], h6[id]`);
169179
// 得到标题数字的最小数字,用来计算每个标题的margin-left间距,例如["h2", "h3"]则返回2
170180
let minTitleNumber = Math.min.apply(null, [...new Set(titles.get().map(value => value.tagName[1]))]);
171181
// 如果没有title的话返回0
@@ -179,22 +189,30 @@ $(function () {
179189
let titleLevel = parseInt($(this).prop("tagName")[1]) - minTitleNumber;
180190
catalogs.append(
181191
`<p class='catalog-item'>
182-
<a href="javascript:void(0)" style="margin-left: ${titleLevel * marginSpace}rem;" data-catalog-target="${$(this).attr('id')}">${$(this).text()}</a>
192+
<a href="javascript:void(0)" style="margin-left: ${titleLevel * marginSpace}rem;" data-catalog-target="${generateID($(this).attr('id'))}">${$(this).text()}</a>
183193
</p>`
184194
);
185195
});
186196
// 添加目录到目标盒子中
187197
$(to).append(catalogs);
188198
// 为目录中每个链接添加一个点击事件
189199
$(`${to} a[data-catalog-target]`).on('click', function (event) {
190-
let titleId = `#${$(this).attr("data-catalog-target")}`;
191-
changeUrlAnchor(titleId);
192-
let offsetTop = $(titleId).offset().top - 20;
200+
let titleId = `${$(this).attr("data-catalog-target")}`;
201+
changeUrlAnchor('#' + titleId);
202+
let offsetTop = $('#user-content-' + titleId).offset().top - 20;
193203
$("html,body").animate({scrollTop: offsetTop}, 300);
194204
event.preventDefault();
195205
});
196206
// 调用用户自定义的成功添加目录后的方法
197207
success();
208+
const urlHash = location.hash;
209+
if (urlHash) {
210+
window.setTimeout(() => {
211+
const titleId = urlHash.substring(1);
212+
let offsetTop = $('#user-content-' + titleId).offset().top - 20;
213+
$("html,body").animate({scrollTop: offsetTop}, 300);
214+
})
215+
}
198216
})();
199217
};
200218

0 commit comments

Comments
 (0)