jquery - 在没有滚动条的固定div内创建可滚动div

标签 jquery html css scroll

我在固定 div 内创建了一个滚动 div,如下:

http://jsfiddle.net/Newtt/9zRHx/

.scrollable {
    width: 200px;
    height: 200px;
    background: #333;
    overflow: scroll;
}
.fixed {
    position: absolute;
    top: 180px;
    width: 200px;
    height: 20px;
    background: #fa2;
}

但是,我想创建一个滚动按钮。现在,JQuery 可以在 Fiddle 上运行,也可以在我希望它运行的网站上运行:

http://leonardorestaurant.in/testing/services.html

JQuery:

$(document).ready(function () {
    $("#down").click(function () {
        $('.scrollable').animate({
            scrollTop: 220
        }, "slow");
    });
    $("#up").click(function () {
       $('.scrollable').animate({
            scrollTop: -200
        }, "slow");
    });
});

但是,它并没有按照我想要的方式工作。每次单击必要的箭头时,我都需要它向下或向上滚动。

在当前方法中,它仅滚动一次,之后不再继续。是否可能是因为我将滚动量设置得太大?另外,是否可以隐藏滚动条但保留滚动操作?

最佳答案

使用 +=/-= 指示动画方法添加该金额,而不是将其设置为最终值。

$(document).ready(function () {
    $("#down").click(function () {
        $('.scrollable').animate({
            scrollTop: '+=220'
        }, "slow");
    });
    $("#up").click(function () {
       $('.scrollable').animate({
            scrollTop: '-=220'
        }, "slow");
    });
});

演示位于 http://jsfiddle.net/9zRHx/1/


了解更多信息:http://api.jquery.com/animate/#animation-properties

.animate()

Animated properties can also be relative. If a value is supplied with a leading += or -= sequence of characters, then the target value is computed by adding or subtracting the given number from the current value of the property.

关于jquery - 在没有滚动条的固定div内创建可滚动div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20249016/

相关文章:

HTML 页面更改未显示在用户的 Chrome 浏览器中

jquery - 根据另一个 div 设置一个 div 的宽度

jquery - 如何存储请求 "offline"以便稍后执行?

JQuery 选择器帮助

jQuery Mobile 视频在转换后闪烁

HTML - 使行内 block 元素看起来很小,但看起来很大

javascript - 专注于表格单元格/列

jQuery - 专注于 TR

javascript - 无法清除 Angular 中 ngDialog 模板中的表单输入值

javascript - 在窗口大小调整时调整 iframe 的宽度