简体   繁体   English

jQuery通过按下按钮向下滚动到每个部分的顶部

[英]Jquery to scroll down to top of each section with button press

This is my code: Js Fiddle 这是我的代码: Js Fiddle

As you can see I have several sections on top of each other with 100% height. 如您所见,我有几个相互重叠的部分,高度均为100%。 I want to know how I can get it so when the user clicks on "learn more" they scroll to the next section, so the top of the section is at the top of the page. 我想知道如何获得它,因此当用户单击“了解更多”时,他们滚动到下一部分,因此该部分的顶部位于页面的顶部。

Normally it would be quite simple as I could do this: 通常,这很简单,因为我可以这样做:

$('body').animate({
scrollTop:$(document).height()
});

However this won't work if the user has already scrolled halfway down on of the sections and then hits the button. 但是,如果用户已经滚动到部分的一半,然后单击按钮,则此方法将无效。 It would also be good if I could use the same function for each button press, instead of having three different functions, one for each different section. 如果我可以为每次按钮按下使用相同的功能,而不是具有三个不同的功能,而每个不同的部分都使用一个功能,那也将是一件好事。

I guess the code would be something like (in pseudo): scroll to top sectiona + 1 我猜代码将是(伪): scroll to top sectiona + 1

with jQuery and smooth scrolling jQuery和平滑滚动

 $('a').click(function(){
        var nextSection = $(this).closest('section').next('section');
        $('html, body').animate({
            scrollTop: $(nextSection).offset().top
        }, 2000);
    });

Why not you pass id's to each section and in href refer to that id like 为什么不将ID传递给每个部分,并在href中引用该ID,例如

<section id="sectionOne">
  <a href="#sectionTwo">Move to section two</a>
</section>

<section id="sectionTwo">
  <a href="#sectionOne">Move to section one</a>
</section>

You can also try the following. 您也可以尝试以下方法。

var amount_to_scroll_by = $(document).scrollTop() + element_to_scroll.getBoundingClientRect().top);

$(document).scrollTop(amount_to_scroll_by); // animate this scroll

Hope this helps :) 希望这可以帮助 :)

Using jquery, you can smoothly scroll to the target. 使用jquery,您可以平滑滚动到目标。

Here is a SAMPLE 这是一个样本

JS: JS:

$("a").click(function(e) {
    e.preventDefault();
    var target = $(this).attr('href');   
    $('html, body').animate({ scrollTop : $(target).offset().top + "px"});    
});

You should first fix up your anchors and use the hash fragments to allow for native navigation between anchors. 您应该首先修复锚点,并使用哈希片段允许锚点之间的本机导航。

I have created a very simple demo for you to understand this (not using your markup to keep it simple). 我创建了一个非常简单的演示供您理解(不使用您的标记使之保持简单)。

Demo: http://jsfiddle.net/abhitalks/9uxGq/15/ 演示: http : //jsfiddle.net/abhitalks/9uxGq/15/

(another demo with your markup: http://jsfiddle.net/abhitalks/9uxGq/19/ ) (带有标记的另一个演示: http : //jsfiddle.net/abhitalks/9uxGq/19/

You need two anchors, one as click link and the other to mark the position of target as anchor. 您需要两个锚点,一个为单击链接,另一个为将目标位置标记为锚点。

For example : 例如

<div>
    <a id="LearnMore1"></a> <!-- Used for marking the anchor with hash fragment -->
    <h2>Sub Heading 2</h2>
    <p>
        Some text content here
    </p>
    <a href="#LearnMore2">Learn More</a> <!-- Used to click to got to next anchor -->
</div>

Note : Of course instead of using a second anchor as a marker, you could use the div (or in your case section ) with an id . 注意 :当然,您可以使用带有iddiv (或在您的案例section )代替使用第二个锚作为标记。 But, an a is better because it is more semantic for content navigation and it means an anchor . 但是, a更好,因为它对于内容导航更具语义,并且表示锚点

Once done, this becomes a fallback for you. 完成后,这将成为您的后备。 Now you can easily implement animations using jQuery etc. 现在,您可以使用jQuery等轻松实现动画。

It would be as simple as this: 就这么简单:

// bind click on anchors (use selectors as per your requirements)
$("a").on("click", function(e) { 
    e.preventDefault(); // prevent the default behaviour
    var nextAnchor = this.hash.replace("#", ""); // get the next marker anchor 
    var gotoPoint = $("#" + nextAnchor).position().top; // get the position of marker
    $('body').animate({ scrollTop: gotoPoint }, 'normal'); // animate the body
});

Alternatively: 或者:

// bind click on anchors (use selectors as per your requirements)
$("a").on("click", function(e) { 
    e.preventDefault(); // prevent the default behaviour
    var nextAnchor = $(this).attr('href'); // get the next marker anchor 
    var gotoPoint = $(nextAnchor).position().top; // get the position of marker
    $('body').animate({ scrollTop: gotoPoint }, 'normal'); // animate the body
});

Now applying this to your use case, the demo becomes: http://jsfiddle.net/abhitalks/9uxGq/19/ 现在将其应用于您的用例,该演示将变为: http : //jsfiddle.net/abhitalks/9uxGq/19/

Hope that helps, and you can work it out in your markup and use-case. 希望有帮助,您可以在标记和用例中解决它。 .

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

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