Skip to content

Commit 2ebdc73

Browse files
committed
Fix anchor jumping on page loads
1 parent 9eefbc5 commit 2ebdc73

1 file changed

Lines changed: 53 additions & 50 deletions

File tree

src/js/skeleton-tabs.js

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,74 @@
11
$(function() {
2-
$('ul.tab-nav li a.button').click(function(e) {
3-
e.preventDefault();
4-
activeTab($(this).attr('href'));
5-
return false;
6-
});
7-
8-
if (location.hash !== '') {
9-
activeTab(location.hash);
10-
} else {
11-
activeTab('#private');
12-
}
2+
setTimeout(() => {
3+
window.scrollTo(0, 0);
4+
}, 0);
5+
$('ul.tab-nav li a.button').click(function(e) {
6+
e.preventDefault();
7+
activeTab($(this).attr('href'));
8+
return false;
9+
});
1310

14-
function activeTab(tab) {
11+
if (location.hash !== '') {
12+
activeTab(location.hash);
13+
} else {
14+
activeTab('#private');
15+
}
1516

16-
let button = $('ul.tab-nav li a.button[href="' + tab + '"]');
17-
let pane = $('div.tab-content div.tab-pane[id="' + tab.slice(1) + '"]');
17+
function activeTab(tab) {
1818

19-
$('ul.tab-nav li a.button.active').removeClass('active');
20-
button.addClass('active');
19+
let button = $('ul.tab-nav li a.button[href="' + tab + '"]');
20+
let pane = $('div.tab-content div.tab-pane[id="' + tab.slice(1) + '"]');
2121

22-
$('div.tab-content div.tab-pane.active').removeClass('active');
23-
pane.addClass('active');
22+
$('ul.tab-nav li a.button.active').removeClass('active');
23+
button.addClass('active');
2424

25-
if (history.pushState) {
26-
history.pushState({}, 'RandomAPI :: View Snippets', `view/snippet${tab}`);
27-
} else {
28-
location.hash = tab;
29-
}
25+
$('div.tab-content div.tab-pane.active').removeClass('active');
26+
pane.addClass('active');
3027

31-
return false;
28+
if (history.pushState) {
29+
history.pushState({}, 'RandomAPI :: View Snippets', `view/snippet${tab}`);
30+
} else {
31+
location.hash = tab;
3232
}
3333

34-
$("select[name='revision']").change(updateRevision);
34+
return false;
35+
}
3536

36-
function updateRevision() {
37-
let self = this;
38-
let code = $(this).parent().siblings('td').children('a[name="codeSnippet"]');
39-
let edit = $(this).parent().siblings('td').children('a[name="editSnippet"]');
37+
$("select[name='revision']").change(updateRevision);
4038

41-
let codeIndex = code.attr('href').lastIndexOf('/');
42-
let editIndex = edit.attr('href').lastIndexOf('/');
39+
function updateRevision() {
40+
let self = this;
41+
let code = $(this).parent().siblings('td').children('a[name="codeSnippet"]');
42+
let edit = $(this).parent().siblings('td').children('a[name="editSnippet"]');
4343

44-
code.attr('href', code.attr('href').slice(0, codeIndex) + '/' + $(this).val());
45-
edit.attr('href', edit.attr('href').slice(0, editIndex) + '/' + $(this).val());
44+
let codeIndex = code.attr('href').lastIndexOf('/');
45+
let editIndex = edit.attr('href').lastIndexOf('/');
4646

47-
let created = $(this).parent().siblings('td').children('span[name="created"]');
48-
let modified = $(this).parent().siblings('td').children('span[name="modified"]');
47+
code.attr('href', code.attr('href').slice(0, codeIndex) + '/' + $(this).val());
48+
edit.attr('href', edit.attr('href').slice(0, editIndex) + '/' + $(this).val());
4949

50-
$.when($.ajax(`ajax/snippetLookup/${$(this).closest('tr').data('ref')}/${$(this).val()}`)).then(function(data) {
51-
if (data.published === 1) {
52-
code.html('View Snippet');
53-
} else {
54-
code.html('Code Snippet');
55-
}
56-
});
57-
}
58-
59-
$('select').each(function () {
60-
var select = $(this);
61-
var selectedValue = select.find('option[selected]').val();
50+
let created = $(this).parent().siblings('td').children('span[name="created"]');
51+
let modified = $(this).parent().siblings('td').children('span[name="modified"]');
6252

63-
if (selectedValue) {
64-
select.val(selectedValue);
53+
$.when($.ajax(`ajax/snippetLookup/${$(this).closest('tr').data('ref')}/${$(this).val()}`)).then(function(data) {
54+
if (data.published === 1) {
55+
code.html('View Snippet');
6556
} else {
66-
select.prop('selectedIndex', 0);
57+
code.html('Code Snippet');
6758
}
6859
});
60+
}
61+
62+
$('select').each(function () {
63+
var select = $(this);
64+
var selectedValue = select.find('option[selected]').val();
65+
66+
if (selectedValue) {
67+
select.val(selectedValue);
68+
} else {
69+
select.prop('selectedIndex', 0);
70+
}
71+
});
6972
});
7073

7174
function revisionPublishPrompt(ref, name, rev) {

0 commit comments

Comments
 (0)