bilibili JS 视频链接:https://www.bilibili.com/video/BV1W54y1J7Ed?p=2&spm_id_from=pageDriver
vscode 配置 html:https://blog.csdn.net/caohongxing/article/details/108632859
JS来源
- 借鉴了C语言的基本语法
- 借鉴了Java语言的数据类型和内存管理
- 借鉴了Scheme语言,将函数提升到第一等公民的地位
- 借鉴了Self 语言,使用基于原型的继承机制
JavaScript 核心由三部分组成
- BOM:操作浏览器部件
- DOM:操作文本流
- ECMAScript:JS的语法规则(这才是核心)
书写位置:行内式,内嵌式,外链式
行内式:(把JS直接写在标签里)
- a标签:写在href属性里
<a href="javascript: codecode;">here</a> - 非a标签:写在行为属性里
<div onclick="alert('hello world')">非a标签</div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<a href="javascript: alert('hello javascirpt');">a标签</a>
<div onclick="alert('hello world')">非a标签</div>
</body>
</html>
内嵌式:把代码书写在一个script标签对内(这种不需要任何行为会自动执行JS代码)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
alert("hello word");
</script>
</body>
</html>
外链式:把代码书写在一一个.js 文件内,如:在同目录下新建一个JS文件,写入alert("hello word"); 之后再HTML中引用:<script src='./test.js'></script>
注:行内式能不用就不用;内嵌式学习的使用;推荐外链式
变量:var 准备一个容器,变量名有规格且区分大小写
var numb = 100;
numb = 200;
输出方式:
alert("hello alert");
console.log("hello log");
document.write("hello document");
JS中的数据:基本数据类型和引用数据类型
基本数据类型:
- 数值类型:Number
- 字符串类型:string 由单引号或双引号括起来
- 布尔类型:Boolean false true
- 空类型:有两种(undefined 和 null)
var n1 = 100;
var n2 = -10.4;
var n3 = 2e5
var n4 = 0x100;
var n5 = 0o100;
var n6 = 0b100;
var k1 = null;
var k2;
var type_k1 = typeof k1;
语句需要分号结尾吗?加不加都行
数据类型转换
var n1 = Number('1.3');
var n2 = parseInt('100ac');
var n3 = parseFloat('10.56ccc');
var s1 = string(true);
var s2 = n1.toString();
var b1 = Boolean(0);
运算符
var s1 = '10' + 20;
s1 += '200';
var b1 = 10 == '10';
var b2 = 10 === '10';
var b3 = 10 != '10';
var b4 = 10 !== '10';
判断if
if(){} if(){} else{} if(){} else if(){} if(){} else if(){} else{}
分支switch
switch(value){
case 100:
console.log('100');
break;
case 200:
console.log('200');
break;
default:
console.log('no');
}
循环:
whick(){} do{} while(); for(var i = 0; i < 3; i++){}
函数:function fn(){return;} 调用:fn();
作用域
作用域:只有函数可以生成私有作用域,那for循环里定义的var i = 0; ,依旧可以在for循环之后使用。
一个引用数据类型:对象数据类型(object),对应python中的字典类型数据
var obj = {1: 1, 'j': 'j', true: true} ,key和value中的值都可以是任意类型- 中括号中的是字符串
- 增:
obj.'c' = 'c'; ,obj[c] = true; - 删:
delete obj.'c'; , delete obj[c]; - 改:和增加的语法一样
- 查:
obj.'c' ,obj[c]
一个引用数据类型:数组(array)
- 有序的(从0开始)、数据集合,且类型任意
var arr = [100, true, 'cha']; - 获取长度:
arr.length ;设置长度:arr.length = 4; (小了会删除,大 了会?) - 获取数据及赋值:
arr[2] = 400;
数组常用方法
var new_lenght = arr.push('new data');
var delete_data = arr.pop();
var new_lenght = arr.unshift("start data");
var delete_data = arr.shift();
var new_arr = arr.reverse()
var delete_arr = arr.splice(1, 1);
var delete_arr = arr.splice(1, 1, 'new data');
var new_arr = arr.sort();
var strs = arr.join('-');
var new_arr = arr1.concat(arr2);
var new_arr = arr.slive(1);
var data_index = arr.indexOf(data);
arr.forEach(function(item, index, arr){
})
var new_arr = arr.map(function(item, index, arr){
return item * 10;
})
var new_arr = arr.filter(function(item, index, arr){
return item > 10;
})
var is_right = arr.every(function(item, index, arr){
return item > 10;
})
var is_right = arr.some(function(item, index, arr){
return item > 10;
})
字符串常用方法
var str = strs.charAt(3);
var strs = strs.toLowerCase();
var strs = strs.replace(',', '.');
var strs = strs.trim()
var strs_arr = strs.split('-')
var new_strs = strs.sbustr(1, 6);
var new_strs = strs.shustring(1, 6)
var new_strs = strs.slice(1, 6)
数字常用方法
var num = Math.random();
var num = Math.round(10.5);
var num = Math.pow(2, 5);
var num = Math.abs(-8);
var num = Math.max(-8, 4, 5, 7, 90);
var num = Math.PI()
function randomNum(min, max){
return Math.floor(Math.random() * (max-min+1)) + min
}
一个引用数据类型:时间(Date),及常用方法
var time = new Date();
var time = new Date(2021, 10, 15, 19, 45, 59);
var year = time.getFullYear();
var month = time.getMonth();
var date = time.getDate();
var hours = time.getHours();
var minutes = time.getMinutes();
var seconds = time.getSeconds();
var day = time.getDay();
var time = time.getTime()
function getDiff(start, stop){
var startTime = start.getTime();
var stopTime = stop.getTime();
var seconds = Math.ceil((stopTime - startTime) / 1000);
var day = Math.floor(seconds / (60*60*24));
var hours = Math.floor(seconds % (60*60*24) / (60*60));
var minutes = Math.floor(seconds % (60*60) / 60);
var seconds = sub % 60;
return {day: day, hours: hours, minutes: minutes, seconds: soconds};
}
BOM操作 操作浏览器相关的内容
- Browser Object Model
- 滚动条位置、定位刷新、后退、打开新标签等等
var w = window.innerWidth;
var h = window.innerHeight;
window.alert('hello');
var res = window.confirm("is True?");
var res = window.prompt("please input");
window.open("地址");
window.close();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>
<button id="on"> 打开新标签 </button>
<button id="off"> 关闭当前页面 </button>
<script>
on.onclick = function(){
window.open('https://www.bilibili.com/video/BV1W54y1J7Ed?p=28');
}
off.onclick = function(){
window.close();
}
</script>
</div>
</body>
</html>
window.onload = function(){
}
window.onresize = function(){
}
window.onscroll = function(){
}
window.history.back();
window.history.forward();
window.onscroll = function(){
var height = document.documentElement.scrollTop || document.body.scrollTop;
var width = document.documentElement.scrollLeft || document.body.scrollLeft;
}
window.scrollTo(left, top);
window.scrollTo({
left: 0,
top: 0,
beharior: 'smooth'
})
定时器:每间隔一段时间修改一下文本内容
- 间隔定时器
- 延时定时器,执行一次
- 定时器有返回值,表示是哪个定时器
var timer1 = setInterval(function(){
}, 1000);
var timer2 = setTimeout(function(){
}, 1000);
clearInterval(timer1);
cleatTimeout(timer2);
DOM Document Object Model 一套操作文档流相关内容的属性和方法
var element = document.getElementById('');
var element = document.getElementsByClassName('');
var element = document.getElementsByTagName('');
var element = document.querySelector('');
var element = document.querySelectorAll('');
var text = element.innerText;
element.innerText = "new text";
var text = element.innerHTML;
element.innerHTML = "<span>new text<span>";
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>
<p> 文本内容 </p>
<button id="on"> 修改内容 </button>
</div>
<script>
var but = document.querySelector('button');
var p = document.querySelector('p');
but.onclick = function(){
p.innerText = "设置内容";
}
p.onclick = function(){
p.innerHTML = "<span>new text<span>"
}
console.log(b.innerText);
</script>
</body>
</html>
console.log(element.id);
element.id = 'cc';
var value = element.getAttribute('name');
element.setAttribute('name', 'value');
element.removeAttribute('name');
var name = element.className;
element.className = 'new name';
var value = element.style.width;
element.style.width = value;
var value = window.getComputedStyle(element).width;
DOM 节点操作:创建、插入、删除、替换、克隆节点
var tag = document.createElement('tag name');
var fatherTag = document.querySelector('div');
fatherTag.appendChild(tag);
fatherTag.insertBefore(tag, signTag);
fatherTag.removeChild(tag);
tag.remove();
fatherTag.replaceChild(tag, signTag);
var tag_clone = tag.cloneNode(false);
获取元素尺寸:元素的宽高
var height = element.offsetHeight();
var width = element.offsetWidth();
var height = element.clientHeight();
var width = element.clientWidth();
回到顶部功能
- 滚动条滚动超过临界点,顶部通栏显示,否则隐藏滚动条滚动超过临界点,回
- 到顶部按钮显示,否则隐藏
- 点击回到顶部按钮,滚动条滚动回到顶部
布局结构:
- 需要一个顶部通栏标签和一个回到顶部按钮标签
- 让页面超过浏览器可视窗口高度
- 设置顶部通栏样式,默认是在超出页面的
- 设置回到顶部按钮样式,默认是在隐藏的
代码逻辑:
- 给浏览器绑定滚动事件,实时获取浏览器卷去的高度
- 判断卷去的高度决定隐藏还是显示
- 给回到顶部按钮绑定点击事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
height: 3000px;
}
.header {
width: 100%;
height: 80px;
display: flex;
justify-content: center;
align-items: center;
font-size: 30px;
color: #fff;
background-color: skyblue;
transition: top .5s linear;
position: fixed;
top: -80px;
left: 0;
}
.goTop {
width: 50px;
height: 50px;
background-color: pink;
font-size: 20px;
text-align: center;
line-height: 25px;
color: #fff;
position: fixed;
bottom: 50px;
right: 50px;
display: none;
}
</style>
</head>
<body>
<div class="header">顶部通知栏</div>
<div class="goTop">回到顶部</div>
<script>
var header = document.querySelector('.header');
var goTop = document.querySelector('.goTop');
window.onscroll = function () {
var height = document.documentElement.scrollTop || document.body.scrollTop;
if (height >= 300) {
header.style.top = '0px';
goTop.style.display = 'block';
} else {
header.style.top = '-80px';
goTop.style.display = 'none';
}
}
goTop.onclick = function () {
window.scrollTo({
top: 0,
behavior: 'smooth'
})
}
</script>
</body>
</html>
多项CheckBox选择卡全选功能
确认需求:
- 全选按钮点击的时候,根据自身状态决定所有选项按钮状态
- 点击每一一个选项按钮的时候,根据所有选项按钮状态决定全选按钮状态
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 100px;
padding: 20px;
border: 1px solid pink;
margin: 30px auto;
border-radius: 5px;
}
hr {
margin: 10px 0;
}
</style>
</head>
<body>
<div class="box">
全选:<input type="checkbox"> <br>
<hr>
<input type="checkbox"> 选项一 <br>
<input type="checkbox"> 选项二 <br>
<input type="checkbox"> 选项三 <br>
<input type="checkbox"> 选项四 <br>
</div>
<script>
var totalInputBtn = document.querySelector('input');
var childInputBtn = document.querySelectorAll('input:nth-child(n+2)');
totalInputBtn.onclick = function () {
var type = totalInputBtn.checked;
for (var i = 0; i < childInputBtn.length; i++) {
childInputBtn[i].checked = type;
}
}
for (var i = 0; i < childInputBtn.length; i++) {
childInputBtn[i].onclick = function () {
var flag = true;
for (var j = 0; j < childInputBtn.length; j++) {
if (childInputBtn[j].checked === false) {
flag = false;
break;
}
}
totalInputBtn.checked = flag;
}
}
</script>
</body>
</html>
选项卡:控制不同叠层的显示与隐藏
- 点击哪-一个按钮,其他按钮全部回归原始,当前高亮
- 点击哪一个按钮,其他盒子全部隐藏,当前索弓对应盒子显示
布局结构:
- 三个表示按钮的盒子,横向排列,初始一个高亮
- 三个显示内容的的盒子,在同一位置不同层级排列,初始一个显示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
ul, ol, li {
list-style: none;
}
.box {
width: 600px;
height: 400px;
border: 3px solid pink;
margin: 50px auto;
display: flex;
flex-direction: column;
}
.box > ul {
height: 60px;
display: flex;
}
.box > ul > li {
flex: 1;
color: #fff;
background-color: skyblue;
font-size: 30px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
}
.box > ul > li.active {
background-color: orange;
}
.box > ol {
flex: 1;
position: relative;
}
.box > ol > li {
width: 100%;
height: 100%;
background-color: purple;
display: flex;
justify-content: center;
align-items: center;
color: #fff;
font-size: 100px;
position: absolute;
left: 0;
top: 0;
display: none;
}
.box > ol > li.active {
display: flex;
}
</style>
</head>
<body>
<div class="box">
<ul>
<li class="active">1</li>
<li>2</li>
<li>3</li>
</ul>
<ol>
<li class="active">1</li>
<li>2</li>
<li>3</li>
</ol>
</div>
<script>
var btns = document.querySelectorAll('ul > li');
var tabs = document.querySelectorAll('ol > li')
btns.forEach(function (item, index) {
item.onclick = function () {
btns.forEach(function (t, i) {
t.className = '';
tabs[i].className = '';
})
item.className = 'active';
tabs[index].className = 'active';
}
})
</script>
</body>
</html>
动态渲染
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
table {
width: 300px;
text-align: center;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script>
var users = [
{id: 1, name: 'first', age: 18},
{id: 2, name: 'second', age: 19},
{id: 3, name: 'third', age: 20}
];
var tbody = document.querySelector('tbody');
users.forEach(function (item) {
var tr = document.createElement('tr');
for (var key in item) {
var td = document.createElement('td');
td.innerHTML = item[key];
tr.appendChild(td);
}
tbody.appendChild(tr);
})
</script>
</body>
</html>
事件绑定的三要素
- 事件源:和谁做好约定
- 事件类型:约定一个什么行为
- 事件处理函数: 当用户触发该行为的时候,执行什么代码
语法: 事件源. on事件类型 = 事件处理函数
事件类型:
事件对象:管理事件信息的数据结构
- 在绑定事件的时候,在事件函数中用一个 变量 来接收就可以,浏览器会自动传给这个变量
div.onclick = function(e){} ,这是e就是事件对象
事件常用信息:
鼠标事件
- offsetX与offsetY,是控件的坐标
- clientX与clientY,是可视窗口的坐标
- pageX与pageY,是文档页面的坐标
键盘事件
事件传播:浏览器响应事件机制
当我们点击了inner时(这个inner,可以叫目标),最先知道的是window,依次向内传播给inner(这个过程叫捕获阶段),之后再依次向外传播给window(这个过程叫冒泡阶段)
默认触发事件都是在冒泡阶段!
阻止事件传播:e.stopPropagation()
事件委托:利用事件冒泡机制,将自己的事件委托给结构父级中的某一层;同时利用事件的target属性来判别是那个对象:e.target.tagName == "DIV" ,比较是使用大写标签名
鼠标跟随:一个标记一直跟着鼠标走
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.sign {
width: 50px;
height: 50px;
background-color: red;
position: fixed;
left: 0;
top: 0;
}
</style>
</head>
<body>
<div class="sign"></div>
<script>
var divSign = document.querySelector('.sign');
document.onmousemove = function (e) {
var x = e.clientX;
var y = e.clientY;
divSign.style.left = x + 'px';
divSign.style.top = y + 'px';
}
</script>
</body>
</html>
轮播图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
ul, ol, li {
list-style: none;
}
image {
width: 100%;
height: 100%;
display: block;
}
.banner {
width: 100%;
height: 500px;
position: relative;
margin: 50px 0;
}
.banner > ul {
width: 100%;
height: 100%;
position: relative;
}
.banner > ul > li {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
opacity: 0;
transition: opacity .5s linear;
}
.banner > ul > li.active {
opacity: 1;
}
.banner > ol {
width: 200px;
height: 30px;
position: absolute;
left: 30px;
bottom: 30px;
background-color: rgba(0, 0, 0, .5);
display: flex;
justify-content: space-around;
align-items: center;
border-radius: 15px;
}
.banner > ol > li {
width: 20px;
height: 20px;
background-color: #fff;
border-radius: 50%;
cursor: pointer;
}
.banner > ol > li.active {
background-color: orange;
}
.banner > div{
width: 40px;
height: 60px;
position: absolute;
top: 50%;
transform: translateY(-50%);
background-color: rgba(0, 0, 0, .5);
display: flex;
justify-content: center;
align-items: center;
font-size: 30px;
color: #fff;
}
.banner > div.left {
left: 0;
}
.banner > div.right {
right: 0;
}
</style>
</head>
<body>
<div class="banner">
<ul class="imgBox">
<li class="active"><img src="./imgs/01.jpg" alt=""></li>
<li><img src="./imgs/02.jpg" alt=""></li>
<li><img src="./imgs/03.jpg" alt=""></li>
<li><img src="./imgs/04.jpg" alt=""></li>
</ul>
<ol>
<li data-i="0" data-name="point" class="active"></li>
<li data-i="1" data-name="point"></li>
<li data-i="2" data-name="point"></li>
<li data-i="3" data-name="point"></li>
</ol>
<div class="left"><</div>
<div class="right">></div>
</div>
<script>
var imgs = document.querySelectorAll('ul > li');
console.log(imgs)
var points = document.querySelectorAll('ol > li');
console.log(points)
var index = 0;
function changeOne(type) {
imgs[index].className = '';
points[index].className = '';
if (type === true) {
index++;
} else if (type === false) {
index--;
} else {
index = type;
}
if (index >= imgs.length) {
index = 0;
}
if (index < 0) {
index = imgs.length - 1;
}
imgs[index].className = 'active';
points[index].className = 'active';
}
var banner = document.querySelector('.banner');
banner.onclick = function (e) {
if (e.target.className === 'left') {
changeOne(false);
console.log('left')
}
if (e.target.className === 'right') {
changeOne(true);
console.log('right')
}
if (e.target.dataset.name === 'point') {
var i = e.target.dataset.i - 0;
changeOne(i);
console.log('here')
}
}
setInterval(function () {
changeOne(true);
}, 3000);
</script>
</body>
</html>
vs内常用快捷键
- 按css选择器,输入字符,然后回车,对生成对应div标签
- 输入
ul>li*3{$} 接回车,可以生成,表格和表格项 ul.imgBox>li*4>img[src=./imgs/0$.jpg]
|