打砖块
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 500px;
height: 500px;
border: 1px solid;
margin: 100px auto;
position: relative;
}
.ball {
width: 20px;
height: 20px;
border-radius: 50%;
background-color: #e67e22;
position: absolute;
bottom: 40px;
left: 250px;
}
.bat {
width: 100px;
height: 20px;
background-color: #3498db;
position: absolute;
bottom: 0;
left: 200px;
}
#brick div {
width: 98px;
border: 1px solid;
height: 28px;
position: absolute;
}
</style>
<script src="js/untils.js"></script>
<script>
window.onload = function() {
var box = document.querySelector(".box");
var ball = document.querySelector(".ball");
var bat = document.getElementById("bat");
var brick = document.getElementById("brick");
var brickDivs = brick.getElementsByTagName("div");
//获取一个随机的水平方向的速度。
var speedX = parseInt(Math.random() * 4 + 4);
var speedY = parseInt(Math.random() * 3 + 6);
var timer = setInterval(function() {
ball.style.left = ball.offsetLeft + speedX + "px";
ball.style.top = ball.offsetTop + speedY + "px"
//限制水平出界
//获取大盒子的宽
var boxWidth = parseInt(getStyle(box, "width"));
//获取大盒子的高
var boxHeight = parseInt(getStyle(box, "height"));
//获取小球的宽
var ballWidth = parseInt(getStyle(ball, "width"));
//获取小球的高
var ballHeight = parseInt(getStyle(ball, "height"));
var ballHeight = parseInt(getStyle(ball, "height"));
if (ball.offsetLeft >= boxWidth - ballWidth || ball.offsetLeft <= 0) {
speedX *= -1;
}
if (ball.offsetTop >= boxHeight - ballHeight) {
// console.log(ball.offsetTop);
alert("游戏结束!");
clearInterval(timer);
}
//小球和木板进行碰撞检测
if (crash(bat, ball)) {
speedY *= -1;
}
for (var i = 0; i < brickDivs.length; i++) {
if (crash(brickDivs[i], ball)) {
//发生了碰撞 ,小球的移动方向发生改变。
speedY *= -1;
//砖块消失
brickDivs[i].remove();
break;
}
}
if (ball.offsetTop <= 0) {
speedY *= -1;
}
}, 30)
createBrick(40);
new Drag("bat");
}
//obj 元素 attr 样式的属性
function getStyle(obj, attr) {
if (obj.currentStyle) {
// ie低版本 元素.currentStyle.样式属性名
// box.currentStyle.width
// box.currentStyle["width"]
return obj.currentStyle[attr];
} else {
// 标准浏览器 getComputedStyle(要获取样式的元素)
// getComputedStyle(box).width
return getComputedStyle(obj)[attr];
}
}
//创建砖块的函数 n是砖块的数量
function createBrick(n) {
var brick = document.getElementById("brick");
for (var i = 0; i < n; i++) {
var node = document.createElement("div");
node.style.backgroundColor = randomColor();
brick.appendChild(node);
node.style.left = i % 5 * node.offsetWidth + "px";
node.style.top = parseInt(i / 5) * node.offsetHeight + "px";
}
}
function randomColor() {
var str = "rgba(" + parseInt(Math.random() * 255) + "," + parseInt(Math.random() * 255) + "," + parseInt(Math.random() * 255) + ")";
return str;
}
function crash(node1, node2) {
//第一个盒子四条边的位置
var l1 = node1.offsetLeft;
var r1 = node1.offsetLeft + node1.offsetWidth;
var t1 = node1.offsetTop;
var b1 = node1.offsetTop + node1.offsetHeight;
//第二个盒子四条边的位置
var l2 = node2.offsetLeft;
var r2 = node2.offsetLeft + node2.offsetWidth;
var t2 = node2.offsetTop;
var b2 = node2.offsetTop + node2.offsetHeight;
if (l2 > r1 || r2 < l1 || b1 < t2 || t1 > b2) {
return false;
} else {
return true;
}
}
</script>
</head>
<body>
<div class="box">
<div class="ball"></div>
<div class="bat" id="bat"></div>
<div id="brick">
</div>
</div>
</body>
</html>
untils.js
function Drag(id) {
this.box = document.getElementById(id);
this.box.onmousedown = this.boxDown.bind(this);
document.onmouseup = this.boxUp;
}
Drag.prototype.boxDown = function (ev) {
//处理下兼容
let e = ev || window.event;
//记录鼠标按下时相对于盒子的位置。
offsetX = e.clientX - this.box.offsetLeft;
document.onmousemove = this.boxMove.bind(this);
}
Drag.prototype.boxMove = function (ev) {
var e = ev || window.event;
var l = e.clientX - offsetX;
//限制出界
if (l <= 0) {
l = 0;
}
if (l >= 400) {
l = 400;
}
this.box.style.left = l + "px";
}
Drag.prototype.boxUp = function () {
document.onmousemove = null;
}
// node 运动的元素节点
// obj 样式对象
// callback 回调函数
function move(node, obj, callback) {
// node节点也是一个对象。 可以自定义属性。
clearInterval(node.timer);
node.timer = setInterval(function () {
//所有的运动都已经完成
var isEnd = true;
for (const attr in obj) {
if (obj.hasOwnProperty(attr)) {
//attr 属性名
//ITarget,变化的值
const iTarget = obj[attr];
//获取运动的样式的当前数值
var icur = null;
if (attr == "opacity") {
icur = parseInt(getStyle(node, attr) * 100);
} else {
icur = parseInt(getStyle(node, attr));
}
var speed = (iTarget - icur) / 8;
speed = speed > 0 ? Math.ceil(speed): Math.floor(speed);
// speed = Math.ceil(speed);
//位置数值先运算。此时还未发生移动。
icur += speed;
if ((speed >= 0 && iTarget > icur) || (speed <= 0 && iTarget < icur)) {
//未完成情况
isEnd = false;
} else {
//已完成
icur = iTarget;
}
//将运动的结果赋值给节点
if (attr == "opacity") {
node.style.opacity = icur / 100;
node.style.filter = "alpha(opacity=" + icur + ")";
} else {
node.style[attr] = icur + "px";
}
}
}
if (isEnd) {
clearInterval(node.timer);
//开始下一个运动,不能写死,
if (callback) {
callback();
}
}
}, 30)
}
//obj 元素 attr 样式的属性
function getStyle(obj, attr) {
if (obj.currentStyle) {
return obj.currentStyle[attr];
} else {
return getComputedStyle(obj)[attr];
}
}
测试碰撞
移动一个盒子撞到另一个盒子时显示“撞上了!”。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
width: 100px;
height: 100px;
position: absolute;
}
#box1 {
background-color: skyblue;
left: 200px;
top: 200px;
}
#box2 {
background-color: brown;
left: 400px;
top: 200px;
}
</style>
<script>
function Drag(id) {
this.box = document.getElementById(id);
this.box.onmousedown = this.boxDown.bind(this);
this.box.onmouseup = this.boxUp;
}
Drag.prototype.boxDown = function(ev) {
//处理下兼容
let e = ev || window.event;
//记录鼠标按下时相对于盒子的位置。
offsetX = e.offsetX;
offsetY = e.offsetY;
document.onmousemove = this.boxMove.bind(this);
}
Drag.prototype.boxMove = function(ev) {
var e = ev || window.event;
this.box.style.left = e.pageX - offsetX + "px";
this.box.style.top = e.pageY - offsetY + "px";
var box2 = document.getElementById("box2");
var flag = crash(this.box, box2);
if (flag) {
alert("撞上了!");
}
}
Drag.prototype.boxUp = function() {
document.onmousemove = null;
}
window.onload = function() {
new Drag("box1");
}
//碰撞检测的函数
function crash(node1, node2) {
//第一个盒子四条边的位置
var l1 = node1.offsetLeft;
var r1 = node1.offsetLeft + node1.offsetWidth;
var t1 = node1.offsetTop;
var b1 = node1.offsetTop + node1.offsetHeight;
//第二个盒子四条边的位置
var l2 = node2.offsetLeft + node2.offsetWidth;
var r2 = node2.offsetLeft + node2.offsetWidth;
var t2 = node2.offsetTop;
var b2 = node2.offsetTop + node2.offsetHeight;
if (l2 > r1 || r2 < l1 || b1 < t2 || t1 > b2) {
return false;
} else {
return true;
}
}
</script>
</head>
<body>
<div id="box1"> </div>
<div id="box2"> </div>
</body>
</html>
完全体
让这个盒子,点击后,宽,高,透明度同时变化
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#box {
width: 100px;
height: 100px;
opacity: 1;
background-color: brown;
margin: 100px auto;
}
</style>
<script>
// node 运动的元素节点
// obj 样式对象
// callback 回调函数
function move(node, obj, callback) {
clearInterval(node.timer);
node.timer = setInterval(function() {
var isEnd = true;
for (const attr in obj) {
if (obj.hasOwnProperty.call(obj, attr)) {
//attr 属性名
//ITarget,变化的值
const iTarget = obj[attr];
//获取运动的样式的当前数值
var icur = null;
if (attr == "opacity") {
icur = parseInt(getStyle(node, attr) * 100);
} else {
icur = parseInt(getStyle(node, attr));
}
var speed = (iTarget - icur) / 8;
speed = Math.ceil(speed);
//位置数值先运算。此时还未发生移动。
icur += speed;
if (speed >= 0 && iTarget > icur || (speed <= 0 && iTarget < icur)) {
//未完成情况
isEnd = false;
} else {
//已完成
icur = iTarget;
}
//将运动的结果赋值给节点
if (attr == "opacity") {
node.style.opacity = icur / 100;
node.style.filter = "alpha(opacity=" + icur + ")";
} else {
node.style[attr] = icur + "px";
}
}
}
if (isEnd) {
clearInterval(node.timer);
//开始下一个运动,不能写死,
if (callback) {
callback();
}
}
}, 30)
}
//obj 元素 attr 样式的属性
function getStyle(obj, attr) {
if (obj.currentStyle) {
return obj.currentStyle[attr];
} else {
return getComputedStyle(obj)[attr];
}
}
window.onload = function() {
var box = document.getElementById("box");
box.onclick = function() {
move(this, {
"width": "300",
"height": "500"
})
}
}
</script>
</head>
<body>
<!-- 让这个盒子,点击后,宽,高,透明度同时变化 -->
<div id="box"></div>
</body>
</html>
轮播图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
#box {
width: 950px;
height: 600px;
margin: 20px auto;
position: relative;
overflow: hidden;
}
#imgs {
list-style: none;
width: 3800px;
height: 600px;
position: absolute;
left: 0;
}
#imgs li {
width: 950px;
height: 600px;
float: left;
}
#imgs li img {
width: 100%;
height: 100%;
}
</style>
<script>
window.onload = function() {
var imgs = document.getElementById("imgs");
var fristLi = document.querySelectorAll("#imgs li")[0].cloneNode(true);
imgs.appendChild(fristLi);
var len = imgs.querySelectorAll("#imgs li").length;
imgs.style.width = len * 950 + "px";
setInterval(function() {
//当最后一张图片走完后,应该让整个ul回到起点位置。
imgs.style.left = imgs.offsetLeft - 2 + "px";
if (imgs.offsetLeft <= -(len - 1) * 950) {
imgs.style.left = "0px";
}
}, 20)
}
</script>
</head>
<body>
<div id="box">
<ul id="imgs">
<li>
<img src="img/1.jpg" alt="">
</li>
<li>
<img src="img/2.jpg" alt="">
</li>
<li>
<img src="img/3.jpg" alt="">
</li>
<li>
<img src="img/4.jpg" alt="">
</li>
</ul>
</div>
</body>
</html>
轮播图缓冲
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
#box {
width: 950px;
height: 600px;
margin: 20px auto;
position: relative;
overflow: hidden;
}
#imgs {
list-style: none;
width: 3800px;
height: 600px;
position: absolute;
left: 0;
}
#imgs li {
width: 950px;
height: 600px;
float: left;
}
#imgs li img {
width: 100%;
height: 100%;
}
</style>
<script src="js/untils.js"></script>
<script>
window.onload = function() {
var imgs = document.getElementById("imgs");
var firstlLi = document.querySelectorAll("#imgs li")[0].cloneNode(true);
imgs.appendChild(firstlLi);
var len = imgs.querySelectorAll("#imgs li").length;
imgs.style.width = len * 950 + "px";
setInterval(function() {
move(imgs, {
left: imgs.offsetLeft - 950
}, function() {
if (imgs.offsetLeft <= -(len - 1) * 950) {
imgs.style.left = "0px";
}
})
}, 2000)
}
</script>
</head>
<body>
<div id="box">
<ul id="imgs">
<li>
<img src="img/1.jpg" alt="">
</li>
<li>
<img src="img/2.jpg" alt="">
</li>
<li>
<img src="img/3.jpg" alt="">
</li>
<li>
<img src="img/4.jpg" alt="">
</li>
</ul>
</div>
</body>
</html>
untils.js
function Drag(id) {
this.box = document.getElementById(id);
this.box.onmousedown = this.boxDown.bind(this);
document.onmouseup = this.boxUp;
}
Drag.prototype.boxDown = function (ev) {
//处理下兼容
let e = ev || window.event;
//记录鼠标按下时相对于盒子的位置。
offsetX = e.clientX - this.box.offsetLeft;
document.onmousemove = this.boxMove.bind(this);
}
Drag.prototype.boxMove = function (ev) {
var e = ev || window.event;
var l = e.clientX - offsetX;
//限制出界
if (l <= 0) {
l = 0;
}
if (l >= 400) {
l = 400;
}
this.box.style.left = l + "px";
}
Drag.prototype.boxUp = function () {
document.onmousemove = null;
}
// node 运动的元素节点
// obj 样式对象
// callback 回调函数
function move(node, obj, callback) {
// node节点也是一个对象。 可以自定义属性。
clearInterval(node.timer);
node.timer = setInterval(function () {
//所有的运动都已经完成
var isEnd = true;
for (const attr in obj) {
if (obj.hasOwnProperty(attr)) {
//attr 属性名
//ITarget,变化的值
const iTarget = obj[attr];
//获取运动的样式的当前数值
var icur = null;
if (attr == "opacity") {
icur = parseInt(getStyle(node, attr) * 100);
} else {
icur = parseInt(getStyle(node, attr));
}
var speed = (iTarget - icur) / 8;
speed = speed > 0 ? Math.ceil(speed): Math.floor(speed);
// speed = Math.ceil(speed);
//位置数值先运算。此时还未发生移动。
icur += speed;
if ((speed >= 0 && iTarget > icur) || (speed <= 0 && iTarget < icur)) {
//未完成情况
isEnd = false;
} else {
//已完成
icur = iTarget;
}
//将运动的结果赋值给节点
if (attr == "opacity") {
node.style.opacity = icur / 100;
node.style.filter = "alpha(opacity=" + icur + ")";
} else {
node.style[attr] = icur + "px";
}
}
}
if (isEnd) {
clearInterval(node.timer);
//开始下一个运动,不能写死,
if (callback) {
callback();
}
}
}, 30)
}
//obj 元素 attr 样式的属性
function getStyle(obj, attr) {
if (obj.currentStyle) {
return obj.currentStyle[attr];
} else {
return getComputedStyle(obj)[attr];
}
}
缓冲测试
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/untils.js"></script>
<style>
#box {
width: 100px;
height: 100px;
background-color: brown;
position: absolute;
left: 800px;
top: 200px;
}
#line {
width: 1px;
height: 100%;
background-color: black;
position: absolute;
left: 200px;
top: 0;
}
</style>
<script>
window.onload = function() {
var btn = document.getElementById("btn");
var box = document.getElementById("box");
btn.onclick = function() {
move(box, {
"left": "200"
})
}
}
</script>
</head>
<body>
<button id="btn">开始运动</button>
<div id="box">
</div>
<div id="line"></div>
</body>
</html>
swiper
swiper是一款免费,强大的滑动的插件。
使用步骤:
1.引入插件(可以使用网络地址,也可以下载到本地)
2.创建HTML结构
3.你可能想要给Swiper定义一个大小,当然不要也行。
4.初始化swiper
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="css/swiper.min.css">
<script src="js/swiper.min.js"></script>
<style>
.swiper-container {
width: 600px;
height: 300px;
}
.swiper-slide img {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide"><img src="img/1.jpg" alt=""></div>
<div class="swiper-slide"><img src="img/2.jpg" alt=""></div>
<div class="swiper-slide"><img src="img/3.jpg" alt=""></div>
</div>
<!-- 如果需要分页器 -->
<div class="swiper-pagination"></div>
<!-- 如果需要导航按钮 -->
<!-- <div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div> -->
<div class="swiper-button-prev swiper-button-white"></div>
<!-- 白色 -->
<div class="swiper-button-next swiper-button-white"></div>
<!-- 黑色 -->
<!-- 如果需要滚动条 -->
<div class="swiper-scrollbar"></div>
</div>
<script>
var mySwiper = new Swiper('.swiper-container', {
// direction: 'vertical', // 垂直切换选项
loop: true, // 循环模式选项
effect: 'cube',
// 如果需要分页器
// pagination: {
// el: '.swiper-pagination',
// },
// 如果需要前进后退按钮
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
// 如果需要滚动条
// scrollbar: {
// el: '.swiper-scrollbar',
// },
})
</script>
</body>
</html>
自执行函数
自执行函数:IIFE 在定义时立即执行的函数
1.声明一个函数
2.立刻调用。
函数分为两个阶段:
1.定义阶段
2.调用阶段
写法:
(function(形参){})(实参)
(function(形参){}(实参))
自执行函数的作用:
解决全局变量的污染
作用域:
全局作用域
局部作用域(函数作用域)
块级作用域
//之前
function show() {
alert("方法执行了!");
}
show();
var fn=function(){
alert("fn函数:a="+a);
}
fn(10);
//四种自执行函数写法,不用调用就可以执行
(function(a) {
alert("fn函数:a=" + a);
})(10);//fn函数:a=10
(function() {
alert("自执行函数第二种")
}());//自执行函数第二种
+function() {
alert("自执行函数第三种")
}();
-function() {
alert("自执行函数第四种")
}();
//自执行函数面对有返回值的不推荐使用,会把它当做运算符
var x = - function(a) {
return a;
}(123);
console.log(x);//-123
//当在外部引入两个js文件时,都有num,这时就会搞混
function showMoney() {
alert("你账户余额为:" + num);
}
showMoney();
可以使用自执行函数,将全局变量变为局部变量,解决全局变量的污染
//这是您的余额
(function(){
var num = 100;
})()
//解决输出同一个的问题
window.onload = function() {
var lis = document.querySelectorAll("li");
for (var i = 0; i < lis.length; i++) {
(function(i) {
lis[i].onclick = function() {
console.log(i);
}
})(i);
}
}