Skip to content

Commit

Permalink
fix bug of page shifting when change to new tab
Browse files Browse the repository at this point in the history
  • Loading branch information
chiaweh2 committed Jul 10, 2024
1 parent 69cd870 commit 9841f86
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions tabHashEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,34 @@ $('div.newsContainerDiv > a[href^="#"]').on('click', function (e) {
$('li').removeClass('active');
$('a[href="' + targetTabId + '"]').parent().addClass('active')

// This prevents the page from scrolling down to where it was previously.
if ('scrollRestoration' in history) {
history.scrollRestoration = 'manual';
}
// This is needed if the user scrolls down during page load and you want to make sure the page is scrolled to the top once it's fully loaded. This has Cross-browser support.
window.scrollTo(0,0);

});


$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
var target = $(e.target).attr("href") // activated tab
window.location.hash = $(e.target).attr('href');
// console.log(target);
$(document).ready(function() {
$('a[data-toggle="tab"]').on('show.bs.tab', function(e) {
const activeTabId = $(e.target).attr('href');
localStorage.setItem('activeTab', activeTabId);
});

var activeTabStore = localStorage.getItem('activeTab');
if (activeTabStore) {
$(activeTabStore).tab('show');
}
});

// old code that will have the hash in the url but somehow casuing the page to scroll down automatcally
// $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
// var target = $(e.target).attr("href") // activated tab
// window.location.hash = $(e.target).attr('href');
// // console.log(target);
// });



Expand Down

0 comments on commit 9841f86

Please sign in to comment.