javascript - 单击 div 时滚动到顶部,如果滚动在顶部则滚动到底部

标签 javascript jquery css jquery-animate scrolltop

我在我的网站上使用 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);
            });
        }
    });
}

有人可以帮我吗? 非常感谢,

最佳答案

做类似的事情

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

关于javascript - 单击 div 时滚动到顶部,如果滚动在顶部则滚动到底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26554839/

相关文章:

javascript - 为什么我的两个 split 语句返回的结果不相同?

javascript - 向数组对象添加属性

javascript - 为什么在使用 AJAX 加载 html 后 jQuery 更改函数不起作用?

php - 单击后,div 内容在 jQuery 选项卡中消失了吗?

javascript - 调用一次从 API 获取数据

Javascript:好的部分 - 增强类型中的示例

javascript - 使用 XMLHttpRequest 的 PHP/Mysql 查询无法检索要显示的图像

javascript - Foundation Zurb 无法更改工具提示文本

jquery - 在 HTML5 CSS3 中实现一个椭圆菜单

html - 有没有办法隐藏垂直溢出的表格?