如何自動在html頁面載入時動態改變div等元素的高度和寬度

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   ar   java   sp   div   

這裡需要用到jquery + css。原理是在頁面載入時用javascript去動態改變一個class的高度和寬度。這樣結合javascript能動態擷取瀏覽器/頁面的高度和寬度,從而使得div能動態跟隨瀏覽頁面的大小變化而變化並且不影響高寬比。下面的代碼建立一個手機頁面,每一行三個圖片分佔33%,每個圖片div的高和寬會隨著瀏覽器的大小變化而自適應。

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />    <title>test photoview</title></head><script type="text/javascript" src="js/jquery-1.11.1.min.js"></script><script type="text/javascript">    function pageload()    {        var size = {            width: window.innerWidth || document.body.clientWidth,            height: window.innerHeight || document.body.clientHeight        }        var cw = (size.width - 60) / 3;        $(‘.autowidth‘).css({ ‘width‘: cw + ‘px‘ });        $(‘.autowidth‘).css({ ‘height‘: cw + ‘px‘ });    }    window.onload = pageload;</script><style>    .bodyclass{        background-color:#dedbdb;    }    .studentblock {        font-family:微軟雅黑;    }    .studentblock:hover {        border:2px solid blue;        border-color:blue;        cursor:pointer;    }    .autowidth {        height:100px;        background-size:cover;        margin-left:8px;        margin-top:0px;    }    .autowidth:hover {        border: 2px solid blue;        cursor: pointer;    }    .tdsection {        width:33%;        padding-bottom:5px;    }</style><body class="bodyclass"><div style="width:100%;height:1000px;overflow-x:hidden;overflow-y:hidden;">    <table style="width:100%">        <tr>            <td class="tdsection">                <div class="autowidth" style="background-image:url(‘image/people1.jpg‘);"></div>                <div style="background-color:#ffffff;margin-left:8px;font-family:微軟雅黑;text-align:center;">春遊</div>            </td>                                                                    <td class="tdsection">                <div class="autowidth" style="background-image:url(‘image/people1.jpg‘);""></div>                <div style="background-color:#ffffff;margin-left:8px;align-content:center;font-family:微軟雅黑;text-align:center;">春遊</div>            </td>                                                                    <td class="tdsection">                <div class="autowidth" style="background-image:url(‘image/people1.jpg‘);"></div>                <div style="background-color:#ffffff;margin-left:8px;align-content:center;font-family:微軟雅黑;text-align:center;">春遊</div>            </td>                                                  </tr>    </table></div></body></html>

 

如何自動在html頁面載入時動態改變div等元素的高度和寬度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.