简体   繁体   English

单击按钮时从Div滚动到Div

[英]Scroll from Div to Div when clicking a button

I've got 4 sections, and a nextButton, for scrolling. 我有4个部分以及一个nextButton用于滚动。 I want that every time I click that button, the page scrolls and the section nearest to the top, scrolls to the top, so that every time I click, I obtain a sequence of scrolling from the first section to the last, in a reasonable way. 我希望每次单击该按钮时,页面都会滚动,最靠近顶部的部分也会滚动到顶部,这样,每次单击时,我都会获得从第一部分到最后一部分的合理滚动顺序方式。

Firstly I made it with a "next" concept, from section1 to section"n", but it was awful. 首先,我从第1节到第“ n”节采用了“下一个”概念,但是效果很糟。 In fact, if you scrolled with mouse or fingers to the bottom, than you clicked the nextButton button for the first time, the page came up back to the first section. 实际上,如果您用鼠标或手指滚动到底部,而不是第一次单击nextButton按钮,页面就会回到第一部分。

No! 没有! I don't want it like that. 我不要那样

So I tried this new logic approach, that needs more than one If--Else If cycle. 因此,我尝试了这种新的逻辑方法,该方法需要多个If-Else If周期。 But It doens't work: 但是它不起作用:

var $output = $('.output');

$(window).on('scroll', function () {
    var scrollTop = $(window).scrollTop(),
        sec1Offset = $('#sec1').offset().top,
        sec2Offset = $('#sec2').offset().top,
        sec3Offset = $('#sec3').offset().top,
        sec4Offset = $('#sec4').offset().top,

        d1 = (sec1Offset - scrollTop),
        d2 = (sec2Offset - scrollTop),
        d3 = (sec3Offset - scrollTop),
        d4 = (sec4Offset - scrollTop);
    $output.html(d1 + " " + d2 + " " + d3 + " " + d4);
});

var scrollTop = $(window).scrollTop(),
    sec1Offset = $('#sec1').offset().top,
    sec2Offset = $('#sec2').offset().top,
    sec3Offset = $('#sec3').offset().top,
    sec4Offset = $('#sec4').offset().top,

    d1 = (sec1Offset - scrollTop),
    d2 = (sec2Offset - scrollTop),
    d3 = (sec3Offset - scrollTop),
    d4 = (sec4Offset - scrollTop),

    aa = (('d1' > 0)),
    bb = (('d1' <= 0) && ('d2' > 0)),
    cc = (('d1' < 0) && ('d2' <= 0) && ('d3' > 0)),
    dd = (('d1' < 0) && ('d2' < 0) && ('d3' <= 0) && ('d4' > 0));

$('#next-btn').click(function () {
    if ('aa') {
        $('html, body').animate({
            scrollTop: $('#sec1').offset().top
        }, 1300);
        return false;
    } else if ('bb') {
        $('html, body').animate({
            scrollTop: $('#sec2').offset().top
        }, 1300);
        return false;
    } else if ('cc') {
        $('html, body').animate({
            scrollTop: $('#sec3').offset().top
        }, 1300);
        return false;
    } else if ('dd') {
        $('html, body').animate({
            scrollTop: $('#sec4').offset().top
        }, 1300);
        return false;
    }
});

I leave the link to JsFiddle ! 我离开链接到JsFiddle

TNX! TNX!

The issue you're facing with the "first click" problem is because you've wrapped your variables as a string. 您遇到的“首次点击”问题是因为您已将变量包装为字符串。

aa = (('d1' > 0)),
bb = (('d1' <= 0) && ('d2' > 0)),
cc = (('d1' < 0) && ('d2' <= 0) && ('d3' > 0)),
dd = (('d1' < 0) && ('d2' < 0) && ('d3' <= 0) && ('d4' > 0));

and

if ('aa') {
    $('html, body').animate({
        scrollTop: $('#sec1').offset().top
    }, 1300);
    return false;
}
...

In both, you're doing conditional checks against a string. 在这两种方法中,您都在对字符串进行条件检查。 If you remove the quotes, it'll work. 如果删除引号,它将起作用。

The reason it worked the first time is because 'aa' will evaluate true every time. 它第一次工作的原因是因为'aa'每次都会评估为真。

Hope this helps! 希望这可以帮助!

http://jsfiddle.net/PxnAw/20/ http://jsfiddle.net/PxnAw/20/

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

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