【CSS3】flex-basis属性演示

【传送门:阮一峰 - Flex 布局教程:语法篇】


相关文章:
1. 【flex-grow】属性演示
2.【flex-shrink】属性演示
3.【flex-basis】属性演示
4.【flex】属性演示

<html>
<head>
	<meta charset="utf-8" />
	<title>title>
	<style type="text/css">
		body {
      
			padding: 0;
			margin: 0;
		}
		.bg {
      
			position: absolute;
			min-width: 100%;
			min-height: 100%;
			background-color: rgb(210, 253, 198);
		}
		.center {
      
			width: 80%;
			margin: 0 auto;
		}
		.center .title {
      
			text-align: center;
		}
		/* 规定弹性容器 */
		.flex-container {
      
			display: -webkit-flex;
			display: flex;
		}
		.main-content {
      
			-webkit-flex: 0 1 50%;
			flex: 0 1 50%;
		}
		.main-content>h3 {
      
			text-align: center;
		}
		/* 分割线 */
		.line {
      
			margin: 0 20px;
			border: 1px dashed #aaa;
			-webkit-flex: 0 1 auto;
			flex: 0 1 auto;
		}

		/* 演示的容器和子元素 */
		.demo {
      
			position: relative;
			height: 100px;
			background-color: white;
			border: 2px solid black;
		}
		.demo .demo-item {
      
			width: 25%;
		}
		/* 非弹性元素的高需要定义 */
		.demo.block-container .demo-item {
      
			height: 100%;
			background-color: coral;
		}
		.demo.flex-container .demo-item {
      
			background-color: lightblue;
		}
		/* 文本输出 */
		.demo .output-txt {
      
			position: absolute;
			top: 30px;
			left: 10px;
			font-weight: bold;
			color: purple;
		}

		/* 控件部分 */
		/* 阻止收缩扩展 */
		.control .flex-container:first-child,
		.control .flex-container:last-child {
      
			margin-top: 10px;
		}
		.control .flex-container div,
		.control .flex-container .btn {
      
			padding: 5px;
			font-weight: bold;
			-webkit-flex: 0 1 100%;
			flex: 0 1 100%;
		}
		.control .flex-container div:first-of-type,
		.control .flex-container div:last-of-type {
      
			-webkit-flex: none;
			flex: none;
		}
		.control .flex-container div input[type="range"] {
      
			width: 100%;
			cursor: pointer;
		}
		.control .flex-container .btn {
      
			position: relative;
			overflow: hidden;
			font-size: 1.05em;
			font-weight: normal;
			text-align: center;
			text-decoration: none;
			color: white;
			background-color: rgb(72, 189, 91);
			border: 0 none;
			border-radius: 10px;
			outline: 0 none;
			cursor: pointer;
		}
		.control .flex-container .btn span:after {
      
			display: block;
			position: absolute;
			left: 0;
			bottom: 0;
			content: '';
			width: 150%;
			height: 150%;
			background-color: rgb(144, 238, 144);
			opacity: 0;
			transition: all 0.5s;
			-webkit-transition: all 0.5s;
		}
		.control .flex-container .btn:active span:after {
      
			width: 0;
			height: 0;
			opacity: 1;
			transition: 0s;
			-webkit-transition: 0s;
		}

		/* 总结部分 */
		.summary {
      
			padding: 10px;
			margin-top: 10px;
			background-color: white;
			border: 2px dashed #aaa;
			border-radius: 14px;
			box-shadow: 8px 8px 6px 0px grey;
		}
		.summary .txt-list {
      
			padding: 15px;
			margin: 0;
			color: white;
			background-color: purple;
			border-radius: inherit;
		}
		.summary .txt-list dd {
      
			line-height: 1.4;
			margin-bottom: 10px;
		}
		.summary .txt-list dd:last-child {
      
			margin-bottom: 0;
		}
	style>
