2016-07-27 31 views
0

我有一個html頁面的登錄和主頁。我有登錄用戶的條件。例如當他/她使用臨時密碼登錄時,重定向頁面的url將爲home.html?change=yes#focuschange然後在我的主頁上當url change == "yes"時,它將隱藏其他div id(home-wrapper)。然後顯示focuschange div ID。並應滾動到所述div ID。但是在加載主頁時,它只是隱藏其他div,並顯示focuschange div,但不顯示滾動。它只顯示在底部。當我重新加載頁面時,它現在將按照我的意願滾動​​。這是爲什麼?我嘗試使用scrollIntoView()animate scrollTop但同樣的問題。當Div id剛剛顯示在屏幕上時滾動頁面id:無顯示:block

我的HTML的結構是這樣的:

<div class="page-content"> 
    <div class="content-block"> 
     <div id="home-wrapper"></div> //this will hide upon page load (by default it will show) 
     <div id="profile-wrapper"> // this will show upon page load (by default it will hide) 
      <div id="info"></div> 
      <div id="focuschange"> 
       <form></form> 
      </div> 
     </div> 
    </div> 
</div> 

而且這是在主頁上我的jQuery函數:

function GetURLParameter(sParam){ 
       var sPageURL = window.location.search.substring(1); 
       var sURLVariables = sPageURL.split('&'); 
       for (var i = 0; i < sURLVariables.length; i++) 
       { 
        var sParameterName = sURLVariables[i].split('='); 
        if (sParameterName[0] == sParam) 
        { 
         return sParameterName[1]; 
        } 
       } 

      } 

      var urlvar = GetURLParameter('change'); 
      if (urlvar == 'yes'){ 

       $('#home-wrapper').hide(); 
       $('#profile-wrapper').show(); 

       setTimeout(function(){ 
       $("#newpass").focus(); 
       }, 200); 
      } 

有滾動DIV ID的問題時,它只是顯示在加載頁面?

在這種情況下應該採取什麼方法?

+0

我可能會誤解你的問題是什麼,但它看起來像你試圖使用「錨」與哈希以實現平滑滾動。是這樣嗎? – Piercey4

+0

@ Piercey4是的,我只是想將頁面滾動到我的網址上的ID,這是'#focuschange' –

回答

0

你可能想改變你的那一行;

var urlvar = GetURLParameter('changepassword'); 

var urlvar = GetURLParameter('change'); 
+0

對不起,我複製了錯誤的變量。我更新了我的帖子。 –

0

既然你已經使用jQuery你可以使用jQuery來進行滾動。

這是一個css-tricks鏈接,它完全自動鏈接點擊。 https://css-tricks.com/snippets/jquery/smooth-scrolling/

如果你需要的是這樣的事情發生在頁面加載,那麼你可以這樣做:

$('html, body').animate({ 
    scrollTop: $('#focuschange').offset().top 
}, 1000); 
+0

我已經試過了,它只在我重新載入頁面時才起作用。我想這也許是因爲它只顯示了之前隱藏的div ID?你怎麼看? –

+0

您是否將滾動部分包裝在$(document).ready(function(){})? 它似乎是您試圖滾動到一個尚未在DOM中的元素... – user1555320