jquery自适应布局

代码整理 - uix.layout.js

/**
* Grace [jQuery.js]
*
*  UIX页面布局
*  [email protected]

*  exp:
*  $.uix.layout();//执行布局
*  class="uix-layout-container";//标识布局容器
*  class="uix_box";//用于调整 布局时将此元素高度铺满父容器(支持设置padding\margin\border)
*  例:

html1:div中
<div class="uix-layout-container">
    <div class="uix-layout-north"></div>
    <div class="uix-layout-north"></div>
    <div class="uix-layout-west"></div>
    <div class="uix-layout-east"></div>
    <div class="uix-layout-center"></div>
    <div class="uix-layout-south"></div>
</div>
html2:body中
<body class="uix-layout-container">
    <div class="uix-layout-north"></div>
    <div class="uix-layout-north"></div>
    <div class="uix-layout-west"></div>
    <div class="uix-layout-east"></div>
    <div class="uix-layout-center"></div>
    <div class="uix-layout-south"></div>
</body>
html3:嵌套
<body class="uix-layout-container">
    <div class="uix-layout-north"></div>
    <div class="uix-layout-north"></div>
    <div class="uix-layout-west"></div>
    <div class="uix-layout-east"></div>
    <div class="uix-layout-center uix-layout-container">
        <div class="uix-layout-north"></div>
        <div class="uix-layout-center"></div>
    </div>
    <div class="uix-layout-south"></div>
</body>

js:
页面结构动态修改后调用 $.uix.layout()即可,若无动态修改则无需做操作

*
*/
(function (undefined) {
    //配置
    var config = {
        useUixLayout: true, //启用
        isDebugger: true, //是否开启日志输出
        version: "V201508171400", //版本
        filename: "uix.layout.js", //脚本名称
        timeout: 500 //布局间隔
    };

    //日志输出
    var log = function () { }
    if (typeof console != "undefined" && console.log) {
        log = function (context, checklog) {
            if (typeof checklog != "undefined" || config.isDebugger)
                console.log("%c" + "[uix.layout]", "color:green;", context);
        }
    }

    //加载日志
    log("加载中", true);
    if (!config.useUixLayout) { log("已停止加载[uix.layout 未启用]", true); return; }
    if (typeof $ == "undefined") { log("已停止加载[需要jQuery支持]", true); return; }
    if (typeof $.uix != "undefined") { log("已停止加载[已加载过]", true); return; }
    log("日志状态[" + (config.isDebugger ? "启用" : "禁用") + "]", true);

    var tool = {
        selecter: ".uix_box", //uix_box高宽自适应
        setAutoBox: function (inputSelecter) {
            var sel = inputSelecter || tool.selecter;
            $(sel).each(function () {
                var o = $(this);
                var p = o.parent();
                var s = tool.getEleSize(o);
                o.height(p.height() - s.otherHeight - tool.getCV(o, ["marginTop", "marginBottom"]));
                o.width(p.width() - s.otherWidth - tool.getCV(o, ["marginLeft", "marginRight"]));
            })
        },
        getCV: function (ele, cn) {
            var s = 0;
            if (typeof cn == "string") cn = [cn];
            $(cn).each(function (i, o) {
                var v;
                s += isNaN(v = parseInt(ele.css(o))) ? 0 : v;
            });
            return s;
        },
        getOtherHeight: function ($obj) { return $obj.outerHeight() - $obj.height() },
        getOtherWidth: function ($obj) { return $obj.outerWidth() - $obj.width() },
        getEleSize: function ($objs) {
            var rev = { height: 0, width: 0, otherHeight: 0, otherWidth: 0, outerHeight: 0, outerWidth: 0, children: [] };
            for (var i = 0; i < $objs.length; i++) {
                var o = $($objs[i]);
                var h = o.height(), w = o.width(), oh = o.outerHeight(), ow = o.outerWidth();
                var c = { height: h, width: w, otherHeight: oh - h, otherWidth: ow - w, outerHeight: oh, outerWidth: ow, ele: o }
                rev.height += c.height;
                rev.width += c.width;
                rev.otherHeight += c.otherHeight;
                rev.otherWidth += c.otherWidth;
                rev.outerHeight += c.outerHeight;
                rev.outerWidth += c.outerWidth;
                rev.children.push(c);
            }
            return rev;
        },
        log: log
    }

    var uixlayout = {
        tool: tool,
        layout: function (cssname) {
            var timeout = function () {
                tool.log("开始布局[" + window.__uixlayoutstate + "]");
                var pares = $(".uix-layout-container");
                pares.each(function (obj, i) {
                    $.uix.initLayout($(this));
                });
                $.uix.setGrid($(".uix_grid")); //自适应表格
                tool.log("布局完毕[" + window.__uixlayoutstate + "]");
                window.__uixlayoutstate = false;
            }

            //如果已经有了一个待执行的操作,则取消之
            if (typeof window.__uixlayoutstate == "number") {
                tool.log("取消布局[" + window.__uixlayoutstate + "]");
                window.clearTimeout(window.__uixlayoutstate);
            }

            //添加一个新操作在待执行序列中
            window.__uixlayoutstate = setTimeout(timeout, config.timeout);
            tool.log("等待布局[" + window.__uixlayoutstate + "] 等待" + config.timeout + "ms");
            return;
        },
        initLayout: function (pare) {
            var parent;
            if (pare[0].tagName.toUpperCase() == "BODY") {
                parent = { height: $(window).height(), width: $(window).width() };
                var marginHeight = tool.getCV($(pare), ["marginTop", "marginBottom"]);
                parent.height -= marginHeight;
            }
            else {
                parent = { height: $(pare[0]).height(), width: $(pare[0]).width() };
                var marginHeight = tool.getCV($(pare), ["marginTop", "marginBottom"]);
                parent.height -= marginHeight;
            }

            parent.element = pare;

            if (pare[0].tagName.toUpperCase() == "BODY") {
                pare.height(parent.height);
            }

            var eles = {
                north: pare.children(".uix-layout-north:visible"),
                south: pare.children(".uix-layout-south:visible"),
                east: pare.children(".uix-layout-east:visible"),
                west: pare.children(".uix-layout-west:visible"),
                center: pare.children(".uix-layout-center:visible")
            }
            var s = {
                parent: parent,
                norths: tool.getEleSize(eles.north),
                souths: tool.getEleSize(eles.south),
                centers: tool.getEleSize(eles.center),
                easts: tool.getEleSize(eles.east),
                wests: tool.getEleSize(eles.west)
            }
            //debugger;
            s.centers.outerHeight = s.parent.height - s.norths.outerHeight - s.souths.outerHeight;
            s.centers.height = s.centers.outerHeight - s.centers.otherHeight;
            s.centers.outerWidth = s.parent.width - s.wests.outerWidth - s.easts.outerWidth;
            s.centers.width = s.centers.outerWidth - s.centers.otherWidth;

            tool.log(s);

            var autoHeight = parent.height - s.norths.outerHeight - s.souths.outerHeight;
            var autoWidth = parent.width - s.wests.outerWidth - s.easts.outerWidth;

            var cheight = s.centers.height;
            var cwidth = s.centers.width;
            eles.north.css({ margin: "0px" });
            eles.south.css({ margin: "0px" });

            var left = 0; //, parentBordr.left
            var top = s.norths.outerHeight; //parentBordr.top; + ;

            //考虑加入前置函数
            //在改变布局前先改变子元素

            for (var i = 0; i < s.wests.children.length; i++) {
                var item = s.wests.children[i];
                var westheight = autoHeight - item.otherHeight;
                item.ele.css({ position: "absolute", left: left + "px", right: "auto", top: top + "px", bottom: "auto", height: westheight + "px", display: "block", margin: "0px" });
                left += item.outerWidth;
            }

            var right = 0; // parentBordr.right;
            for (var i = 0; i < s.easts.children.length; i++) {
                var item = s.easts.children[i];
                var eastheight = autoHeight - item.otherHeight;
                item.ele.css({ position: "absolute", right: right + "px", left: "auto", top: top + "px", bottom: "auto", height: eastheight + "px", display: "block", margin: "0px" });
                right += item.outerWidth;
            }

            eles.center.css({ height: cheight, "marginLeft": s.wests.outerWidth, "marginRight": s.easts.outerWidth });
            tool.log("整体布局完成");

            tool.log("开始检测回调函数  提示:可设置window.uixAfterResize值[false:禁用回调|function:自定义回调|undefined(默认):自动检测]");
            this.resizecontral(s);
            tool.log("回调函数处理完毕");

            $.uix.tool.setAutoBox(); //uix_box 高宽自适应
        },

        resizecontral: function (sizes) {
            //调整布局内常用版式
            //检查用户设置的 uixAfterResize 变量,
            // boolean fale:不进行排盘,
            // function 调用自定义函数,
            // undefined 自动检测所属版式
            if (typeof window.uixAfterResize == "boolean" && window.uixAfterResize == false) {
                tool.log("禁用自动解析回调[window.uixAfterResize==false]");
                return;
            }

            if (typeof window.uixAfterResize == "function") {
                tool.log("调用自定义回调函数[window.uixAfterResize=function]");
                window.uixAfterResize(sizes);
                return;
            }
            if (typeof window.uixAfterResize == "undefined") {
                tool.log("使用自动解析回调[window.uixAfterResize=undefined]");
                var n = sizes.norths.children.length;
                var w = sizes.wests.children.length;
                var e = sizes.easts.children.length;
                var c = sizes.centers.children.length;
                var s = sizes.souths.children.length;
                tool.log("解析页面结构"
                + " north[" + n + "] "
                + " west[" + w + "] "
                + " east[" + e + "] "
                + " south[" + s + "] "
                + " center[" + c + "]");

                //判断界面结构,选择合适的回调方法,
                if (w == 0 && e == 0 && c == 1) {
                    $.uix.afterResize1(sizes);
                }
                if (w == 1 && e == 0 && c == 1) {
                    $.uix.afterResize2(sizes);
                }
                return;
            }
        },

        initpage: function () {
            log("等待页面加载完成后初始化", true);
            $(window.document.body).ready(function () {
                if ($(".uix-layout-container").length == 0) { log("已停止加载[未发现.uix-layout-container]", true); return; }
                $.uix.tool.log("触发布局[window onload]");
                $.uix.layout();
                $(window).bind("resize", function () {
                    $.uix.tool.log("触发布局[window onresize]");
                    $.uix.layout();
                });
                $(".uix-layout-north,.uix-layout-south,.uix-layout-east,.uix-layout-west").bind("resize", function () {
                    $.uix.tool.log("触发布局[uix-layout-" + $(this).attr("class") + " onresize]");
                    $.uix.layout();
                });
                log("初始化完毕", true);
            });
        },

        afterResize1: function (size) {
            //特定结构回调1
        },
        afterResize2: function (size) {
            //特定结构回调2
        }
    };
    $.extend({ uix: uixlayout });
    log("加载完毕", true);
    $.uix.initpage();
})();

