2009-08-20 265 views
2

這是怎麼發生的,它不會在窗戶的乘坐側創建垂直5px寬的酒吧?如何通過JQuery修改div元素的CSS屬性(高度寬度)?

<html> 
<head runat="server"> 
    <script type="text/javascript" src="jquery.js"></script> 
    <script type="text/javascript"> 

     $(document).ready(function() { 
      $("#scrollBar").css({ 
       height: $(window).height, 
       width: 5, 
       position: "absolute",     
       top: 0, 
       left: $(window).width - 5, 
       margin: 0 
      }); 
     }); 

    </script> 
</head> 
<body> 
    <div id="scrollBar" style="background-color: Red">test 
    </div> 
</body> 
</html> 

回答

6

使用這個代替:

我發現幾個問題:

  • 代替$(window).height,使用$(window).height()
  • 代替left: $(window).width - 5,使用right: 0

    $(document).ready(function() { 
        $("#scrollBar").css({ 
         height: $(window).height(), 
         width: 5, 
         position: "absolute",     
         top: 0, 
         right: 0, 
         margin: 0 
        }); 
    }); 
    
+0

「正確」的唯一問題是,IE經常將它分解並關閉1px。 – Pat 2009-08-20 02:16:36

+0

填充/邊距有幫助嗎?我現在沒有IE,所以我無法測試它。 – Randell 2009-08-20 02:24:33

+0

在ie8.works上測試過。 – krishna 2009-08-20 02:55:45

0

相反的left: $(window).width - 5,你可以嘗試right: 0

編輯:是的,這是非常愚蠢的。但是,如果我沒有弄錯,那麼也應該引用它們,如'width':'5px',.

$("#scrollBar").css({ 
    'height': $(window).height(), 
    'width': '5px', 
    'position': 'absolute', 
    'top': 0, 
    'right': 0, 
    'margin': 0 
    }); 
1

,你可能想要把 「爲5px」,而不是5的寬度和$(窗口).height()+ 「PX」 的高度和($(窗口).WIDTH() - 5)+「px」爲左側。

1

通過閱讀jQuery文檔,您可以看到需要將css屬性及其值作爲字符串值。

 
$("#scrollBar").css({ 
       'height': $(window).height(), 
       'width': '5px', 
       'position': 'absolute',     
       'top': '0', 
       'left': $(window).width() - 5, 
       'margin': '0' 
      }); 

http://docs.jquery.com/CSS/css#properties

我真的不知道艱難的你將如何顯示這些計算值。

PS:Jonathas是正確的高度和寬度是功能,而不是屬性。

相關問題