我做了一个单页网站(+单条帖子)和一个菜单,当点击它们时会滚动到锚点:
if ( window.location.hash ) scroll(0, 0);
setTimeout( function() { scroll(0, 0); }, 1);
$(function() {
$('.scroll').on('click', function(e) {
e.preventDefault();
$('html, body').animate({
scrollTop: $($(this).attr('href')).offset().top
}, 1000, 'swing');
});
if(window.location.hash) {
$('html, body').animate({
scrollTop: $(window.location.hash).offset().top
}, 1000, 'swing');
}
});
var $root = $('html, body');
$('a').on('click', function(event){
var hash = this.hash;
if (hash && this.href.slice(0, -hash.length-1) == location.href.slice(0, -location.hash.length-1)) {
$root.animate({
scrollTop: $(hash).offset().top
}, 'swing', function() {
location.hash = hash;
});
return false;
}
});但是,是否也可以保持此函数的原样,但将#锚隐藏在URL中?
发布于 2017-02-27 23:00:32
我猜你可以阻止hashchange事件:
$(window).on('hashchange', function(e) { e.preventDefault(); });https://stackoverflow.com/questions/42488647
复制相似问题