简体   繁体   English

JS下一个标签页跳转部分

[英]JS next tab jump section

I have a simple next and previous tab to move between "section". 我有一个简单的下一个和上一个选项卡,可在“部分”之间移动。 When you click the next tab the page moved down and skips the next "section" and goes the one after. 当您单击下一个选项卡时,页面将向下移动并跳过下一个“部分”,然后转到下一个部分。

    var $sec = $("section");
$(".prev, .next").click(function() {
  var y = $sec.filter(function(i, el) {
    return el.getBoundingClientRect().top > 0;
  })[$(this).hasClass("next") ? "next" : "prev"]("section").offset().top;
  $("html, body").stop().animate({
    scrollTop: y
  });
});
      <div data-role="page" id="main">
        <section>
            <div id="home-section" class="section">
                <img src="images/welcome-homepage.jpg" />
            </div>
        </section>
        <section>
            <div id="about-section" class="section">
                <img src="images/about-us.jpg" />
            </div>
        </section>
        <section>
            <div id="service-charter-section" class="section">
                <img src="images/service-charter.jpg" />
            </div>
        </section>
        <section>
            <div id="testionials-section" class="section">
                <img src="images/about-us.jpg" />
            </div>
        </section>
    </div>

This should work. 这应该工作。 It does not account for current scroll position, ie if you scroll halfway down and hit next, it will continue in series from the last item clicked through. 占当前滚动位置,也就是说,如果你中途滚动下来,点击下一步,将在系列继续从最后一项通过点击。 It just makes an array of all sections positions and hops through that as you click prev / next. 单击prev / next时,它只是对所有部分的位置进行排列并进行跳转。

 jQuery(document).ready(function($) { var sectionPosition = 0; var scrollPositions = [] function scroll(y) { $('html').stop().animate({ scrollTop: y }); } $("section").each(function(i, el) { scrollPositions.push(parseInt($(el).offset()['top'])) }) $(".prev, .next").click(function() { if ($(this).hasClass('next')) { scroll(scrollPositions[sectionPosition + 1]); if (sectionPosition < scrollPositions.length) { sectionPosition++; } } else if (sectionPosition > 0) { scroll(scrollPositions[sectionPosition - 1]); sectionPosition--; } }); }); 

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

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