根据页面上突出显示的div自动向下滚动

时间:2016-06-13 05:24:48

标签: javascript jquery html

我有一个页面,我必须播放与文本相关的某些音频文件,并且当播放相关的音频文件时,应突出显示相关文本。我面临的问题是我需要保持突出显示的div可见原因。我不知道如何自动向下滚动页面以保持突出显示的div可见,以便他可以阅读它。

JSBin Example

HTML

$aud.onplay = function() {
    $btn.text("Pause Audio");
    playing = true;
    $("div > span:nth-child(" + (audioIndex + 1) + ")").addClass("playing");
    var wHeight = $( window ).height();
    var wHalfHeight = wHeight;
    var x = $(".playing").position();
    var curentSpanPosition = x.top();
    wHalfHeight = wHalfHeight/2;

    //if  ( curentSpanPosition > wHalfHeight )
    //{
    $(".playing").animate({
        scrollTop:  curentSpanPosition
    });
    //}
  };

我尝试了以下代码,但这不起作用

withRouter

1 个答案:

答案 0 :(得分:1)

var x = $(".playing").position();之后添加以下内容:

$('html, body').animate({
        scrollTop: x.top
}, 400);

Here is your updated code

相关问题