简体   繁体   English

如何为此自定义滚动添加下一个和上一个按钮?

[英]How to add next and previous buttons for this custom scroll?

i have this http://jsfiddle.net/utLytLmt/ with a custom scroll with differents points to make scroll to, i'm using the jQuery.scrollTo plugin but there is not an option to make navigation buttons (the complementary jQuery.serialScroll seems to be made only for when the distance between points is equal). 我有这个http://jsfiddle.net/utLytLmt/与自定义滚动与不同点滚动,我使用jQuery.scrollTo插件但没有选项来制作导航按钮(补充jQuery.serialScroll似乎只在点之间的距离相等时才会产生。

is there a way to make these next and previous buttons to move betweeen the sections? 有没有办法让这些下一个和前一个按钮移动到各个部分之间?

<button id="next">Next</button> | <button id="prev">Previous</button>

Thanks in advance. 提前致谢。

I've modified your fiddle to get it working. 修改了你的小提琴 ,让它运作起来。

I started by changing your code above to this: 我开始将上面的代码更改为:

var scrollPoint = [70, 130, 210, 360, 445, 580, 7130, 816, 933, 1000];
var scrollDuration = 2500;

$("input[type=button]").each(function (index) {
    $(this).click(function () {
        $("body").scrollTo(scrollPoint[index] +  "px", scrollDuration);
    });
});

(rather than using the button index as here, it would be better to use hrefs or data attributes as indexes to your scrollPositions though) (而不是像这里使用按钮索引,最好使用hrefs或数据属性作为scrollPositions的索引)

This allows you to figure out the previous or next point based on on 这允许您基于on计算出上一个或下一个点

var scrollPosition = $(window).scrollTop();
//sometimes scroll position is 69.9995... We want it to be 70:
scrollPosition = Math.round(scrollPosition);

Try it out (I've assumed the "7130px" scroll point is a mistake) 尝试一下 (我假设“7130px”滚动点是错误的)

Here is the same thing with a reduced reduce function (which finds the next/previous point): http://jsfiddle.net/y6rb17n0/3/ reduce函数reduce (找到下一个/前一个点)也是一样的: http//jsfiddle.net/y6rb17n0/3/

Important Note 重要的提示

A body with a height of 1000px can scroll to 1000 - $(window).height() (so your body needs to be larger for this group of scrollPoints ). 高度为1000像素的物体可以滚动到1000 - $(window).height() (因此这组scrollPoints身体需要更大)。

I would suggest using data attributes for the offset and do this: 我建议使用偏移量的data属性并执行此操作:

Check the UPDATED DEMO 检查更新的演示

EXPLANATION 说明

Every time you click on an input or prev/next button, store the target offset to a hidden input. 每次单击输入或上一个/下一个按钮时,将目标偏移存储到隐藏输入。

Then, on the next prev/next button click, check for the stored value, find the input that has a data-top equal to the stored value and find the previous or next element and trigger the scroll with the new element's data-top value. 然后,在下一个上一个/下一个按钮单击,检查存储的值,找到data-top等于存储值的输入并找到上一个或下一个元素并使用新元素的data-top值触发滚动。

UPDATED THE DEMO 更新了演示

HTML HTML

<input type="button" value="Click Me1" class="next1" data-top="70">
....

jQuery jQuery的

$(document).on('click', 'input:not(#stored)', function() {
    var that = $(this);
    var t = that.attr('data-top');
    $('#stored').val(t);
    $("body").scrollTo(t + 'px', 2500);
});

$(document).on('click', '#btnHolder > button', function() {
    var that = $(this);
    var id = that.attr('id');
    var stVal = $('#stored').val();
    //console.log(stVal);
    if (stVal) {
        if (id == 'next') {
            var tp = $('[data-top="' + stVal + '"]').next().attr('data-top');        
        } else if (id == 'prev') {
            var tp = $('[data-top="' + stVal + '"]').prev().attr('data-top');        
        }
        console.log($('[data-top="' + stVal + '"]').next().attr('data-top'));
        $("body").scrollTo(tp + 'px', 2500);
        $('#stored').val(tp);
    }
});

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

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