如何在ScrollTO上添加缓动

时间:2012-02-29 06:39:16

标签: jquery

如何在ScrollTo上添加缓动。我想使用这个效果,但有缓和。请在这里查看演示。 http://www.webdesignerwall.com/demo/scrollto-demo/

$(function() {

function scroll(direction) {

    var scroll, i,
            positions = [],
            here = $(window).scrollTop(),
            collection = $('.post');

    collection.each(function() {
        positions.push(parseInt($(this).offset()['top'],10));
    });

    for(i = 0; i < positions.length; i++) {
        if (direction == 'next' && positions[i] > here) { scroll = collection.get(i); break; }
        if (direction == 'prev' && i > 0 && positions[i] >= here) { scroll = collection.get(i-1); break; }
    }

    if (scroll) {
        $.scrollTo(scroll, {
            duration: 750       
        });
    }

    return false;
}

$("#next,#prev").click(function() {        
    return scroll($(this).attr('id'));        
});

$(".scrolltoanchor").click(function() {
    $.scrollTo($($(this).attr("href")), {
        duration: 750
    });
    return false;
});

});
</script>

2 个答案:

答案 0 :(得分:1)

在选项中添加easing属性:

$.scrollTo(element, {
    duration: 200,
    easing: 'swing'
});

答案 1 :(得分:0)

您可以为DOM元素的scrollTop和scrollHeight属性设置动画:

$("#element").animate({"scrollTop": "500px"}, "fast");
$("#element").animate({"scrollLeft": "500px"}, "fast");