如何防止页面“跳转"?页面加载时锚是否在URL中? [英] How to prevent page "jump" if anchor is in the URL when page loads?

查看:54
本文介绍了如何防止页面“跳转"?页面加载时锚是否在URL中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果页面加载时锚点位于链接中,如何防止jQuery默认浏览器行为跳到锚点?

How to prevent with jQuery default browser behavior, jump to anchor if anchor is in the link when page loads?

var myLink = document.location.toString();

if (myLink.match('#')) {
 $('html, body').animate({ scrollTop: 0 }, 0); // OR
 $(window).scrollTop();
}

或者....找不到解决方案...已搜索...并已搜索

Or.... can't find solution... searched... and searched

推荐答案

在链接上使用return false/event.preventDefault

use return false / event.preventDefault on your links

var myLink = document.location.toString();

if (myLink.match('#')) {
    $('html, body').animate({ scrollTop: 0 }, 0); // OR
    $(window).scrollTop();
    return false;
}

或者您是说要平滑滚动到您在哈希符号中引用的ID的位置?

Or do you mean that you want o smooth scroll to the position of the ID you are refering to in your hash sign?

在阅读您的评论后,请忽略此消息.

after reading your comments ignore this message.

可能是这样的: http://jsbin.com/ajafor/4#hello

这篇关于如何防止页面“跳转"?页面加载时锚是否在URL中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