2016-06-15 28 views
0

我正在使用pagescroll(Bootstrap),當我使用#scroll或#test作爲鏈接時它工作正常。但是當我使用在/之前使用Pagescroll ID

<a href="/#experiences" class="page-scroll">Experiences</a> 

它不會。我想這是因爲/的,但我需要它,所以菜單返回,而不是僅僅增加一個#experiences到annother鏈接像

www.test.de/othersite#experiences 

這是我的jQuery。這是可以在jQ中解決嗎?

$('a.page-scroll').bind('click', function(event) { 
    var $anchor = $(this); 
    $('html, body').stop().animate({ 
     scrollTop: ($($anchor.attr('href')).offset().top - 50) 
    }, 1250, 'easeInOutExpo'); 
    event.preventDefault(); 
}); 

回答

1

您可以使用.replace()"/#experiences""#experiences"

實例與您的代碼:

$('a.page-scroll').bind('click', function(event) { 
    var $anchor = $(this); 
    $('html, body').stop().animate({ 
     scrollTop: ($($anchor.attr('href').replace("/", "")).offset().top - 50) 
    }, 1250, 'easeInOutExpo'); 
    event.preventDefault(); 
}); 

注:

如果你正在嘗試做的是有一個這樣的網址: www.test.de/othersite/#experiences 那麼這將無法工作,因爲使用\在HTTP請求中引用文件的路徑,所以這是不可能的。

+0

dang it。我之前嘗試過使用 var str = $(this); var $ res = str.replace(「/」,「」); 但它似乎沒有工作。你的替換工作得很好。謝謝! – Isengo

相關問題