Starry Bgc🌭🧂🍞🥪🥪🍱
星空背景:
Math 内置对象设置星星的随机位置,
onresize 事件来提高用户效果,
CSS hover 选择器来进行交互效果。
展示效果
搭建基本结构
CSS 样式
span {
width: 30px;
height: 30px;
background: url("images/star.png") no-repeat;
background-size: 100% 100%;
}
html,body {
width: 100%;
height: 100%;
background-color: #000;
overflow: hidden;
}
HTML 样式
<span></span>
添加动画
这里我们需要给 星星 添加 hover 选择器来控制他们的 缩放 旋转 再给 span 添加一个过渡。
span:hover {
transform: scale(3,3) rotate(180deg) !important;
transition: all 2s;
}
然后再添加一个 动画效果 ,再添加一个 绝对定位 方便后面进行随机位置。
span {
position:absolute;
animation: flash 1s alternate infinite;
}
@keyframes flash {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
编写 JS 代码实现网页兼容高宽封装
这里的 document.compatMode 是获取浏览器的渲染方式 BackCompat 为 混杂模式 也为 怪异模式,
一般浏览器都为标准模式,如何才是 混杂模式 呢? 只需要把 HTML 中 <!DOCTYPE html> 去掉即可。
function getScreen() {
let width,
height = null;
if (window.innerWidth) {
width = window.innerWidth;
height = window.innerHeight;
} else if (document.compatMode == "BackCompat"