Javascript scrillTop js脚本工作不正常
我在我的网站上添加了用于内部链接滚动手势的脚本:Javascript scrillTop js脚本工作不正常,javascript,jquery,google-chrome,firefox,scrolltop,Javascript,Jquery,Google Chrome,Firefox,Scrolltop,我在我的网站上添加了用于内部链接滚动手势的脚本: $(document).ready(function() { // Pour tous les liens commençant par #. $("a[href^='#']").click(function (e) { // On annule le comportement initial au cas ou la base soit différente de la page courante.
$(document).ready(function() {
// Pour tous les liens commençant par #.
$("a[href^='#']").click(function (e) {
// On annule le comportement initial au cas ou la base soit différente de la page courante.
e.preventDefault();
// On ajoute le hash dans l'url.
window.location.hash = $(this).attr("href");
$('body').animate({
scrollTop: $(window.location.hash).offset().top - 72
}, 'slow');
return false
});
});
这个脚本并不完美。在第一次clic中,我们选择了正确的位置,但滚动不起作用。但是,从第二次单击开始,滚动可以正常工作
编辑:
我的html代码:
<a class="link-icon visible-desktop" href="#home-keyNumbers-section"><img src="{{ asset('/bundles/visualimmersionsite/images/icons/Arrowhead-Down-256-hover.png') }}" alt="Scroller pour mieux nous connaître" /> </a>
<section id="home-keyNumbers-section" name="home-keyNumbers-section" class="designed-section-2 ">
<div class="container">
{% include 'VisualImmersionSiteBundle:Site:templates/key_numbers_template.html.twig' %}
</div>
</section>
{%include'VisualImmersionSiteBundle:Site:templates/key_numbers_template.html.twig%}
你有主意吗
谢谢您的帮助:)您的代码应该是:
$(document).ready(function () {
// Pour tous les liens commençant par #.
$("a[href^='#']").click(function (e) {
$('body, html').animate({
scrollTop: $($(this).attr("href")).offset().top - 72
}, 'slow', function () {
// On ajoute le hash dans l'url.
window.location.hash = $(this).attr("href");
});
return false
});
});
您的代码应该是:
$(document).ready(function () {
// Pour tous les liens commençant par #.
$("a[href^='#']").click(function (e) {
$('body, html').animate({
scrollTop: $($(this).attr("href")).offset().top - 72
}, 'slow', function () {
// On ajoute le hash dans l'url.
window.location.hash = $(this).attr("href");
});
return false
});
});
您的代码应该是:
$(document).ready(function () {
// Pour tous les liens commençant par #.
$("a[href^='#']").click(function (e) {
$('body, html').animate({
scrollTop: $($(this).attr("href")).offset().top - 72
}, 'slow', function () {
// On ajoute le hash dans l'url.
window.location.hash = $(this).attr("href");
});
return false
});
});
您的代码应该是:
$(document).ready(function () {
// Pour tous les liens commençant par #.
$("a[href^='#']").click(function (e) {
$('body, html').animate({
scrollTop: $($(this).attr("href")).offset().top - 72
}, 'slow', function () {
// On ajoute le hash dans l'url.
window.location.hash = $(this).attr("href");
});
return false
});
});
这仍然会导致视口滚动;这或多或少相当于单击链接。您可以使用推送新URL,而不会导致其被“访问”:
history.pushState(null, null, $(this).attr('href'));
如果您需要支持IE8及更早版本,则可以保持当前滚动位置:
var originalScroll = window.scrollY; // Grab window.scrollX if applicable
window.location.hash = $(this).attr("href");
window.scrollY = originalScroll;
不过,您可能必须将window.scrollY=originalScroll
放在设置超时中,超时时间为0
这仍然会导致视口滚动;这或多或少相当于单击链接。您可以使用推送新URL,而不会导致其被“访问”:
history.pushState(null, null, $(this).attr('href'));
如果您需要支持IE8及更早版本,则可以保持当前滚动位置:
var originalScroll = window.scrollY; // Grab window.scrollX if applicable
window.location.hash = $(this).attr("href");
window.scrollY = originalScroll;
不过,您可能必须将window.scrollY=originalScroll
放在设置超时中,超时时间为0
这仍然会导致视口滚动;这或多或少相当于单击链接。您可以使用推送新URL,而不会导致其被“访问”:
history.pushState(null, null, $(this).attr('href'));
如果您需要支持IE8及更早版本,则可以保持当前滚动位置:
var originalScroll = window.scrollY; // Grab window.scrollX if applicable
window.location.hash = $(this).attr("href");
window.scrollY = originalScroll;
不过,您可能必须将window.scrollY=originalScroll
放在设置超时中,超时时间为0
这仍然会导致视口滚动;这或多或少相当于单击链接。您可以使用推送新URL,而不会导致其被“访问”:
history.pushState(null, null, $(this).attr('href'));
如果您需要支持IE8及更早版本,则可以保持当前滚动位置:
var originalScroll = window.scrollY; // Grab window.scrollX if applicable
window.location.hash = $(this).attr("href");
window.scrollY = originalScroll;
您可能必须将window.scrollY=originalScroll
放入一个setTimeout
中,超时时间为0
。您能提供一个JSFIDLE吗?您的html是什么样子的?@nils对不起,我不知道JSFIDLE是什么:要支持FF,您应该使用$('body,html')。animate(…)代码>。顺便说一句,动画制作完成后,您应该在URL中添加哈希,而不是在之前。您可以提供示例代码,以便我们能够查找任何错误:)您可以提供JSFIDLE吗?您的html看起来像什么?@nils对不起,我不知道JSFIDLE是什么:要支持FF,您应该使用$('body,html')。animate(…)代码>。顺便说一句,动画制作完成后,您应该在URL中添加哈希,而不是在之前。您可以提供示例代码,以便我们能够查找任何错误:)您可以提供JSFIDLE吗?您的html看起来像什么?@nils对不起,我不知道JSFIDLE是什么:要支持FF,您应该使用$('body,html')。animate(…)代码>。顺便说一句,动画制作完成后,您应该在URL中添加哈希,而不是在之前。您可以提供示例代码,以便我们能够查找任何错误:)您可以提供JSFIDLE吗?您的html看起来像什么?@nils对不起,我不知道JSFIDLE是什么:要支持FF,您应该使用$('body,html')。animate(…)代码>。顺便说一句,动画完成后,您应该在URL中添加哈希,您可以提供示例代码,以便我们能够查找任何错误:)firefox可以:)但是我仍然有滚动问题firefox可以:)但是我仍然有滚动问题firefox可以:)但是我仍然有滚动问题firefox可以:)但是我仍然有滚动问题firefox可以:)但是我仍然有滚动问题很棒,它现在可以工作了!但我不明白是什么造成的差异:你认为是在脚本中已经访问过的链接,是正确的吗?@ MeurSultff:设置<代码>窗口。位置。哈希< /C> >就像访问链接;默认情况下,使用指向真实元素的哈希访问链接将滚动到该元素<代码>历史。pushState
只更改历史状态;它不会导航到您传递给它的URL。(这也不仅仅适用于散列;您可以将其用于任何本地URL。)太好了,它现在可以工作了!但我不明白是什么造成的差异:你认为是在脚本中已经访问过的链接,是正确的吗?@ MeurSultff:设置<代码>窗口。位置。哈希< /C> >就像访问链接;默认情况下,使用指向真实元素的哈希访问链接将滚动到该元素<代码>历史。pushState
只更改历史状态;它不会导航到您传递给它的URL。(这也不仅仅适用于散列;您可以将其用于任何本地URL。)太好了,它现在可以工作了!但我不明白是什么造成的差异:你认为是在脚本中已经访问过的链接,是正确的吗?@ MeurSultff:设置<代码>窗口。位置。哈希< /C> >就像访问链接;默认情况下,使用指向真实元素的哈希访问链接将滚动到该元素<代码>历史。pushState
只更改历史状态;它不会导航到您传递给它的URL。(这也不仅仅适用于散列;您可以将其用于任何本地URL。)太好了,它现在可以工作了!但我不明白是什么造成的差异:你认为是在脚本中已经访问过的链接,是正确的吗?@ MeurSultff:设置<代码>窗口。位置。哈希< /C> >就像访问链接;默认情况下,使用指向真实元素的哈希访问链接将滚动到该元素<代码>历史。pushState
只更改历史状态;它不会导航到您传递给它的URL。(这也不仅仅适用于散列;您可以将其用于任何本地U