li 水平排列并自动填满 ul
找了li 如何水平排列并自动填满 ul,同时 li 宽度平均?资料,里面有提到"请用js动态计算保证兼容性",
因为我想实现的是,水平滚动条,ul的上级div是固定的宽度1000px, 我就想到用jquery 获取每个li的高度,ul的宽度等于每个li的宽度.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
<title>index.html</title>
<meta name="description" content="Hello world"> <!-- jQuery 1.7.2+ or Zepto.js 1.0+ -->
<script src="http://ajax.useso.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <style type="text/css">
#category{
width:1000px;
height:54px;
border:1px solid red;
overflow-x:auto;
overflow-y:hidden;
}
#category ul{
margin:0px;
padding:0px;
list-style:none;
/**float:left;和overflow:hidden;是为了让ul的宽度为实际宽度,具体的区别,我还待学习**/
overflow:hidden;
}
#category ul li{
float:left;
margin:0px 10px;
padding:10px;
border:red;
} </style> </head>
<body>
<div id="category">
<ul>
<li>Test</li>
<li>Test12321321</li>
<li>Test111</li>
<li>Test11</li>
<li>Test</li>
<li>Test</li>
<li>Test</li>
<li>Test</li>
<li>Test</li>
<li>Test</li>
<li>Test</li>
<li>Test</li>
<li>Test</li>
<li>Test</li>
</ul>
</div>
<script type="text/javascript">
jQuery(function($){
var ul_width = 0 ;
$("#category ul li").each(function(index,value){
var width = $(value).width();
//40是padding-left + padding-right + margin-left + margin-left
ul_width += (width + 40);
//console.log(width);
});
$("#category ul").width(ul_width);
}); </script>
</body>
</html>
最终的效果图是:

li 水平排列并自动填满 ul的更多相关文章
- [WP8] ListBox的Item宽度自动填满
[WP8] ListBox的Item宽度自动填满 范例下载 范例程序代码:点此下载 问题情景 开发WP8应用程序的时候,常常会需要使用ListBox作为容器来呈现各种数据集合.但是在ListBox呈现 ...
- ext:grid分页,列宽度自动填满grid宽度
var cm = new Ext.grid.ColumnModel([{ header : '编号', dataIndex : 'id' }, { header ...
- css怎么让li水平排列和div居中
让li向左浮动即可 给div定一个宽度,然后margin:0 auto;即可:
- 两个DIV,左DIV宽度固定,右DIV自动填满剩余空间
<style type="text/css"> #main{ width:98%; } #sidebar{ float:left; width:200px; backg ...
- css布局------左边宽度不定,右边宽度自动填满剩余空间
HTML <div class="container"> <div class="left"></div> <div ...
- -webkit-box 高度自动填满
<style> .box{ display: -webkit-box; -webkit-box-orient: vertical; height: 200px; background: # ...
- 手机H5,用Jquery使图片自动填满两栏式排版
遇上这样的排版,手机的解象度都不同,假如只用CSS3根本就做不出这样的排版:因此要用Jquery. 1. HTML <div class="postImgCenterCrop" ...
- ul li横向排列及圆点处理
如何用CSS制作横向菜单 让ul li横向排列及圆点处理 第一步:建立一个无序列表 我们先建立一个无序列表,来建立菜单的结构.代码是:<ul> <li><a href ...
- CSS + ul li 横向排列的两种方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
随机推荐
- CSS换行:word-wrap、word-break和text-wrap区别
一.word-wrap:允许对长的不可分割的单词进行分割并换行到下一行.(中英文处理效果一样) word-wrap有两个取值: 1.word-wrap: normal:只在允许的断字点换行(浏览器保持 ...
- 关于SQL语言,查询关联多张表出现的,无法返回空值的问题。
用外连接: SELECT * from PERempms left outer join PERPROMSon PERPROMS.BRANCH =PERempms.BRANCH left outer ...
- dataStructure@ Check whether a given graph is Bipartite or not
Check whether a given graph is Bipartite or not A Bipartite Graph is a graph whose vertices can be d ...
- poj1743--Musical Theme(后缀数组)
题意:求一列数字中走向相同的两个字序列,长度要求大于5 题解:相邻数字求差,原题就变成求相同的长度大于4的子串. [存疑:在保证两个子串不相交时觉得限定条件应该是大于x,但是wa了= = 不是很理解] ...
- Java HashMap实例源码分析
引言 HashMap在键值对存储中被经常使用,那么它到底是如何实现键值存储的呢? 一 Entry Entry是Map接口中的一个内部接口,它是实现键值对存储关键.在HashMap中,有Entry的实现 ...
- css div 不能贴边
*{margin:0;padding:0} 在css 最前加上这句,取消所有内边距和外边距.
- jboss eap6出现Tags_$$_javassist_26 cannot be cast to javassist.util.proxy.ProxyObject的解决办法
使用了spring,hibernate.部署在jboss eap6中时,查询时出现java.lang.ClassCastException: com.vteba.product.base.model. ...
- 偶遇mysql外键不好使
原来是创建表时选择的类型不一样,应该是innoDB,而且关联的主表类型也必须是innoDB
- HTML5要点(四)对象全整理
最近在自学H5,一下整理出来一些主要用到的知识点 1.JavaScript 对象 JS Array JS Boolean JS Date JS Math JS Number JS String JS ...
- HTML5要点(二)
<p> <b>今天周一</b>,<span>呵呵呵呵呵...</span> </p> <i>斜体文字</i&g ...