【问题标题】:scrolling div with jquery用jquery滚动div
【发布时间】:2012-11-17 02:06:38
【问题描述】:

我有一个当前正在滚动的 div,它使用 overflow: auto 和 CSS 中的设置高度。我有一个按钮,想要单击并使 div 稍微向上滚动。我试过使用 animate 和 scrollTop ,但都没有奏效。

我该怎么做?

【问题讨论】:

  • 看,它在这里工作:jsfiddle.net/pVY2m(刚刚在网上找到了这个示例)
  • 你能发布你尝试过的东西吗

标签: jquery


【解决方案1】:

以下 JavaScript/jQuery 将允许您逐渐滚动带有 overflow:auto 的 div:

// The <div> with (overflow:auto)
var scrollBox = $("#leDiv");

// height of the scrollable area
var boxHeight = scrollBox.height();

// height of the scrollable content
var contentHeight = scrollBox[0].scrollHeight;

$('#scrollButton').click(function() {

    // Get the current position of the scroll
    var currentPos = scrollBox.scrollTop();

    // Dont scroll if were at the top    
    if (currentPos <= 0) {
        currentPos = 0;
    } else {
        scrollBox.animate({
            scrollTop: currentPos - 80
        }, 600);
    }
});​

工作示例:http://jsfiddle.net/B5sR9/4/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    相关资源
    最近更新 更多