以下是 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;}