2012-07-10 92 views
0

我的頁面上有很多鏈接,它們調用一些jQuery代碼來打開視頻。鏈接列表在頁面上可能會變得很長。當有人點擊一個鏈接時,我想讓它定位到視頻打開頁面的一部分(<div id="player">)。我認爲在<a href="#player"...會做伎倆。我錯過了什麼?html鏈接go to top of page

<div id="player"></div> 
<a href="#player" onclick="DoNav('<?php echo $url; ?>');"> <?php echo $result_videos[$i]["camera_name"]; ?> </a> 

<script type="text/javascript"> 
function DoNav(theUrl) 
{ 
    // only add the player if it doesn't yet exist 
    if($('#myfileplayer').length == 0) { 
    var mydiv = $("#player"); 
    var myvideo = $("<video id='myfileplayer' src='"+ theUrl + "' width='320' height='240' controls></video>"); 
    mydiv.append(myvideo); 
    } else { 
    $('#myfileplayer').attr("src",theUrl); 
    } 

} 
</script> 

上面還挺代碼適用於視頻的第一次點擊,雖然它可以把你的視頻播放器下方,而不是在上面。我第二次下載鏈接列表並點擊一個鏈接時,它什麼都不做。

我也試過在div上面加上<a name="blah"></a>(這是html4),然後把href改爲:<a href="#blah"...。那也行不通。

回答

0

如果我理解正確,您希望您的窗口在單擊鏈接時滾動到div#player頂部?

$("yourLinkSelector").live('click', function(evnt){ 
    evnt.preventDefault(); 
    $('html, body').animate({scrollTop:$("div#player").offset().top - 50}, "slow"); 
    //then your code to open the video 
}); 

然後,你可以當你打開視頻

像這樣的東西應該做的伎倆通過jQuery做到這一點