简体   繁体   English

jQuery / Javascript滚动到只有类元素的页面中的div

[英]Jquery / Javascript scroll to div in page with only class element

I'm know you can scroll to different parts of a webpage using id and name elements, but I've got some different here. 我知道您可以使用id和name元素滚动到网页的不同部分,但是这里有些不同。 I'm using a WordPress theme from Croma called Dumanis and their div only offer classes, not Id's. 我正在使用来自Croma的一个名为Dumanis的WordPress主题,并且他们的div仅提供类,而不提供ID。 So I'm trying to figure a way to click on something in the navigation menu to take people to the proper div class element. 因此,我试图找到一种在导航菜单中单击某些内容的方法,以使人们进入适当的div类元素。 How would I accomplish that? 我该怎么做? I see a lot of people refer to scrollTop but in this case, I don't want it to scroll completely to the top or the top social media bar will cover part of the div up. 我看到很多人都提到scrollTop,但是在这种情况下,我不希望它完全滚动到顶部,否则顶部的社交媒体栏会覆盖div的一部分。 What to do? 该怎么办?

Here's the page so far: http://www.aishla.com/blog/2014/my-aish/ 到目前为止,这是此页: http : //www.aishla.com/blog/2014/my-aish/

You can use .eq() to grab the elements from the UL list that contains the links, then you can use contains() to grab the text of the H2 elements that divide the sections, here's a mockup: 您可以使用.eq()来获取包含链接的UL列表中的元素,然后可以使用contains()来获取将各部分分开的H2元素的文本,这是一个模型:

jQuery('#sub-nav a').click(function(e){
    e.preventDefault();
    jQuery(window).scrollTop(jQuery('h2:contains("Contact")').offset().top - jQuery('h2:contains("Contact")').outerHeight());
});

Of course, this just takes every link to the contact portion, but you can easily abstract from this. 当然,这仅占用了到contact部分的每个链接,但是您可以轻松地从中抽象出来。

Plug it into the console and you'll see. 将其插入控制台,您将看到。

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

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