demo

时间: 2024-09-29 16:22:09

jquery自适应布局的相关文章

响应式布局和自适应布局的不同

学了前端一段时间了,发现大家都搅浑了自适应布局和响应式布局的差别.现在我来和大家说下它们的不同: 自适应的体验   http://m.ctrip.com/html5/响应式的体验   http://segmentfault.com/ 整理了自己查阅的知识点,给各位一些提示. 起初,网页设计者都会涉及固定宽度的页面,最开始的电脑显示器分辨率种类不多,因为当时本来电脑就少,即使有变化也是800,850,870,880,比如开源中国的网页就是固定宽度为998来定制的,至于为啥是998,我也不知道...

三列自适应布局

自适应布局(四种写法,三种方案) 1.使用position:absolute. <!doctype html> <html> <head> <style> body{ width: 100%; padding: 0; margin: 0; } div{ height: 500px; } .div1{ width: 100px; background: red; float: left; } .div2{ height: 500px; background:

微信小程序新单位rpx与自适应布局

rpx是微信小程序新推出的一个单位,按官方的定义,rpx可以根据屏幕宽度进行自适应,在rpx出现之前,web页面的自适应布局已经有了多种解决方案,为什么微信还捣鼓出新的rpx单位?在解释这个单位前,我们先简单了解一下目前的主流的自适应布局解决方案: 响应式(Responsive web design) rem 流式布局 scale伸缩布局 响应式 响应式布局的问题在于需要维护多个样式文件,维护成本太大,一般的移动H5页面都不会优先考虑. rem rem是近几年比较流行的方案,淘宝移动web端就是

css教程:css自适应布局方法

在进行web前端项目开发(http://www.maiziedu.com/course/web-px/)时,通常情况下会对浏览器进行自适应布局,自适应布局分两类:高度和宽度,方法有很多,我用三列布局举例,我就列几个通俗易懂的例子呗,懂了三列的,两列的原理一样,呵呵哒. 效果图如下:高度自适应--宽度自适应 1,高度自适应布局 原理就是把每个模块设置为绝对定位,然后设置中间自适应的模块的top和bottom属性的值分别为头部模块和底部模块的高,然后中间模块的高度就自适应了.代码如下: html代码

抛砖引玉之宽度自适应布局

抛砖引玉之宽度自适应布局 什么是宽度自适应布局呢? 就是当浏览器窗口大小改变时,浏览器里的元素宽度也随之改变,从而达到自适应布局. 常见的宽度自适应布局有: 1.  两列:左边宽度不变,右边宽度自适应 2.  三列:左右两边宽度不变,中间部分自适应 3.  三列:左右两边宽度自适应,中间部分不变 一.利用div+css实现以上“自适应布局” (1)两列:左边宽度固定,右边宽度自适应 利用div+float+margin,已在随笔‘float剖析’中讲解,具体代码和效果图见下: <!DOCTYPE

两列右侧自适应布局

<div class="g-bd1 f-cb"> <div class="g-sd1"> <p>左侧定宽</p> </div> <div class="g-mn1"> <div class="g-mn1c"> <p>右侧自适应</p> </div> </div> </div> /* 两

静态布局、自适应布局、流式布局、响应式布局、弹性布局简析

近期学习,有很多感想,有时候看似相近的概念,其实意义却不相同.所以学习要针对不同的名词有明确的区分意识. 抽空时间,打算学习下display:flex;本以为就是一个小小的知识点,正式去研究的时候,才发现display:flex;有很多内容,能实现很多效果.比如三栏布局(左右两栏固定,中间栏自适应),圣杯布局. 后来想着经常听到流式布局,自适应布局,响应式布局,他们有什么区别呢,就去搜了许多内容查看,才发现每种布局都有优缺点和不同使用场景. 静态布局:给页面元素设置固定的宽度和高度,单位用px,

移动h5自适应布局

问题一,分辨率Resolution适配:不同屏幕宽度,html元素宽高比和字体大小,元素之间的距离自适应,使用rem单位. 问题二,单位英寸像素数PPI适配:使用rem单位,文字会发虚.段落文字,使用px单位,用media query或js来适配.标题文字可以直接使用rem单位. 问题三,设备像素比例DPR适配:1物理像素在<meta name="viewport" content="initial-scale=1, maximum-scale=1, minimum-s

多栏自适应布局

一.两栏自适应,左右栏高度均200px, 左栏宽度自适应,右栏宽度200px <!--html代码--> <div class="right"></div> <div class="left"></div> * { margin: 0; padding: 0; } /*方案一*/ .right { width: 200px; height: 200px; background-color: orange;