2011-05-02 62 views
0

我有我的頁面上運行此腳本通過更改其'左'css屬性部分移動屏幕div。它發生在頁面加載如果頁面請求包含查詢字符串:更改JavaScript中相對div的左側位置不工作在Firefox /鉻

 function pageLoad() { 

     var isQueryRequest = isQueryPostback(); // check for query string 

     if(isQueryRequest) 
     { 
      document.getElementById("container").style.left = -340; // not working 

      document.getElementById("selectorText").innerHTML = "<< Show Query View"; 
      document.getElementById("querySelector").onclick = slideOut; 
     } 
     else 
     { 
      document.getElementById("selectorText").innerHTML = "<< Hide Query View"; 
      document.getElementById("querySelector").onclick = slideIn; 
     } 
    } 


    <div id="container" style="position:relative; left:30px;" > 
     // contains other divs 
    </div> 

伊夫確認執行進入第一如果塊,但該行中沒有Firefox或Chrome執行。它在IE中執行得很好。查看螢火蟲時,頁面上沒有明顯錯誤。

不知道它是否相關,但運行在.aspx頁面。

編輯 - 中的slideIn()和滑出式()如下:

function slideOut() { 

    var slidingDiv = document.getElementById("container"); 

    var stopPosition = 30; 

    if (parseInt(slidingDiv.style.left) < stopPosition) { 
     slidingDiv.style.left = parseInt(slidingDiv.style.left) + 28 + "px"; 
     setTimeout(slideOut, 1); 
    } 

    document.getElementById("selectorText").innerHTML = "<< Hide Query View"; 
    document.getElementById("querySelector").onclick = slideIn; 
    } 

    function slideIn() { 

    var slidingDiv = document.getElementById("container"); 
    var stopPosition = -340; 

    if (parseInt(slidingDiv.style.left) > stopPosition) { 
     slidingDiv.style.left = parseInt(slidingDiv.style.left) - 28 + "px"; 
     setTimeout(slideIn, 1); 
    } 

    document.getElementById("selectorText").innerHTML = "<< Show Query View"; 
    document.getElementById("querySelector").onclick = slideOut; 
    } 
+0

你可以發佈'slideOut()'和'slideIn()'函數嗎? – Blender 2011-05-02 18:01:16

+0

剛剛意識到我可能需要包含'px'? – 2011-05-02 18:06:07

+1

在'px'中輸出'style.left'還是以整數形式輸出?我建議使用JavaScript庫來處理這類東西(jQuery?),因爲你有點通過手動動畫重塑輪子。這是一件好事,但在許多情況下不切實際。 – Blender 2011-05-02 18:09:00

回答

0

這可能會給你帶來麻煩:

.innerHTML = "<< Show Query View"; 

逃生者低於跡象,因爲它們可能會呈現爲HTML,這「會搞砸了你的佈局:

.innerHTML = "&lt;&lt; Show Query View"; 
0

把值在雙引號,如:

{ 
.... 
.......style.left="300px"; 
.... 
}