热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

jquery自适应布局的简单实例

下面小编就为大家带来一篇jquery自适应布局的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

代码整理 - uix.layout.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* Grace [jQuery.js]
*
* UIX页面布局
* 290353142@qq.com
 
* 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中
 
  <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>
 
html3:嵌套
 
  <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>
 
 
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 &#63; "启用" : "禁用") + "]", 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))) &#63; 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];" westheight="autoHeight" -="" item.otherheight;="" item.ele.css({="" position:="" "absolute",="" left:="" left="" +="" "px",="" right:="" "auto",="" top:="" top="" bottom:="" height:="" display:="" "block",="" margin:="" "0px"="" });="" }="" right="0;" parentbordr.right;="" for="" (var="" i="0;" <s.easts.children.length;="" eastheight="autoHeight" eles.center.css({="" 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:不进行排盘,="" 调用自定义函数,="" undefined="" 自动检测所属版式="" if="" (typeof="" window.uixafterresize="=" "boolean"="" &&="" false)="" tool.log("禁用自动解析回调[window.uixafterresize="=false]");" return;="" "function")="" tool.log("调用自定义回调函数[window.uixafterresize="function]");" window.uixafterresize(sizes);="" "undefined")="" tool.log("使用自动解析回调[window.uixafterresize="undefined]");" n="sizes.norths.children.length;" w="sizes.wests.children.length;" e="sizes.easts.children.length;" c="sizes.centers.children.length;" s="sizes.souths.children.length;" tool.log("解析页面结构"="" "="" north["="" "]="" west["="" east["="" south["="" center["="" "]");="" 判断界面结构,选择合适的回调方法,="" (w="=" 0="" 1)="" $.uix.afterresize1(sizes);="" 1="" $.uix.afterresize2(sizes);="" initpage:="" ()="" log("等待页面加载完成后初始化",="" true);="" $(window.document.body).ready(function="" ($(".uix-layout-container").length="=" 0)="" log("已停止加载[未发现.uix-layout-container]",="" $.uix.tool.log("触发布局[window="" onload]");="" $.uix.layout();="" $(window).bind("resize",="" onresize]");="" $(".uix-layout-north,.uix-layout-south,.uix-layout-east,.uix-layout-west").bind("resize",="" $.uix.tool.log("触发布局[uix-layout-"="" $(this).attr("class")="" log("初始化完毕",="" afterresize1:="" (size)="" 特定结构回调1="" afterresize2:="" 特定结构回调2="" };="" $.extend({="" uix:="" uixlayout="" log("加载完毕",="" $.uix.initpage();="" })();<="" pre="">
</s.wests.children.length;>

以上这篇jquery自适应布局的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。


推荐阅读
  • 本文详细介绍了W3C标准盒模型和IE传统盒模型的区别,探讨了CSS3中box-sizing属性的使用方法及其在布局中的重要性。通过实例分析,帮助读者更好地理解和应用这一关键概念。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 在使用 MUI 框架进行应用开发时,开发者常常会遇到 mui.init() 和 mui.plusReady() 这两个方法。本文将详细解释它们的区别及其在不同开发环境下的应用。 ... [详细]
  • 作为一名专业的Web前端工程师,掌握HTML和CSS的命名规范是至关重要的。良好的命名习惯不仅有助于提高代码的可读性和维护性,还能促进团队协作。本文将详细介绍Web前端开发中常用的HTML和CSS命名规范,并提供实用的建议。 ... [详细]
  • Startup 类配置服务和应用的请求管道。Startup类ASP.NETCore应用使用 Startup 类,按照约定命名为 Startup。 Startup 类:可选择性地包括 ... [详细]
  • 自己用过的一些比较有用的css3新属性【HTML】
    web前端|html教程自己用过的一些比较用的css3新属性web前端-html教程css3刚推出不久,虽然大多数的css3属性在很多流行的浏览器中不支持,但我个人觉得还是要尽量开 ... [详细]
  • 程序员妻子吐槽:丈夫北漂8年终薪3万,存款情况令人意外
    一位程序员的妻子在网上分享了她丈夫在北京工作八年的经历,月薪仅3万元,存款情况却出乎意料。本文探讨了高学历人才在大城市的职场现状及生活压力。 ... [详细]
  • 国内BI工具迎战国际巨头Tableau,稳步崛起
    尽管商业智能(BI)工具在中国的普及程度尚不及国际市场,但近年来,随着本土企业的持续创新和市场推广,国内主流BI工具正逐渐崭露头角。面对国际品牌如Tableau的强大竞争,国内BI工具通过不断优化产品和技术,赢得了越来越多用户的认可。 ... [详细]
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • 本文总结了2018年的关键成就,包括职业变动、购车、考取驾照等重要事件,并分享了读书、工作、家庭和朋友方面的感悟。同时,展望2019年,制定了健康、软实力提升和技术学习的具体目标。 ... [详细]
  • 在计算机技术的学习道路上,51CTO学院以其专业性和专注度给我留下了深刻印象。从2012年接触计算机到2014年开始系统学习网络技术和安全领域,51CTO学院始终是我信赖的学习平台。 ... [详细]
  • 本周信息安全小组主要进行了CTF竞赛相关技能的学习,包括HTML和CSS的基础知识、逆向工程的初步探索以及整数溢出漏洞的学习。此外,还掌握了Linux命令行操作及互联网工作原理的基本概念。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • CSS 布局:液态三栏混合宽度布局
    本文介绍了如何使用 CSS 实现液态的三栏布局,其中各栏具有不同的宽度设置。通过调整容器和内容区域的属性,可以实现灵活且响应式的网页设计。 ... [详细]
  • 本文详细介绍了如何解决Uploadify插件在Internet Explorer(IE)9和10版本中遇到的点击失效及JQuery运行时错误问题。通过修改相关JavaScript代码,确保上传功能在不同浏览器环境中的一致性和稳定性。 ... [详细]
author-avatar
简写青春
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有