zoukankan      html  css  js  c++  java
  • 如何自动在html页面加载时动态改变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 = {
                 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;
        }
        .autohover {
            border: 2px solid blue;
            cursor: pointer;
        }
        .tdsection {
            33%;
            padding-bottom:5px;
        }
    </style>
    
    <body class="bodyclass">
    <div style="100%;height:1000px;overflow-x:hidden;overflow-y:hidden;">
        <table style="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>
  • 相关阅读:
    server2012/win8 卸载.net framework 4.5后 无法进入系统桌面故障解决【转】
    Entity Framework中AutoDetectChangesEnabled為false時更新DB方法
    git常用命令备忘录
    MSSQL日誌傳輸熱備份注意事項
    c#生成唯一编号方法记录,可用数据库主键 唯一+有序
    Angular 隨記
    使用dumpbin命令查看dll导出函数及重定向输出到文件【轉】
    UML类图与类的关系详解【转】
    知識隨記
    session不会过期
  • 原文地址:https://www.cnblogs.com/chunyih/p/4034145.html
Copyright © 2011-2022 走看看