jQuery自适应父容器宽度高度特效js代码

版权:原创 更新时间:1年以上
[该文章底部包含文件资源,可根据自己情况,决定是否下载资源使用,时间>金钱,如有需要,立即查看资源]

以下是 jQuery自适应父容器宽度高度特效js代码 的示例演示效果:

当前平台(PC电脑)
  • 平台:

部分效果截图:

jQuery自适应父容器宽度高度特效js代码

HTML代码(index.html):

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery自适应父容器宽度高度特效</title>
<link rel="stylesheet" type="text/css" href="css/normalize.css" />
<link rel="stylesheet" type="text/css" href="css/zzsc-demo.css">
<style type="text/css">
	html, body {
		width: 100%;
		height: 100%;
		margin: 0;
	  }
	  body {
		background-image: url(img/Stranger-Things-Bicycle-Lights-Children-Night.jpg);
		background-size: cover;
		background-position: 50% 0%;
	  }
	  #my-container {
		color: white;
		width: 500px;
		height: 200px;
		position: absolute;
		left: 3%;
		bottom: 8%;
		width: 57%;
		height: 21%;
	  }
</style>

</head>
<body>
<div class="zzsc-container">
	<div id="my-container">
		  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.</p>
	</div>
</div>

<script type="text/javascript" src="src/adapttext.js"></script>
<script type="text/javascript">
	document.addEventListener('DOMContentLoaded', function() {
		new AdaptText(document.getElementById('my-container'), {
		  callback: function(newFontSize) {
			console.log('new font-size is ', newFontSize);
		  }
		});
	  });
</script>

</body>
</html>

JS代码(adapttext.js):

/*! * adapttext.js v1.0.1 - https://github.com/luruke/AdaptText.js * MIT License */
(function (root,factory){
	'use strict';
	if (typeof define === 'function' && define.amd){
	define([],factory);
}
else if (typeof module === 'object' && module.exports){
	module.exports = factory();
}
else{
	root.AdaptText = factory();
}
}
(this,function (){
	var AdaptText = function(element,options){
	//TODO:check if there is a child this.element = element;
	this.child = this.element.children[0];
	this.options ={
	minFontSize:10,maxFontSize:200,tollerance:10,callback:function(){
}
}
;
	for (var prop in options) if (options.hasOwnProperty(prop)) this.options[prop] = options[prop];
	this.child.style.display = 'inline-block';
	this.child.style.margin = '0px';
	//TODO:add debounce this.onResize = this.elaborate.bind(this);
	window.addEventListener('resize',this.onResize);
	this.elaborate();
}
;
	AdaptText.prototype.destroy = function(){
	window.removeEventListener('resize',this.onResize);
}
;
	//TODO:memoize the result beased on element's width/height? AdaptText.prototype.elaborate = function(){
	var fontSize = this.options.minFontSize;
	do{
	this.element.style.fontSize = (fontSize++) + 'px';
	if (fontSize > this.options.maxFontSize) break;
}
while (this.isFitting());
	fontSize--;
	this.element.style.fontSize = fontSize + 'px';
	this.options.callback(fontSize);
}
;
	AdaptText.prototype.isFitting = function(){
	var parentWidth = this.element.clientWidth;
	var parentHeight = this.element.clientHeight;
	var width = this.child.clientWidth;
	var height = this.child.clientHeight;
	return (width - this.options.tollerance <= parentWidth && height - this.options.tollerance <= parentHeight);
}
;
	var $ = window.jQuery || false;
	if ($){
	$.fn.adaptText = function (options){
	$(this).each(function(){
	$(this).data('adaptText',new AdaptText(this,options));
}
);
}
;
}
return AdaptText;
}
));
	

CSS代码(zzsc-demo.css):

body{background:#494A5F;color:#D5D6E2;font-weight:500;font-size:1.05em;font-family:"Microsoft YaHei","Segoe UI","Lucida Grande",Helvetica,Arial,sans-serif;}
a{color:rgba(255,255,255,0.6);outline:none;text-decoration:none;-webkit-transition:0.2s;transition:0.2s;}
a:hover,a:focus{color:#74777b;text-decoration:none;}
.zzsc-container{margin:0 auto;}
/* Header */
.zzsc-header{padding:1em 190px 1em;letter-spacing:-1px;text-align:center;}
.zzsc-header h1{color:#D5D6E2;font-weight:600;font-size:2em;line-height:1;margin-bottom:0;font-family:"Microsoft YaHei","宋体","Segoe UI","Lucida Grande",Helvetica,Arial,sans-serif,FreeSans,Arimo;}
.zzsc-header h1 span{font-family:"Microsoft YaHei","宋体","Segoe UI","Lucida Grande",Helvetica,Arial,sans-serif,FreeSans,Arimo;display:block;font-size:60%;font-weight:400;padding:0.8em 0 0.5em 0;color:#c3c8cd;}
附件:下载该文件资源,减少时间成本(增值服务)
留言
该资源可下载
File Source
.rar
114.76 KB
jquery特效7
最新结算
HTM5 Canvas实现3D飞机飞行动画特效代码
类型: .rar 金额: CNY 2.31¥ 状态: 待结算 详细>
HTM5 Canvas实现3D飞机飞行动画特效代码
类型: .rar 金额: CNY 0.29¥ 状态: 待结算 详细>
jQuery图像缩放工具插件Zoomer特效代码
类型: .rar 金额: CNY 0.29¥ 状态: 待结算 详细>
jQuery图像缩放工具插件Zoomer特效代码
类型: .rar 金额: CNY 2.31¥ 状态: 待结算 详细>
Labelauty–jQuery单选框_复选框美化插件特效代码
类型: .rar 金额: CNY 2.31¥ 状态: 待结算 详细>
Labelauty–jQuery单选框_复选框美化插件特效代码
类型: .rar 金额: CNY 0.29¥ 状态: 待结算 详细>
jQuery网页版打砖块小游戏源码
类型: .rar 金额: CNY 0.29¥ 状态: 待结算 详细>
jQuery网页版打砖块小游戏源码
类型: .rar 金额: CNY 2.31¥ 状态: 待结算 详细>
jquery虚拟键盘中文打字效果js代码
类型: .rar 金额: CNY 2.31¥ 状态: 待结算 详细>
jquery虚拟键盘中文打字效果js代码
类型: .rar 金额: CNY 0.29¥ 状态: 待结算 详细>
我们力求给您提供有用的文章,再此基础上,会附加营收资源,不做任何广告,让平台可以更好发展 若您发现您的权利被侵害,或使用了您的版权,请发邮件联系 sunlifel@foxmail.com ggbig觉得 : 不提供源码的文章不是好文章
合作伙伴
联系我们
  • QQ:21499807
  • 邮箱:sunlifel@foxmail.com
  • QQ扫一扫加QQ
    QQ扫一扫
Copyright 2023-2024 ggbig.com·皖ICP备2023004211号-1
打赏文章