head>
<body>
	<div class="bg">
		<div class="center">
			<h2 class="title">flex-basis:规定弹性子元素的初始宽度h2>
			
			<div class="flex-container">
				
				<div class="main-content">
					<h3>block盒子h3>
					
					<div class="demo block-container">
						
						<div class="demo-item">正常子元素div>
						
						<div class="output-txt">
							<div>width: <output class="width">25%output>;div>
							<div>flex-basis: <output class="flex-basis">autooutput>;div>
						div>
					div>
					
					<div class="control">
						<div class="flex-container">
							<div>width:0%div>
							<div><input type="range" min="0" max="150" value="25" />div>
							<div>150%;div>
						div>
						<div class="flex-container">
							<div>flex-basis:0%div>
							<div><input type="range" min="0" max="150" value="25" />div>
							<div>150%;div>
						div>
						<div class="flex-container">
							<button class="btn"><span>【重置】 flex-basis: auto;span>button>
						div>
					div>
				div>
				
				<div class="line">div>
				
				<div class="main-content">
					<h3>flex盒子h3>
					
					<div class="demo flex-container">
						
						<div class="demo-item">弹性子元素div>
						
						<div class="output-txt">
							<div>width: <output class="width">25%output>;div>
							<div>flex-basis: <output class="flex-basis">autooutput>;div>
						div>
					div>
					
					<div class="control">
						<div class="flex-container">
							<div>width:0%div>
							<div><input type="range" min="0" max="150" value="25" />div>
							<div>150%;div>
						div>
						<div class="flex-container">
							<div>flex-basis:0%div>
							<div><input type="range" min="0" max="150" value="25" />div>
							<div>150%;div>
						div>
						<div class="flex-container">
							<button class="btn"><span>【重置】 flex-basis: auto;span>button>
						div>
					div>
				div>
			div>
			<div class="summary">
				<dl class="txt-list">
					<dt>总结:dt>
					<dd>1. "flex-basis" 会覆盖弹性子元素的 "width",优先级:"flex-basis" > "width";dd>
					<dd>2. "flex-basis" 仅对弹性子元素有效,非弹性子元素无任何效果【容器为 "display: flex"、"display: inline-flex"】;dd>
					<dd>3. 若弹性子元素同时设置 "width"、"flex-basis(不为 auto)" 时,则以 "flex-basis" 的值作为初始宽度,且修改 "width" 无效;dd>
					<dd>4. 弹性子元素内 "width" 被 "flex-basis" 覆盖时,通过设置 "flex-basis: auto;",即可修改 "width"【但,"flex-basis" 不香吗?】;dd>
					<dd>5. 【特性】在单独设置的情况下,"flex-basis" 比 "width" 更具灵活性:"flex-basis: 0;" 由内容撑开,"flex-basis: 999%;" 不会撑破容器;dd>
					<dd>6. 【区别】在 "flex-basis: auto;" 的情况下,弹性子元素不设置 "width" 时,由内容撑开,而设置 "width: 0" 则令宽度为 0;dd>
					<dd>7. 【区别】默认情况下,普通盒子会被 width > 100% 的子元素撑破,而弹性盒子则不会被撑破;dd>
				dl>
			div>
		div>
	div>
	<script type="text/javascript">
		;(function(doc) {
      
			// 获取元素
			var item = doc.querySelectorAll('.demo .demo-item'),
				output_block = doc.querySelectorAll('.main-content:first-of-type output'),
				output_flex = doc.querySelectorAll('.main-content:last-of-type output'),
				// 组合选择 input 和 button
				input_block = doc.querySelectorAll('.main-content:first-child .control input[type="range"], .main-content:first-child .control button.btn'),
				input_flex = doc.querySelectorAll('.main-content:last-child .control input[type="range"], .main-content:last-child .control button.btn');

			// 创建并填充成二维数组,当然也可以直接获取所有滑键集进行遍历
			var input = new Array();
				input.push(input_block);
				input.push(input_flex);
				// input.push(reset);

			// 文本输出的二维数组
			var output = new Array();
				output.push(output_block);
				output.push(output_flex);

			/* - - - - - - - - - - - - - - - - - - - - - - - - IE兼容 - - - - - - - - - - - - - - - - - - - - - - - - */
			// 检测浏览器是否为 IE
			var IEVersion = (function() {
      
				// 取得浏览器的userAgent字符串
				var userAgent = navigator.userAgent;

				//判断是否IE<11浏览器
				var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1;

				//判断是否IE的Edge浏览器
				var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; 
				var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;

				if (isIE) {
      
					var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
					reIE.test(userAgent);
					var fIEVersion = parseFloat(RegExp["$1"]);
					if(fIEVersion == 7) {
      
						// 不是很准确,IE5 也是返回 7
						return 7;
					} else if (fIEVersion == 8) {
      
						return 8;
					} else if (fIEVersion == 9) {
      
						return 9;
					} else if (fIEVersion == 10) {
      
						return 10;
					} else {
      
						// IE版本<=7
						return 6;
					}
				} else if (isEdge) {
      
					// edge
					return 'edge';
				} else if (isIE11) {
      
					// IE11
					return 11;
				} else {
      
					// 不是ie浏览器
					return -1;
				}
			})();

			// 监听句柄兼容 IE
			function addHandler(element, type, handler){
      
				if(element.addEventListener){
      
					element.addEventListener(type, handler, false);
				}else if(element.attachEvent){
      
					element.attachEvent('on' + type, handler);
				}else{
      
					element['on' + type] = handler;
				}
			}

			/* - - - - - - - - - - - - - - - - - - - - - - - - 项目 - - - - - - - - - - - - - - - - - - - - - - - - */
			// 修改样式
			function chgStyle(ele, type, txt) {
      
				// IE不支持"input"事件,改用"change"事件
				if (IEVersion !== -1 && type === 'input') {
      
					type = 'change';
				}
				for (var i = 0; i < ele.length; i++) {
      
					for (var j = 0; j < ele[i].length; j++) {
      
						// 避免污染,创建函数域
						(function(type) {
      
							// 筛选除 button
							if (ele[i][j].tagName.toLowerCase() === 'button') {
      
								// 该变量会污染上层(括号外)的变量
								type = 'click';
							}
							// console.log(type);
							addHandler(ele[i][j], type, function(i, j) {
      
								return function() {
      
									if (j === 0) {
      	// width
										txt[i][j].value = ele[i][j].value + '%';
										item[i].style.width =  ele[i][j].value + '%';
										if (ele[i][0].getAttribute('data-disable') === null) {
      
											// flex-basis: auto; 表示跟随 width
											ele[i][1].value = ele[i][j].value;
										}
									} else if (j === 1) {
      	// flex-basis
										txt[i][j].value = ele[i][j].value + '%';
										item[i].style.flexBasis =  ele[i][j].value + '%';
										item[i].style.webkitFlexBasis =  ele[i][j].value + '%';
										// 禁止滑键跟随
										ele[i][0].setAttribute('data-disable', 'on');
									} else {
      	// reset-btn
										txt[i][1].value = 'auto';
										item[i].style.flexBasis =  'auto';
										item[i].style.webkitFlexBasis =  'auto';
										ele[i][1].value = ele[i][0].value;
										ele[i][0].removeAttribute('data-disable');
									}
								}
							}(i, j));
						})(type);
					}
				}
			}

			/*
				// 方法二:创建一个新变量,作判断赋值
				function chgStyle(ele, type, txt) {
					// IE不支持"input"事件,改用"change"事件
					if (IEVersion !== -1 && type === 'input') {
						type = 'change';
					}
					for (var i = 0; i < ele.length; i++) {
						for (var j = 0; j < ele[i].length; j++) {

							// 创建变量,通过判断赋值
							var tp = '';
							// 筛选除 button
							if (ele[i][j].tagName.toLowerCase() === 'button') {
								tp = 'click';	// 主要原因在于该值会污染括号外的值
							}  else {
								if (IEVersion !== -1 && type === 'input') {
									tp = 'change';
								} else {
									tp = type;
								}
							}

							console.log(tp);

							addHandler(ele[i][j], tp, function(i, j) {
								return function() {
									if (j === 0) {	// width
										txt[i][j].value = ele[i][j].value + '%';
										item[i].style.width =  ele[i][j].value + '%';
										if (ele[i][0].getAttribute('data-disable') === null) {
											// flex-basis: auto; 表示跟随 width
											ele[i][1].value = ele[i][j].value;
										}
									} else if (j === 1) {	// flex-basis
										txt[i][j].value = ele[i][j].value + '%';
										item[i].style.flexBasis =  ele[i][j].value + '%';
										item[i].style.webkitFlexBasis =  ele[i][j].value + '%';
										// 禁止滑键跟随
										ele[i][0].setAttribute('data-disable', 'on');
									} else {	// reset-btn
										txt[i][1].value = 'auto';
										item[i].style.flexBasis =  'auto';
										item[i].style.webkitFlexBasis =  'auto';
										ele[i][1].value = ele[i][0].value;
										ele[i][0].removeAttribute('data-disable');
									}
								}
							}(i, j));
						}
					}
				}
			*/

			/*
				// 方法三:不传入 type,直接内部声明变量,再作判断,注意调用时要修改实参
				function chgStyle(ele, txt) {
					for (var i = 0; i < ele.length; i++) {
						for (var j = 0; j < ele[i].length; j++) {
							// 或者,不传入参数,直接内部声明变量,再作判断
							var type = '';
							// 筛选除 button
							if (ele[i][j].tagName.toLowerCase() === 'button') {
								type = 'click';	// 主要原因在于该值会污染括号外的值
							}  else {
								if (IEVersion !== -1 && type === 'input') {
									type = 'change';
								} else {
									type = 'input';
								}
							}

							console.log(type);

							addHandler(ele[i][j], type, function(i, j) {
								return function() {
									if (j === 0) {	// width
										txt[i][j].value = ele[i][j].value + '%';
										item[i].style.width =  ele[i][j].value + '%';
										if (ele[i][0].getAttribute('data-disable') === null) {
											// flex-basis: auto; 表示跟随 width
											ele[i][1].value = ele[i][j].value;
										}
									} else if (j === 1) {	// flex-basis
										txt[i][j].value = ele[i][j].value + '%';
										item[i].style.flexBasis =  ele[i][j].value + '%';
										item[i].style.webkitFlexBasis =  ele[i][j].value + '%';
										// 禁止滑键跟随
										ele[i][0].setAttribute('data-disable', 'on');
									} else {	// reset-btn
										txt[i][1].value = 'auto';
										item[i].style.flexBasis =  'auto';
										item[i].style.webkitFlexBasis =  'auto';
										ele[i][1].value = ele[i][0].value;
										ele[i][0].removeAttribute('data-disable');
									}
								}
							}(i, j));
						}
					}
				}
			*/
		
			// etc...

			function bindEvent() {
      
				chgStyle(input, 'input', output);
			}

			function init() {
      
				bindEvent();
			}

			init();

		})(document);
	script>
body>
html>

你可能感兴趣的:(CSS3,#,盒子模型练习)