简体   繁体   English

Animate Scroll to Anchor仅适用于First Link

[英]Animate Scroll to Anchor only works for First Link

I've used this script on other pages and for some reason on this particular page it is only animating the first link. 我在其他页面上使用了此脚本,由于某些原因,在此特定页面上,它只是为第一个链接添加了动画效果。 It is a submenu on multiple pages with the id of #scroll . 它是ID为#scroll多个页面上的子菜单。

Here is the script: 这是脚本:

$("#scroll").click(function(event){     
    event.preventDefault();
    $('html,body').animate({scrollTop:$(this.hash).offset().top}, 500);
});

and here is the html: 这是HTML:

<ul>
    <li><a id="scroll" href="#one">Academy</a></li>
    <li><a id="scroll" href="#two">School</a></li>
    <li><a id="scroll" href="#three">School 2</a></li>
    <li><a id="scroll" href="#four">High School</a></li>

</ul>

with div's below that I want the subnav to scroll too: 与下面的div我也希望subnav滚动:

<div id="one">Lorem Ipsum is simply dummy text.</div>
<div id="two">Lorem Ipsum is simply dummy text.</div>
<div id="three">Lorem Ipsum is simply dummy text.</div>
<div id="four">Lorem Ipsum is simply dummy text.</div>

Could it be conflicts with CSS? 与CSS冲突吗? I can put up here if necessary - just don't want the excess code for no reason! 如果有必要,我可以在这里放-只是无缘无故不要多余的代码!

IDs are meant to be unique, whereas classes can be used multiple times, change the jQuery to this: ID是唯一的,而类可以多次使用,请将jQuery更改为:

$(".scroll").click(function(event){     
    event.preventDefault();
    $('html,body').animate({scrollTop:$(this.hash).offset().top}, 500);
});

and the HTML to this: 以及与此相关的HTML:

<ul>
    <li><a class="scroll" href="#one">Academy</a></li>
    <li><a class="scroll" href="#two">School</a></li>
    <li><a class="scroll" href="#three">School 2</a></li>
    <li><a class="scroll" href="#four">High School</a></li>
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM