2016-06-13 75 views
0

我有頁面,我必須播放某些與文本相關的音頻文件,以及何時播放相關的音頻文件,那麼相關文本應該是高亮顯示的。我面臨的問題是,我需要保持突出顯示的div可見的原因。我不知道如何自動向下滾動頁面以保持突出顯示的div可見,以便他可以閱讀它。根據頁面上哪個突出顯示的div自動向下滾動

JSBin Example

HTML

<audio id="myAudio"> 
    <source src="" type="audio/mpeg"> 
</audio> 
<button>Play Audio</button> 
<div class="wrapper"> 
    <span>Rooster</span> 
    <span>Cat</span> 
    <span>Horse</span> 
    <span>Elephant</span> 
    <span>Vulture</span> 
    <span>Duck</span> 
    <span>Rooster</span> 
    <span>Cat</span> 
    <span>Horse</span> 
    <span>Elephant</span> 
    <span>Vulture</span> 
    <span>Duck</span> 
    <span>Rooster</span> 
    <span>Cat</span> 
    <span>Horse</span> 
    <span>Elephant</span> 
    <span>Vulture</span> 
    <span>Duck</span> 
    <span>Rooster</span> 
    <span>Cat</span> 
    <span>Horse</span> 
    <span>Elephant</span> 
    <span>Vulture</span> 
    <span>Duck</span> 
</div> 

我嘗試下面的代碼,但這不工作

$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 
    }); 
    //} 
    }; 
+0

我已經開始探索使用'var x = $(「。playing」)。position();' 'x.top'的選項,我將比較'x.top'窗口的高度和窗口向下滾動。我認爲這應該工作 – Learning

+0

這可能會幫助你:http://stackoverflow.com/questions/6677035/jquery-scroll-to-element –

回答

1

添加以下後var x = $(".playing").position();

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

Here is your updated code

+0

我用下面的代碼\t var wHeight = $(window).height(); \t'var wHalfHeight = wHeight; \t var x = $(「。playing」)。position(); \t var curentSpanPosition = x.top; \t wHalfHeight = wHalfHeight/2; \t \t 如果(curentSpanPosition> wHalfHeight) \t { \t $( 'HTML,身體')動畫({ scrollTop的:curentSpanPosition },1000)。 \t}' – Learning