3-1 js代码

来源:3-1 header响应式布局(1)

hunmix

2018-02-09 16:07:19

http://img.mukewang.com/climg/5a7d55720001edd707040480.jpg

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
<title>移动端</title>
<style>
*{
margin:0px;
padding:0px;
}
/*补充代码*/
html{
font-size: 20px;
}
.box{
min-width: 680px;
display: flex;
justify-content: space-around;
padding-top: 20px;
padding-bottom: 20px;
background:#ccc;
}
.box-item{
display: flex;
flex-direction:column;
align-items:center;
box-sizing: border-box;
width: 10rem;
height: 30%;
overflow: hidden;
padding:15px;
text-align: center;
background:#fff;
font-size: 12px;
box-shadow:1px 1px 2px 1px #ccc;
border-radius: 10px;
}
.box-item>img{
width: 7rem;
height: auto;
border-radius:50%;
margin-bottom: 5%;
}
.box-item>p{
width: 7rem;
height: 4rem;
word-break:break-all;
overflow: hidden;
}
</style>
</head>
<body>
<div class="box">
<div class="box-item">
<img src="http://climg.mukewang.com/591411a400018ad902000200.jpg" alt="">
<h2>快乐动起来呀</h2>
<p>先后在360、去哪儿网、今日头条任资深前端工程师,在业务开发和工程架构方面有较多实战经验。</p>
</div>
<div class="box-item">
<img src="http://climg.mukewang.com/591411b400010aa702000200.jpg" alt="">
<h2>7七月</h2>
<p>作为微信小程序第一波开发者,他受邀编写小程序开发书籍,不久就会出版,他喜欢写代码带来成就感。</p>
</div>
<div class="box-item">
<img src="http://climg.mukewang.com/591411c0000199be02000200.jpg" alt="">
<h2>Geely</h2>
<p>Geely,丰富的互联网项目经验,公司内部技术讲师,热爱技术,乐于分享。教学格言:把复杂的技术简单化,简单的技术极致化</p>
</div>
<div class="box-item">
<img src="http://climg.mukewang.com/591411b400010aa702000200.jpg" alt="">
<h2>Scott</h2>
<p>是国内最早接触 Node.js 的工程师,也是最早用 Node.js 做开发的工程师之一,拥有大量 Node.js 全站开发经验。</p>
</div>
<div class="box-item">
<img src="http://climg.mukewang.com/591411cb0001483f02000200.jpg" alt="">
<h2>moocer</h2>
<p>曾混迹于企业应用领域,后转战电商,现奋战于互联网教育行业,转眼间已匆匆数年,喜爱技术甚于自己,至今不悔。感天地之广大,岁月之蹉跎,若能重来,仍代码。</p>
</div>
</div>
<script type="text/javascript">
(function(){
var doc=document.documentElement;
var resizeEvent="oriwntationchange" in window ? "orientationchange":"reszie";
function recalc(){
var clientWidth=document.documentElement.clientWidth;
if(!clientWidth) return false;
if(clientWidth>=1125){
doc.style.fontSize=20+"px";
}else if(clientWidth<1125 && clientWidth>681){
doc.style.fontSize=20*(clientWidth/1125)+"px";
console.log(doc.style.fontSize)
}else{
doc.style.fontSize=12+"px";
}
}
window.addEventListener("resize",recalc,false);
document.addEventListener('DOMContentLoaded',recalc,false);
})()
</script>
</body>
</html>
js部分
<script type="text/javascript">
		(function(){
			var doc=document.documentElement;
			var resizeEvent="oriwntationchange" in window ? "orientationchange":"reszie";
			function recalc(){
				var clientWidth=document.documentElement.clientWidth;
				if(!clientWidth) return false;
				if(clientWidth>=1125){
					doc.style.fontSize=20+"px";
				}else if(clientWidth<1125 && clientWidth>681){
					doc.style.fontSize=20*(clientWidth/1125)+"px";
					console.log(doc.style.fontSize)
				}else{
					doc.style.fontSize=12+"px";
				}
			}
			window.addEventListener("resize",recalc,false);
			document.addEventListener('DOMContentLoaded',recalc,false);
		})()
	</script>

刚才写作业的时候按视频的思路敲了一下,但是不知道图中圈出代码的用途,求告知QAQ

写回答

3回答

一路电光带火花

2018-02-12

就跟轮播图一样,页面刚打开时就要去调用一下自动触发轮播的函数~这个是一样的道理,页面打开时,就出发这个函数,现算出页面文档字体的大小,当页面缩放时再触发相应的函数,然后再去触发这个函数。

很多封装的代码,随着你经验的丰富,代码写多了,很多自然就懂了,现在不要太去纠结这些封装好的代码,看不懂很正常。

0

一路电光带火花

2018-02-11

recalc()封装的是将元素宽度转换为rem的方法,要调用一下。

你别太较真了,这些东西都是固定的,直接调用就行,JQ库封装了更多的原生JS,也要去一行一行明白啊。。。。。

0
hunmix
h 看都看不懂写就更别谈了QAQ主要是上面绑定的两个事件都调用了这个函数,为什么下面要再调用一遍 费解
h018-02-11
共1条回复

一路电光带火花

2018-02-09

按照课程中的代码,addEventListener它是定义在document对象上的一个方法,你得有这个方法,下面才能调用,如果没有addEventListener这个方法,就不用调用了。

把判断去掉也是可以的,这里写是比较严谨的写法,网上去搜的话,基本都是这么写的,知道怎么用就行了,这都是固定的套路。

0
hunmix
h 还是不懂= =。。那为什么要在后面在调用一遍recalc()呢
h018-02-09
共1条回复

0 学习 · 5012 问题

查看课程