代码之家  ›  专栏  ›  技术社区  ›  182764125216

如何从javascript函数结果设置CSS宽度?

  •  0
  • 182764125216  · 技术社区  · 15 年前

    我在div包装器中有一个GridView,左侧有一些行标题需要始终可见。到目前为止这一切都有效,但我需要包装器有一个可变的宽度来适应浏览器的大小。

    我使用一些javascript根据浏览器宽度得到了所需的包装宽度,但是我不知道如何将这个宽度设置为wrapper.width。
    它不必在页面加载或检查浏览器大小调整后更新包装宽度。

    我拙劣的图解:

    |   |column headers      |
    | R |--------------|      
    | O | gridview data      |
    | W |              |      
    |   |  this part         |
    | H |  will scroll |<--->
    | E |  while the         |
    | A |  Row headers |
    | D |  stay there        |
    | E |              |
    | R |______________      |
    | S | scroll bar   |
    

    asp:(请参见下面的编辑)

    <pseudocode>
     <table>
       <tr><td>row headers</td>
           <td><div class="Wrapper">
                  <asp:GridView>dataBind()</asp:gridview>
           </div></td></tr>
     </table>
    </pseudocode>
    

    css:

    div.Wrapper 
    {
     width:800px;<--this needs to change based on the browser width
     overflow: auto;
    }
    

    javascript代码:

    var Width = window.innerWidth - 275 || document.body.clientWidth - 275
    

    我要么需要一种设置wrapper.width=width的方法,要么需要一种完全不同的、希望更好的方法来实现这一点。

    我试着用一个%作为宽度,但它没有用浏览器窗口作为100%,它占整个GridView的全宽度的百分比,这对我没有任何帮助。

    提前谢谢你的帮助。

    编辑:添加了一些代码

    <script type="text/javascript">
        var Width = window.innerWidth - 275 || document.body.clientWidth - 275;
        var divElement = document.getElementById("wrappist");
    <!--I tried a few different things at this point, here are a couple of them-->
        divElement.offsetWidth = "80px";
        divElement.style.width = Width.toString() + 'px'; 
    </script>
    
    <div runat="server" id="wrappist" class="Wrapper">
      <asp:GridView runat="server" ID="ALPGrid" CssClass="Grid"
           CellPadding="5" GridLines="Both" AutoGenerateColumns="false"
           OnRowDataBound="ALP_RowDataBound" OnRowCreated="ALP_RowCreated"
           DataKeyNames="ItemID">
      <HeaderStyle CssClass="GridHeader" />
      <RowStyle CssClass="Row" />
      <columns>column stuff in here</columns>
      </asp:GridView>
    </div>
    
    2 回复  |  直到 15 年前
        1
  •  1
  •   Adrian    15 年前

    试试这个:

    var Width = window.innerWidth - 275 || document.body.clientWidth - 275
    var divElement = document.getElementById('idOfDiv');
    d.style.width = Width.toString() + 'px';
    

    编辑:忘记解释了。这将设置元素内联div的宽度,以便覆盖类中的样式设置。

        2
  •  1
  •   Michael    15 年前

    纯CSS解决方案怎么样?

    <div class="wrapper">
    <div class="row_headers"></div>
    <div class="grid"></div>
    </div>
    

    css

    .wrapper
    {
    width:800px; /*remove/change */
    }
    .row_headers
    {
    float:left;
    width:100px; /*keep same as .grid margin-left */
    height:300px;
    background-color:#333;
    padding-bottom:16px;
    }
    .grid
    {
    margin-left:100px; /*keep same as .row_headers width */
    height:300px;
    padding-bottom:16px;
    background-color:#ddd;
    overflow:auto;
    }