Javascript 在滚动后删除锚定链接-也适用于其他页面的链接
我已经建立了一个带有平滑滚动的单页网站,在平滑滚动后从URL中删除锚链接。下面是我使用的jQuery:Javascript 在滚动后删除锚定链接-也适用于其他页面的链接,javascript,jquery,Javascript,Jquery,我已经建立了一个带有平滑滚动的单页网站,在平滑滚动后从URL中删除锚链接。下面是我使用的jQuery: $(function() { $('a.page-scroll').on('click', function(event) { var $anchor = $(this); $('html, body').stop().animate({ scrollTop: $($anchor.attr('href')).offset().to
$(function() {
$('a.page-scroll').on('click', function(event) {
var $anchor = $(this);
$('html, body').stop().animate({
scrollTop: $($anchor.attr('href')).offset().top - 60
}, 1000, 'easeInOutExpo');
event.preventDefault();
});
});
在我添加其他页面之前,一切都很好。在另一个外部页面上出现类似
的链接后,我无法将锚链接删除
我到处找,但找不到有效的解决方案
如果链接来自外部页面,我并不完全关心平滑滚动。我最需要的是导航/滚动到主页的id部分(使用偏移量以适应固定导航),并在链接来自外部页面(来自我网站上的其他页面或其他网站)时从浏览器URL窗口中删除锚定链接
我也尝试过这种方法,但它同样只适用于同一页面上id的内部链接:
<script>
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top - 60
}, 1000);
return false;
}
}
});
});
</script>
$(函数(){
$('a[href*=#]:非([href=#])。单击(函数(){
if(location.pathname.replace(/^\/,'')==this.pathname.replace(/^\/,'')和&location.hostname==this.hostname){
var target=$(this.hash);
target=target.length?target:$('[name='+this.hash.slice(1)+']');
if(目标长度){
$('html,body')。设置动画({
scrollTop:target.offset().top-60
}, 1000);
返回false;
}
}
});
});
我也尝试过这个,但运气不好:
<script type="text/javascript">
(function($) {
$(document).ready(function() {
var url=document.URL.split("#");
var ancher=url[1];
$('html, body').animate({
'scrollTop': $('#'+ancher).offset().top - 60
}, 5000);
});
})(jQuery);
</script>
(函数($){
$(文档).ready(函数(){
var url=document.url.split(“#”);
var-ancher=url[1];
$('html,body')。设置动画({
'scrollTop':$('#'+ancher).offset().top-60
}, 5000);
});
})(jQuery);
任何除夕夜的帮助都会非常感激,这样我就可以完成这个项目了 我可能不理解这个问题的范围,但我相信您正在努力使href不会在想要滚动的页面上启动,而是在链接到其他页面而不是页面本身的部分的页面上启动。也许这样的事情适合你:
$(function() {
$('a.page-scroll').bind('click', function(event) {
var $anchor = $(this);
if ($anchor[0].href[0] === '#') {
$('html, body').stop().animate({
scrollTop: $($anchor.attr('href')).offset().top - 60
}, 1000, 'easeInOutExpo');
event.preventDefault();
return false;
} else {
return true;
}
});
});
这样做的目的是要看到href中的主角是a#,这意味着它是指向自身某个部分的链接
让我知道这是否有帮助和/或我是否在正确的轨道上
ps:我把.bind放在那里了,因为我不知道您使用的是哪个版本的jQuery,但是更新的语法是使用.on
新年快乐
只是稍微附加一点,使主页的深度链接可以转到相应的部分,但没有哈希标记:
您可以从window.location中删除该“hash”变量,但如果尝试完全删除哈希标记,则会导致浏览器刷新。这也会导致查看者失去该点(从而删除深层链接的目的)
要更改哈希标记值(保留#),请执行以下操作:
要删除散列标记及其值(清除#及其后面的所有内容),请执行以下操作:
如果它不是很明显,您可以在页面加载时运行它
$(document).ready(function() {
if (typeof(window.location.hash) !== 'undefined' && window.location.hash.length > 0) {
window.location.hash = ''; // will clear the hash anytime someone arrives with a hash tag
}
});
我可能不理解问题的严重程度,但我相信您正在努力使href不会在想要滚动的页面上启动,而是在链接到其他页面而不是页面本身的部分的页面上启动。也许这样的事情适合你:
$(function() {
$('a.page-scroll').bind('click', function(event) {
var $anchor = $(this);
if ($anchor[0].href[0] === '#') {
$('html, body').stop().animate({
scrollTop: $($anchor.attr('href')).offset().top - 60
}, 1000, 'easeInOutExpo');
event.preventDefault();
return false;
} else {
return true;
}
});
});
这样做的目的是要看到href中的主角是a#,这意味着它是指向自身某个部分的链接
让我知道这是否有帮助和/或我是否在正确的轨道上
ps:我把.bind放在那里了,因为我不知道您使用的是哪个版本的jQuery,但是更新的语法是使用.on
新年快乐
只是稍微附加一点,使主页的深度链接可以转到相应的部分,但没有哈希标记:
您可以从window.location中删除该“hash”变量,但如果尝试完全删除哈希标记,则会导致浏览器刷新。这也会导致查看者失去该点(从而删除深层链接的目的)
要更改哈希标记值(保留#),请执行以下操作:
要删除散列标记及其值(清除#及其后面的所有内容),请执行以下操作:
如果它不是很明显,您可以在页面加载时运行它
$(document).ready(function() {
if (typeof(window.location.hash) !== 'undefined' && window.location.hash.length > 0) {
window.location.hash = ''; // will clear the hash anytime someone arrives with a hash tag
}
});
对于平滑滚动的页面,请尝试使用。
它将从浏览器URL窗口中删除锚链接处的标签(无需重新加载页面)
对于平滑滚动的页面,请尝试使用。
它将从浏览器URL窗口中删除锚链接处的标签(无需重新加载页面)
您知道jQuery的
bind
方法从1.7版开始就被弃用了吗?谢谢Robusto。剧本来自另一个人写的剧本。我需要自己开始写这些东西。一切都很及时。谢谢你的帮助。在很大程度上是基于by-谢谢!您知道jQuery的bind
方法从1.7版开始就被弃用了吗?谢谢Robusto。剧本来自另一个人写的剧本。我需要自己开始写这些东西。一切都很及时。谢谢你的帮助。在很大程度上是基于by-谢谢!嗨,非常感谢你的帮助,并为没有说得更清楚而道歉。我有两种情况。第一个,使用问题中列出的第一个jquery函数可以很好地工作,就是在主页内从内部/页上链接平滑滚动,并从浏览器窗口的URL中删除锚链接。第二个场景,我需要帮助,是对从其他页面到主页的链接执行相同的操作。因此,指向from的链接将滚动至#contact,但仅显示在浏览器窗口中。我将删除。绑定并替换为。on-也感谢您。以下是指向我描述的问题的链接。我试图在从中单击返回主页时,从浏览器URL中删除“我做什么”、“公文包”和“联系人”锚链接
// smooth scrolling
function scrollTo(selectors)
{
if(!$(selectors).length) return;
var selector_top = $(selectors).offset().top - 0;
$('html,body').animate({ scrollTop: selector_top }, 'slow');
}
// do scroll and clear the hash tag
$(window).on('load', function(){
if( typeof(location.hash) !== 'undefined' && location.hash.length ) {
scrollTo(location.hash);
history.replaceState(null, null, location.pathname);
}
});