01-页面滚动事件
<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>Documenttitle>
<style>
body {
height: 3000px;
}
div {
width: 200px;
height: 200px;
background-color: pink;
overflow: auto;
}
style>
head>
<body>
<div>我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字div>
<script>
let div = document.querySelector('div');
div.addEventListener('scroll', function() {
alert(1)
})
script>
body>
html>
效果展示
02-页面加载事件
<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>Documenttitle>
head>
<body>
<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>Documenttitle>
<style>
body {
height: 3000px;
}
div {
width: 200px;
height: 200px;
background-color: pink;
overflow: auto;
}
style>
<script>
document.addEventListener('DOMContentLoaded', function() {
let div = document.querySelector('div');
console.log(div);
})
script>
head>
<body>
<div>我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字我里面可以放很多的文字div>
body>
html>
body>
html>
03-三大家族宽高对比
<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>Documenttitle>
<style>
* {
padding: 0;
margin: 0;
}
div {
width: 150px;
height: 150px;
background-color: pink;
overflow: auto;
padding: 10px;
border: 10px solid red;
margin: 100px;
}
style>
head>
<body>
<div>我有很多很多内容我有很多很多内容我有很多很多内容我有很多很多内容我有很多很多内容我有很多很多内容我有很多很多内容我有很多很多内容我有很多很多内容我有很多很多内容我有很多很多内容我有很多很多内容我有很多很多内容我有很多很多内容我有很多很多内容我有很多很多内容我有很多很多内容我有很多很多内容我有很多很多内容我有很多很多内容我有很多很多内容我有很多很多内容div>
<script>
let div = document.querySelector('div');
console.log(div.scrollHeight);
console.log(div.scrollWidth);
console.log('---------------------------------------------------------------------------------------');
console.log(div.offsetWidth);
console.log(div.offsetHeight);
console.log(div.offsetLeft);
console.log(div.offsetTop);
console.log('---------------------------------------------------------------------------------------');
console.log(div.clientHeight);
console.log(div.clientWidth);
console.log(div.clientLeft);
console.log(div.clientTop);
script>
body>
html>
04-检测页面滚动的距离
<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>Documenttitle>
<style>
body {
height: 3000px;
}
style>
head>
<body>
<script>
window.addEventListener('scroll', function() {
console.log(document.documentElement.scrollTop);
document.documentElement.scrollTop = 500;
})
script>
body>
html>
05-电梯导航案例
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Documenttitle>
<style>
* {
margin: 0;
padding: 0;
}
body {
height: 3000px;
}
.aside {
position: fixed;
left: 0;
top: 50%;
transform: translateY(-50%);
}
.item {
height: 40px;
line-height: 40px;
text-align: center;
padding: 0 10px;
cursor: pointer;
}
.active {
background-color: red;
color: #fff;
}
.content {
width: 660px;
margin: 400px auto;
}
.neirong {
height: 300px;
margin-bottom: 20px;
color: #fff;
}
.content1 {
background-color: red;
}
.content2 {
background-color: blue;
}
.content3 {
background-color: orange;
}
.content4 {
background-color: yellowgreen;
}
style>
head>
<body>
<div class="aside">
<div class="item active">男装/女装div>
<div class="item">儿童服装/游乐园div>
<div class="item">电子产品div>
<div class="item">电影/美食div>
div>
<div class="content">
<div class="neirong content1">男装/女装div>
<div class="neirong content2">儿童服装/游乐园div>
<div class="neirong content3">电子产品div>
<div class="neirong content4">电影/美食div>
div>
<script>
let items = document.querySelectorAll('.item');
let contents = document.querySelectorAll('.neirong');
for (let i = 0; i < items.length; i++) {
items[i].addEventListener('click', function() {
document.querySelector('.aside .active').classList.remove('active');
this.classList.add('active');
document.documentElement.scrollTop = contents[i].offsetTop;
})
}
script>
body>
html>
效果展示
06-client家族
<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>client家族title>
head>
<body>
<script>
window.addEventListener('resize', function() {
let w = document.documentElement.clientWidth;
if (w >= 1500) {
document.body.style.backgroundColor = 'pink';
} else if (w > 540) {
document.body.style.backgroundColor = 'yellow'
} else {
document.body.style.backgroundColor = 'blue';
}
})
script>
body>
html>
07-仿京东固定头部
<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>Documenttitle>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.content {
overflow: hidden;
width: 1000px;
height: 3000px;
background-color: pink;
margin: 0 auto;
}
.backtop {
display: none;
width: 50px;
left: 50%;
margin: 0 0 0 505px;
position: fixed;
bottom: 60px;
z-index: 100;
}
.backtop a {
height: 50px;
width: 50px;
background: url(./images/bg2.png) 0 -600px no-repeat;
opacity: 0.35;
overflow: hidden;
display: block;
text-indent: -999em;
cursor: pointer;
}
.header {
position: fixed;
top: -80px;
left: 0;
width: 100%;
height: 80px;
background-color: purple;
text-align: center;
color: #fff;
line-height: 80px;
font-size: 30px;
transition: all .3s;
}
.sk {
width: 300px;
height: 300px;
background-color: skyblue;
margin-top: 500px;
}
style>
head>
<body>
<div class="header">我是顶部导航栏div>
<div class="content">
<div class="sk">秒杀模块div>
div>
<div class="backtop">
<img src="./images/close2.png" alt="">
<a href="javascript:;">a>
div>
<script>
let sk = document.querySelector('.sk')
let header = document.querySelector('.header')
window.addEventListener('scroll', function() {
if (document.documentElement.scrollTop >= sk.offsetTop) {
header.style.top = '0';
} else {
header.style.top = '-80px';
}
})
script>
body>
html>
效果展示
08-仿新浪返回顶部
<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>Documenttitle>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.content {
width: 1000px;
height: 3000px;
background-color: pink;
margin: 0 auto;
}
.backtop {
display: none;
width: 50px;
left: 50%;
margin: 0 0 0 505px;
position: fixed;
bottom: 60px;
z-index: 100;
}
.backtop a {
height: 50px;
width: 50px;
background: url(./images/bg2.png) 0 -600px no-repeat;
opacity: 0.35;
overflow: hidden;
display: block;
text-indent: -999em;
cursor: pointer;
}
style>
head>
<body>
<div class="content">div>
<div class="backtop">
<img src="./images/close2.png" alt="">
<a href="javascript:;">a>
div>
<script>
let backtop = document.querySelector('.backtop');
window.addEventListener('scroll', function() {
let num = document.documentElement.scrollTop;
if (num >= 500) {
backtop.style.display = 'block';
} else {
backtop.style.display = 'none';
}
})
backtop.children[1].addEventListener('click', function() {
document.documentElement.scrollTop = 0;
})
script>
body>
html>
效果展示
09-轮播图案例
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>QQ音乐轮播图title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
li {
list-style: none;
}
.main {
width: 700px;
margin: auto;
background: #000;
}
.slides {
height: 320px;
position: relative;
}
.slides ul li {
position: absolute;
top: 0;
left: 0;
opacity: 0;
transition: all .3s;
}
.slides li.active {
opacity: 1;
}
.slides .extra {
width: 700px;
height: 53px;
line-height: 53px;
position: absolute;
bottom: 0px;
background-color: rgba(0, 0, 0, 0.8);
z-index: 10;
}
.slides .extra h3 {
width: 82%;
margin: 0;
margin-right: 20px;
padding-left: 20px;
color: #98E404;
font-size: 28px;
float: left;
font-weight: 500;
font-family: "Microsoft Yahei", Tahoma, Geneva;
}
.slides .extra a {
width: 30px;
height: 29px;
display: block;
float: left;
margin-top: 12px;
margin-right: 3px;
background-image: url(./assets/icon_focus_switch.png);
}
.slides .extra .prev {
background-position: 0 0;
}
.slides .extra .prev:hover {
background-position: -30px 0;
}
.slides .extra .next {
background-position: -60px 0;
}
.slides .extra .next:hover {
background-position: -90px 0;
}
.indicator {
padding: 10px 0;
}
.indicator ul {
list-style-type: none;
margin: 0 0 0 4px;
padding: 0;
overflow: hidden;
}
.indicator ul li {
position: relative;
float: left;
width: 60px;
margin: 0 4px 0 5px;
text-align: center;
cursor: pointer;
}
.indicator li img {
display: block;
border: 0;
text-align: center;
width: 60px;
}
.indicator li .mask {
width: 60px;
height: 60px;
position: absolute;
top: 0;
left: 0;
background-color: rgba(0, 0, 0, 0.4);
}
.indicator li .border {
display: none;
width: 54px;
position: absolute;
bottom: 0;
left: 0;
z-index: 20;
border: 3px solid #98E404;
}
.indicator .active .mask {
display: none;
}
.indicator .active .border {
display: block;
}
style>
head>
<body>
<div class="main">
<div class="slides">
<ul>
<li class="active">
<a href="#"><img src="./assets/b_01.jpg" alt="第1张图的描述信息">a>
li>
<li>
<a href="#"><img src="./assets/b_02.jpg" alt="第2张图的描述信息">a>
li>
<li>
<a href="#"><img src="./assets/b_03.jpg" alt="第3张图的描述信息">a>
li>
<li>
<a href="#"><img src="./assets/b_04.jpg" alt="第4张图的描述信息">a>
li>
<li>
<a href="#"><img src="./assets/b_05.jpg" alt="第5张图的描述信息">a>
li>
<li>
<a href="#"><img src="./assets/b_06.jpg" alt="第6张图的描述信息">a>
li>
<li>
<a href="#"><img src="./assets/b_07.jpg" alt="第7张图的描述信息">a>
li>
<li>
<a href="#"><img src="./assets/b_08.jpg" alt="第8张图的描述信息">a>
li>
<li>
<a href="#"><img src="./assets/b_09.jpg" alt="第9张图的描述信息">a>
li>
<li>
<a href="#"><img src="./assets/b_10.jpg" alt="第9张图的描述信息">a>
li>
ul>
<div class="extra">
<h3>第1张图的描述信息h3>
<a class="prev" href="javascript:;">a>
<a class="next" href="javascript:;">a>
div>
div>
<div class="indicator">
<ul>
<li class="active">
<img src="assets/s_01.jpg">
<span class="mask">span>
<span class="border">span>
li>
<li>
<img src="assets/s_02.jpg">
<span class="mask">span>
<span class="border">span>
li>
<li>
<img src="assets/s_03.jpg">
<span class="mask">span>
<span class="border">span>
li>
<li>
<img src="assets/s_04.jpg">
<span class="mask">span>
<span class="border">span>
li>
<li>
<img src="assets/s_05.jpg">
<span class="mask">span>
<span class="border">span>
li>
<li>
<img src="assets/s_06.jpg">
<span class="mask">span>
<span class="border">span>
li>
<li>
<img src="assets/s_07.jpg">
<span class="mask">span>
<span class="border">span>
li>
<li>
<img src="assets/s_08.jpg">
<span class="mask">span>
<span class="border">span>
li>
<li>
<img src="assets/s_09.jpg">
<span class="mask">span>
<span class="border">span>
li>
<li>
<img src="assets/s_10.jpg">
<span class="mask">span>
<span class="border">span>
li>
ul>
div>
div>
<script>
let lis = document.querySelectorAll('.indicator li');
let piclis = document.querySelectorAll('.slides ul li');
let text = document.querySelector('.extra h3');
let next = document.querySelector('.next');
let prev = document.querySelector('.prev');
let main = document.querySelector('.main');
for (let i = 0; i < lis.length; i++) {
lis[i].addEventListener('mouseenter', function() {
document.querySelector('.indicator .active').classList.remove('active');
this.classList.add('active')
document.querySelector('.slides ul .active').classList.remove('active');
piclis[i].classList.add('active');
text.innerHTML = '第' + (i + 1) + '张图片的描述信息';
index = i;
})
}
let index = 0;
next.addEventListener('click', function() {
index++;
index = index % lis.length;
common();
})
prev.addEventListener('click', function() {
index--;
index = (lis.length + index) % lis.length
common();
})
function common() {
document.querySelector('.indicator .active').classList.remove('active');
lis[index].classList.add('active');
document.querySelector('.slides ul .active').classList.remove('active');
piclis[index].classList.add('active');
text.innerHTML = '第' + (index + 1) + '张图片的描述信息';
}
let timer = setInterval(function() {
next.click();
}, 1000)
main.addEventListener('mouseenter', function() {
clearInterval(timer)
})
main.addEventListener('mouseleave', function() {
timer = setInterval(function() {
next.click();
}, 1000)
})
script>
body>
html>
效果展示