练习rem响应式布局;
- HTML代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/reset.min.css">
<link rel="stylesheet" href="css/index.css">
<script>
;(function (designWidth) {
function computedFont() {
let winW = document.documentElement.clientWidth || document.body.clientWidth;
document.documentElement.style.fontSize = winW / designWidth * 100 + 'px';
}
computedFont();
window.addEventListener('resize', computedFont);
window.addEventListener('orientationchange', computedFont);
})(750);
</script>
<title>Title</title>
</head>
<body>
<section class="container">
<audio src="img/myDream.m4a"
preload="none"
id="musicAudio"></audio>
<div class="backgroundImg"></div>
<div class="bg"></div>
<header class="header">
<div class="content">
<img src="img/myDream.jpg" alt="">
<div class="song">
<p>我的梦</p>
<p>张靓颖</p>
</div>
<a href="javascript:;" id="musicBtn"></a>
</div>
</header>
<main class="main">
<div class="wrapper">
<p>我的未来不是梦</p>
<p>我的未来不是梦</p>
<p>我的未来不是梦</p>
<p>我的未来不是梦</p>
<p>我的未来不是梦</p>
<p>我的未来不是梦</p>
<p>我的未来不是梦</p>
<p>我的未来不是梦</p>
<p>我的未来不是梦</p>
<p>我的未来不是梦</p>
<p>我的未来不是梦</p>
</div>
</main>
<footer class="footer">
<div class="progress">
<span id="current">00:00</span>
<span id="duration">00:00</span>
<div class="probg">
<div class="already"></div>
</div>
</div>
<a href="javascript:;" id="down">下载这首音乐</a>
</footer>
</section>
<script src="js/zepto.min.js"></script>
<script src="js/index.js"></script>
</body>
</html>
- CSS代码
* {
margin: 0;
padding: 0;
}
html {
font-size: 100px;
height: 100%;
}
body {
height: 100%;
}
.container {
height: 100%;
position: relative;
}
.backgroundImg, .bg {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: -2;
}
.backgroundImg {
background: url("../img/myDream.jpg") no-repeat;
background-size: cover;
filter: blur(7px);/*让盒子有一个模糊度*/
-webkit-filter: blur(7px);
}
.bg {
background: rgba(0, 0, 0, .2);
}
.header {
position: relative;
padding: .3rem;
background: rgba(0, 0, 0, .3);
}
.header .content img {
width: 1.2rem;
height: 1.2rem
}
.header .content .song {
display: inline-block;
color: white;
vertical-align: top;
}
.header .content .song p {
height: .6rem;
}
.header .content .song p:nth-child(1) {
font-size: .45rem;
}
.header #musicBtn {
display: block;
width: .6rem;
height: .6rem;
background: url("../img/music.svg") no-repeat;
background-size: 100% 100%;
position: absolute;
right: .3rem;
top: 50%;
margin-top: -.3rem;
}
.header #musicBtn.select {
animation: move 1s linear 0s infinite;
}
@keyframes move {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.main {
padding: .3rem;
height: 5rem;
position: relative;
overflow: hidden;
}
.main .wrapper {
position: absolute;
top: 0;
left: 0;
width: 100%;
transition: all .3s linear 0s;
}
.main .wrapper p {
height: .84rem;
line-height: .84rem;
text-align: center;
color: rgba(255, 255, 255, .5);
letter-spacing: .04rem;
font-size: .4rem;
}
.main .wrapper p.select {
color: #31C27C;
}
.progress {
width: 100%;
height: .8rem;
position: relative;
overflow: hidden;
}
.progress span {
position: absolute;
color: #fff;
}
.progress span:nth-child(1) {
left: .3rem;
}
.progress span:nth-child(2) {
right: .3rem;
}
.progress .probg {
width: 65%;
margin: .15rem auto;
background: rgba(255, 255, 255, .3);
height: .04rem;
}
.progress .probg .already {
height: .04rem;
background: #31C27C;
width: 45%;
}
.footer #down {
display: block;
border-radius: .5rem;
width: 60%;
margin: auto;
height: 1rem;
line-height: 1rem;
text-align: center;
font-size: .4rem;
color: #fff;
background: url("../img/sprite_play.png") .2rem -5.86rem no-repeat #31C27C;
/*设置雪碧图的大小*/
background-size: .8rem 7rem;
}
- JS代码
// 1. 获取元素
let $main = $('.main');
let $header = $('.header');
let $footer = $('.footer');
let musicAudio = document.querySelector('#musicAudio');
let $musicBtn = $('#musicBtn');
let $wrapper = $('.wrapper');
let autoTimer = null;
// 2. 计算当前内容区域的高度
// 用屏幕的高度 - header的高度 - footer的高度 - 0.6rem
function computeMain() {
// px
let winH = document.documentElement.clientHeight;
let headerH = $header[0].offsetHeight;
let footerH = $footer[0].offsetHeight;
// 获取当前最新的fontSize
let fontSize = parseFloat(document.documentElement.style.fontSize);
// 用屏幕的高度 - header高度 - footer高度 - .6 rem;
let curMainH = (winH - headerH - footerH) / fontSize - 0.9 + 'rem';
$main.css({
height: curMainH
});
}
computeMain();
// 3. 请求数据
$.ajax({
url: 'json/lyric.json',
type: 'GET',
async: false,
success({lyric}) {
bindHTML(lyric)
}
});
// 4. 绑定数据
function bindHTML(data) {
// 1. 解决标题符号(歌曲名-歌手)
let d1 = data.replace(/&#(\d+);/g, (res, a) => {
switch (parseFloat(a)) {
case 32:
return " ";
case 40:
return '(';
case 41:
return ')';
case 45:
return '-';
}
// 如果捕获的不是以上这些数字要把捕获的内容返回,否则会把捕获的内容变成 undefined
return res;
});
let ary = [];
d1.replace(/\[(\d+):(\d+).(?:\d+)\]([^&#]+)(?: )/g, (res, min, sec, val) => {
ary.push({
minute: min,
second: sec,
value: val
})
});
console.log(ary);
// 循环绑定数据
let str = ``;
ary.forEach(({minute, second, value}) => {
str += `<p data-min="${minute}" data-sec="${second}">${value}</p>`;
});
$wrapper.html(str);
// 歌词就绪后播放音乐
musicAudio.play();
// 让音乐控制按钮转起来
$musicBtn.addClass('select');
// 计算播放时间、跟新进度条、变更选中歌词
autoTimer = setInterval(computedTime, 1000);
// musicAudio.ontimeupdate = computedTime;
}
// 5. 给音符按钮绑定触摸事件,如果当前正在播放,轻触按钮则暂停播放;如果当前处于暂停状态,轻触按钮则恢复播放;
$musicBtn.tap(function () {
//
if (musicAudio.paused) {
musicAudio.play();
$musicBtn.addClass('select');
// 重新绑定时器
autoTimer = setInterval(computedTime, 1000);
} else {
musicAudio.pause();
clearInterval(autoTimer);
$musicBtn.removeClass('select');
}
});
// 6. 根据播放时间更新高亮的歌词,并且跟新进度条
let curTop = 0;
let step = 0;
function computedTime() {
// 在audio元素上会有一个currentTime表示当前播放进度,duration表示音频总时长;
let {currentTime, duration} = musicAudio;
let curTime = formatTime(currentTime);
let durTime = formatTime(duration);
$('#current').html(curTime);
$('#duration').html(durTime);
// 如果当前时间大于等于总时间,清除定时器
if (currentTime >= duration) {
clearInterval(autoTimer);
$musicBtn.removeClass('select');
}
// 设置进度条
$('.already').css({
width: (currentTime / duration) * 100 + '%'
});
// 设置高亮的歌词
let min = curTime.split(':')[0];
let sec = curTime.split(':')[1];
let highLightItem = $wrapper.find('p').filter(`[data-min="${min}"]`).filter(`[data-sec="${sec}"]`);
if (highLightItem.length) {
step++;
highLightItem.addClass('select').siblings().removeClass('select');
if (step > 4) {
curTop -= .84;
$('.wrapper').css('top', `${curTop}rem`);
}
}
}
function formatTime(time) {
let min = Math.floor(time / 60);
let sec = Math.round(time - min * 60);
min < 10 && (min = '0' + min);
sec < 10 && (sec = '0' + sec);
return `${min}:${sec}`;
}
``` 【发上等愿,结中等缘,享下等福,择高处立,寻平处住,向宽处行】