【问题标题】:scroll top when clicking on a div, then scroll bottom if scroll is on top单击 div 时滚动顶部,如果滚动在顶部,则滚动底部
【发布时间】:2014-10-24 19:20:20
【问题描述】:

我在我的网站上使用 Jquery 代码。单击 div (.bottom) 时,我的页面会滚动到顶部。它工作得很好,这是我的代码:

function scroll_cartouche(){
    $('.bottom').click(function() {
  $("html, body").animate({ scrollTop: $(document).height() }, 500);
});

现在我的页面已经滚动到顶部,我想在再次单击 .bottom div 时滚动到底部(返回初始位置)

这是我尝试过的代码,但我有一个错误:

function scroll_cartouche(){
    $(window).scroll(function () {
        if ($(this).scrollTop() != 0) {
            $('.bottom').click(function() {
                $("html, body").animate({ scrollTop: $(document).height() }, 500);
            });
        } else {
            $('.bottom').click(function() {
            $("html, body").animate({ scrollTop: 0 }, 500);
            });
        }
    });
}

谁能帮我解决这个问题? 非常感谢,

【问题讨论】:

    标签: javascript jquery css jquery-animate scrolltop


    【解决方案1】:

    做类似的事情

    $('.bottom').click(function() {
        if ($(window).scrollTop() == 0) {
            $("html, body").animate({ scrollTop: $(document).height() }, 500);
        } else {
            $("html, body").animate({ scrollTop: 0 }, 500);
        }
    });
    

    【讨论】:

    • 感谢@cforcloud,工作正常...我只需要反转条件!非常感谢
    • ... 并且 click 不应添加到 $(window).scroll 函数中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多