滚动到顶部不起作用

Hello I am new to jQuery and I have a problem! I want to scroll to the top, in a page that is loaded via AJAX call.

This works (test):

$(document).on('click', '#top_icon', function() {
    alert('ok');
});

But this is not working (this is what I want to achieve):

$(document).on('click', '#top_icon', function() {
    $('html, body').animate({scrollTop: '0px'}, 800);
});

Try this,

$(document).on('click', '#top_icon', function() {
    $('html, body').animate({'scrollTop': '0px'}, 800);
    return false;
});

scrollTop is undefined in your case.

I am not sure about jQuery, but scrollTop is not a CSS property and therefor might not be part of the properties that can be animated.

But you can create a simple animation for this yourself:

var startValue = 0;
var endValue = 0;
var duration = 800;
var distance = 0;
var velocity = 0;
var step = 0;
var endTime = 0;

var animate = function() {

    var elapsedTime = new Date().getTime() - step;
    document.body.scrollTop += velocity * elapsedTime;
    step = new Date().getTime();

    if (step > endTime)
        document.body.scrollTop = endValue;
    else
        setTimeout(animate, 15);

}

    yourButton.onclick = function() {

        startValue = document.body.scrollTop;
        distance = endValue - startValue;
        velocity = distance / duration;
        step = new Date().getTime();
        endTime = step + duration;
        animate();   

    };

​ Here is a fiddle: http://jsfiddle.net/pXvQG/12/, animate by scrolling down and click the body.