<!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;
}
.father{
width: 200px;
height: 200px;
background-color: pink;
margin: 150px;
position: relative;
}
.son{
width: 100px;
height: 100px;
background-color: yellow;
margin-left: 45px;
}
.w{
width: 200px;
height: 200px;
background-color: skyblue;
margin: 0 auto 200px;
padding: 10px;
border: 15px solid red;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
<div class="w"></div>
<script>
//offset系列
var father = document.querySelector('.father');
var son = document.querySelector('.son');
//1.可以得到元素的偏移 位置 返回的不带单位的数值
console.log(father.offsetTop);
console.log(father.offsetLeft);
//它以带有定位的父亲为准 如果没有父亲 或者父亲没有定位 则以body为准
console.log(son.offsetTop);
console.log(son.offsetLeft);
//2.可以得到元素的大小 宽度和高度 是包含padding + border + width
var w = document.querySelector('.w');
console.log(w.offsetWidth);
console.log(w.offsetHeight);
//3.返回带有定位的父亲 否则返回的是body
console.log(son.offsetParent);//返回带有定位的父亲 否则一级一级往上找 知道找到带有定位的父亲为止
console.log(son.parentNode);//返回父亲 是最近一级的父亲 亲爸爸 不管有没有定位
</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>获取鼠标在盒子内的坐标</title>
<style>
.box{
width: 200px;
height: 200px;
background-color: pink;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="box">
<div class="son"></div>
</div>
<script>
var box = document.querySelector('.box');
box.addEventListener('mousemove', function(e){
console.log('x坐标为:' + (e.pageX - box.offsetLeft));
console.log('y坐标为:' + (e.pageY - box.offsetTop));
});
</script>
</body>
</html>
很重要的模态框案例
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.login-header {
width: 100%;
text-align: center;
height: 30px;
font-size: 24px;
line-height: 30px;
}
ul,
li,
ol,
dl,
dt,
dd,
div,
p,
span,
h1,
h2,
h3,
h4,
h5,
h6,
a {
padding: 0px;
margin: 0px;
}
.login {
display: none;
width: 512px;
height: 280px;
position: fixed;
border: #ebebeb solid 1px;
left: 50%;
top: 50%;
background: #ffffff;
box-shadow: 0px 0px 20px #ddd;
z-index: 9999;
transform: translate(-50%, -50%);
}
.login-title {
width: 100%;
margin: 10px 0px 0px 0px;
text-align: center;
line-height: 40px;
height: 40px;
font-size: 18px;
position: relative;
cursor: move;/* 十字架型鼠标 */
}
.login-input-content {
margin-top: 20px;
}
.login-button {
width: 50%;
margin: 30px auto 0px auto;
line-height: 40px;
font-size: 14px;
border: #ebebeb 1px solid;
text-align: center;
}
.login-bg {
display: none;
width: 100%;
height: 100%;
position: fixed;
top: 0px;
left: 0px;
background: rgba(0, 0, 0, .3);
}
a {
text-decoration: none;
color: #000000;
}
.login-button a {
display: block;
}
.login-input input.list-input {
float: left;
line-height: 35px;
height: 35px;
width: 350px;
border: #ebebeb 1px solid;
text-indent: 5px;
}
.login-input {
overflow: hidden;
margin: 0px 0px 20px 0px;
}
.login-input label {
float: left;
width: 90px;
padding-right: 10px;
text-align: right;
line-height: 35px;
height: 35px;
font-size: 14px;
}
.login-title span {
position: absolute;
font-size: 12px;
right: -20px;
top: -30px;
background: #ffffff;
border: #ebebeb solid 1px;
width: 40px;
height: 40px;
border-radius: 20px;
}
</style>
</head>
<body>
<div class="login-header"><a id="link" href="javascript:;">点击,弹出登录框</a></div>
<div id="login" class="login">
<div id="title" class="login-title">登录会员
<span><a id="closeBtn" href="javascript:void(0);" class="close-login">关闭</a></span>
</div>
<div class="login-input-content">
<div class="login-input">
<label>用户名:</label>
<input type="text" placeholder="请输入用户名" name="info[username]" id="username" class="list-input">
</div>
<div class="login-input">
<label>登录密码:</label>
<input type="password" placeholder="请输入登录密码" name="info[password]" id="password" class="list-input">
</div>
</div>
<div id="loginBtn" class="login-button"><a href="javascript:void(0);" id="login-button-submit">登录会员</a></div>
</div>
<!-- 遮盖层 -->
<div id="bg" class="login-bg"></div>
<script>
//1.获取元素
var login = document.querySelector('.login');
var mask = document.querySelector('.login-bg');
var link = document.querySelector('#link');
var closeBtn = document.querySelector('#closeBtn');
//鼠标只在title的范围内才能拖拽
var title = document.querySelector('#title');
//2.点击弹出层这个链接 link 让mask 和login 显示出来
link.addEventListener('click', function(){
mask.style.display = 'block';
login.style.display = 'block';
});
//3.点击关闭mask和login
closeBtn.addEventListener('click', function(){
mask.style.display = 'none';
login.style.display = 'none';
});
//4.开始拖拽
//(1)当我们鼠标按下就获得鼠标在盒子内的坐标
title.addEventListener('mousedown', function(e){
var x = e.pageX - login.offsetLeft;
var y = e.pageY - login.offsetTop;
//(2)鼠标移动的时候,把鼠标页面中的坐标,减去 鼠标在盒子内的坐标就是模态框的left和top值了
document.addEventListener('mousemove', move);
function move(e){
login.style.left = e.pageX - x + 'px';
login.style.top = e.pageY - y + 'px';
}
//(3)鼠标弹起,就让鼠标移动事件移除
document.addEventListener('mouseup', function(e){
document.removeEventListener('mousemove', move);
});
});
</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>立即执行函数</title>
</head>
<body>
<script>
// 1.立即执行函数:不需要调用,立马能够自己执行的函数
function fn(){
console.log(1);
}
fn();
//2.写法 也可以传递参数进来
// (function(){})(); 或者 (function(){}());
(function(a, b){
console.log(a + b);
var num = 10;//局部变量
})(1, 2);//第二个小括号可以看看做调用函数
(function sum(a, b){
console.log(a + b);
var num = 10;//局部变量
}(2, 3));
//3.立即执行函数最大的作用就是独立创建了一个作用域,里面所有的变量都是局部变量不会有命名冲突的情况
</script>
</body>
</html>
pageshow事件可以解决火狐浏览器的页面刷新bug
resize事件表示页面大小发生变化时触发
flexible分析
(function flexible(window, document) {
// 获取的html 的根元素
var docEl = document.documentElement
// dpr 物理像素比
var dpr = window.devicePixelRatio || 1
// adjust body font size 设置我们body 的字体大小
function setBodyFontSize() {
// 如果页面中有body 这个元素 就设置body的字体大小
if (document.body) {
document.body.style.fontSize = (12 * dpr) + 'px'
} else {
// 如果页面中没有body 这个元素,则等着 我们页面主要的DOM元素加载完毕再去设置body
// 的字体大小
document.addEventListener('DOMContentLoaded', setBodyFontSize)
}
}
setBodyFontSize();
// set 1rem = viewWidth / 10 设置我们html 元素的文字大小
function setRemUnit() {
var rem = docEl.clientWidth / 10
docEl.style.fontSize = rem + 'px'
}
setRemUnit()
// reset rem unit on page resize 当我们页面尺寸大小发生变化的时候,要重新设置下rem 的大小
window.addEventListener('resize', setRemUnit)
// pageshow 是我们重新加载页面触发的事件
window.addEventListener('pageshow', function(e) {
// e.persisted 返回的是true 就是说如果这个页面是从缓存取过来的页面,也需要从新计算一下rem 的大小
if (e.persisted) {
setRemUnit()
}
})
// detect 0.5px supports 有些移动端的浏览器不支持0.5像素的写法
if (dpr >= 2) {
var fakeBody = document.createElement('body')
var testElement = document.createElement('div')
testElement.style.border = '.5px solid transparent'
fakeBody.appendChild(testElement)
docEl.appendChild(fakeBody)
if (testElement.offsetHeight === 1) {
docEl.classList.add('hairlines')
}
docEl.removeChild(fakeBody)
}
}(window, document))
<!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>
div{
width: 200px;
height: 200px;
background-color: pink;
/* text-align: center;
line-height: 200px; */
overflow: auto;
}
</style>
</head>
<body>
<div>
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
我是内容
</div>
<script>
//scroll系列
var div = document.querySelector('div');
console.log(div.scrollHeight);//返回自身实际的高度,不含边框,返回值不带单位
console.log(div.clientHeight);
// scroll 是滚动事件 当滚动条发生变化会触发
div.addEventListener('scroll',function(){
console.log(div.scrollTop);
});
</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>
.slider-bar {
position: absolute;
left: 50%;
top: 300px;
margin-left: 600px;
width: 45px;
height: 130px;
background-color: pink;
}
.w {
width: 1200px;
margin: 10px auto;
}
.header {
height: 150px;
background-color: purple;
}
.banner {
height: 250px;
background-color: skyblue;
}
.main {
height: 1000px;
background-color: yellowgreen;
}
span {
display: none;
position: absolute;
bottom: 0;
}
</style>
</head>
<body>
<div class="slider-bar">
<span class="goBack">返回顶部</span>
</div>
<div class="header w">头部区域</div>
<div class="banner w">banner</div>
<div class="main w">主体部分</div>
<script>
//1.获取元素
var sliderBar = document.querySelector('.slider-bar');
var banner = document.querySelector('.banner');
var bannerTop = banner.offsetTop;//一定要写在外面 防止页面滚动的时候不准确
var sliderBarTop = sliderBar.offsetTop - bannerTop;//一定要写在外面 防止页面滚动的时候不准确
//获取main元素 主体元素
var main = document.querySelector('.main');
var goBack = document.querySelector('.goBack');
var mainTop = main.offsetTop;//一定要写在外面 防止页面滚动的时候不准确
//2.页面滚动事件 scroll
document.addEventListener('scroll', function(e){
//pageYOffset整个页面被卷去的头部
//console.log(window.pageYOffset);
//3.当我们页面被卷去的头部大于等于了172此时 侧边栏就要改为固定定位
if(window.pageYOffset >= bannerTop){
sliderBar.style.position = 'fixed';
sliderBar.style.top = sliderBarTop + 'px';
}else{
sliderBar.style.position = 'absolute';
sliderBar.style.top = '300px';
}
// 4.当我们页面滚动到main盒子,就显示goback模块
if(window.pageYOffset >= mainTop){
goBack.style.display = 'block';
}else{
goBack.style.display = 'none';
}
});
</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>
div{
position: absolute;
left: 0px;
width: 100px;
height: 100px;
background-color: pink;
}
</style>
</head>
<body>
<div></div>
<script>
//动画原理
// 1.获得盒子当前位置
// 2.让盒子在当前位置加上一个移动距离
// 3.利用定时器不断重复这个操作
// 4.加一个结束定时器的条件
// 5.注意此元素需要添加定位,才能使用element.style.left
var div = document.querySelector('div');
var timer = window.setInterval(function(){
if(div.offsetLeft >= 400){
//停止动画 本质是停止定时器
window.clearInterval(timer);
}
div.style.left = (div.offsetLeft + 1) + 'px';
},