Skip to content

Instantly share code, notes, and snippets.

@Trovin
Last active July 31, 2019 15:05
Show Gist options
  • Save Trovin/0543f0ffe6b772f783ca16a06937516f to your computer and use it in GitHub Desktop.
Save Trovin/0543f0ffe6b772f783ca16a06937516f to your computer and use it in GitHub Desktop.
JS redirect & anchor scroll
//redirect & anchor scroll
var host = window.location.hostname;
function anchorRedirect() {
var pathToBlogPage = '/blog/#to-blog';
var pathToNewsPage = '/blog/#to-news';
var anchorItemBlog = $('.js-link_blog');
var anchorLinkBlog = $('.js-link_blog').find('a');
var anchorItemNews = $('.js-link_news');
var anchorLinkNews = $('.js-link_news').find('a');
anchorItemBlog.on('click', function (e) {
e.preventDefault();
var host = window.location.hostname;
var pathToRedirect = 'http://' + host + pathToBlogPage;
window.location.replace(pathToRedirect);
});
anchorItemNews.on('click', function (e) {
e.preventDefault();
var pathToRedirect = 'http://' + host + pathToNewsPage;
window.location.replace(pathToRedirect);
});
//check redirect
function checkRedirect() {
var currentLocation = location.href;
var pathToBlogPage = 'http://' + host + pathToBlogPage;
var pathToNewsPage = 'http://' + host + pathToNewsPage;
if(currentLocation == pathToBlogPage) {
var destination = $('#blog').offset().top;
$('html, body').animate({
scrollTop: destination
}, 400);
}
else if(currentLocation == pathToNewsPage) {
var destination = $('#news').offset().top;
$('html, body').animate({
scrollTop: destination
}, 800);
}
}
checkRedirect();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment