JavaScript Day 6
上课日期:2019年8月15日
课程内容
这次的三个案例都是用js代码实现的对网页上图片的一个处理,我们在浏览网页的时候,如果图片全部摆成一行,或者摆在一个地方,很容易影响页面美观,所以使用三维轮播等效果,就能使美观性大大提高。
一、案例:三维图片轮播
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
body {
background: black;
}
.banner {
position: relative;
width: 900px;
height: 350px;
margin: auto;
}
.imglist {
width: 400px;
height: 200px;
position: absolute;
box-shadow: 0 0 15px #fff;
transition: all .1s linear;
}
.imglist > img {
width: 100%;
height: 100%;
}
.items {
position: absolute;
z-index: 10;
left: 0;
bottom: -40px;
width: 100%;
text-align: center;
}
.items > div {
width: 17px;
height: 17px;
margin: 0 5px;
border: 2px solid #fff;
box-sizing: border-box;
display: inline-block;
transform: rotate(45deg);
}
.bgcolor {
background: #17ff1e;
}
</style>
</head>
<body>
<div class="banner">
<div class="imglist" style="left: 250px;top: 150px;z-index: 5">
<img src="./img/1.jpg" alt=""/>
</div>
<div class="imglist" style="left:0px;top: 100px;z-index: 4">
<img src="./img/2.jpg" alt=""/>
</div>
<div class="imglist" style="left:0px;top: 50px;z-index: 3">
<img src="./img/3.jpg" alt=""/>
</div>
<div class="imglist" style="left:250px;top: 0px;z-index: 2">
<img src="./img/4.jpg" alt=""/>
</div>
<div class="imglist" style="left:500px;top: 50px;z-index: 3">
<img src="./img/5.jpg" alt=""/>
</div>
<div class="imglist" style="left:500px;top: 100px;z-index: 4">
<img src="./img/6.jpg" alt=""/>
</div>
<div class="items">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</div>
<script>
/*
* 1.网页加载完成 默认转动
*
* */
var lists = document.getElementsByClassName("imglist");
var item = document.getElementsByClassName("items")[0].children;
var speed = 1500;
var timer = null;
var index = 0//记录当前正面的那张图的索引
var count = 0;//用户操作的时候转的次数
var isanimate = true;//记录动画的状态
function animate() {
timer = setInterval(function () {
ShunAnimate();
addColor();
}, speed);
}
//顺时针动画
function ShunAnimate() {
var ele_0 = [lists[0].style.left, lists[0].style.top, lists[0].style.zIndex];
for (var i = 0; i < lists.length; i++) {
if (i < lists.length - 1) {
lists[i].style.left = lists[i + 1].style.left;
lists[i].style.top = lists[i + 1].style.top;
lists[i].style.zIndex = lists[i + 1].style.zIndex;
}
else {
lists[i].style.left = ele_0[0];
lists[i].style.top = ele_0[1];
lists[i].style.zIndex = ele_0[2];
}
if (lists[i].style.zIndex == 5) {
index = i;
}
}
}
function addMouseEnter() {
for (var i = 0; i < lists.length; i++) {
lists[i].onmouseenter = function () {
//停止计时器
clearInterval(timer);
}
lists[i].onmouseleave = function () {
animate();
}
}
}
function addColor() {
//下面的点跟随变化的一个方法
for (var i = 0; i < item.length; i++) {
//当前变色
if (i == index) {
item[i].className = "bgcolor";
}
else {
item[i].className = "";
}
}
}
function additemEvent() {
for (var i = 0; i < item.length; i++) {
item[i].index = i;
item[i].onmouseenter = function () {
clearInterval(timer);
if (isanimate) {
isanimate = false;
}
else {
return;
}
//进入当前的变色
count = this.index - index;
//顺时针动画
var num = 0;
var c = 0;
if (this.index > index) {
c = item.length - count;
}
else if (this.index < index) {
c = index - this.index;
}
loop();
function loop() {
num++;
if (num > c) {
console.log("动画完成");
isanimate = true;
return;
}
ShunAnimate();//0.3ms
setTimeout(loop, 100);
}
index = this.index;
addColor();
}
item[i].onmouseleave = function () {
animate();
}
}
}
window.onload = function () {
animate();
addMouseEnter();
addColor();
additemEvent();
}
</script>
</body>
</html>
二、案例:横向滚动
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
* {
margin: 0;
padding: 0;
}
.par {
position: relative;
width: 600px;
height: 300px;
border: 1px solid black;
margin: auto;
overflow: hidden;
}
.nav {
width: 4200px;
height: 300px;
}
.tras {
transition: margin-left 0.5s ease-in;
}
.nav > li {
width: 600px;
height: 300px;
float: left;
list-style: none;
}
.nav > li img {
width: 100%;
height: 100%;
}
.dian {
position: absolute;
width: 100%;
height: auto;
left: 0;
bottom: 0;
text-align: center;
}
.dian > div {
width: 13px;
height: 13px;
border-radius: 50%;
border: 1px solid #fff;
display: inline-block;
}
.bgcolor {
background: red;
}
.btn {
position: absolute;
top: 140px;
z-index: 11;
width: 100%;
}
.btn > span {
display: block;
width: 30px;
height: 30px;
background: rgba(0, 0, 0, 0.69);
color: #fff;
text-align: center;
line-height: 30px;
}
.btn > span:nth-child(1) {
border-radius: 0 50% 50% 0;
float: left;
}
.btn > span:nth-child(2) {
border-radius: 50% 0 0 50%;
float: right;
}
</style>
</head>
<body>
<div class="par">
<ul class="nav tras">
<li>
<img src="img/1.jpg">
</li>
<li>
<img src="img/2.jpg">
</li>
<li>
<img src="img/3.jpg">
</li>
<li>
<img src="img/4.jpg">
</li>
<li>
<img src="img/5.jpg">
</li>
<li>
<img src="img/6.jpg">
</li>
<li>
<img src="img/1.jpg">
</li>
</ul>
<div class="dian">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div class="btn">
<span><</span>
<span>></span>
</div>
</div>
<script>
/*
* 1.初始动画
* */
var nav = document.getElementsByClassName("nav")[0];
var par = document.getElementsByClassName("par")[0];
var dian = document.getElementsByClassName("dian")[0].children;
var btnspan = document.getElementsByClassName("btn")[0].children;
var speed = 2000;
var count = 0;
var index = 0;
var timer = null;
function animate() {
timer = setInterval(function () {
transanimte();
}, speed)
}
function transanimte() {
nav.className = "nav tras";
count++;
nav.style.marginLeft = (-600 * count) + "px";
setTimeout(function () {
//动画完成之后去做判断
if (count >= nav.children.length - 1) {
nav.className = "nav";
count = 0;
nav.style.marginLeft = "0px";
}
index = count == 6 ? 0 : count;
addColor();
}, 500);
}
function addColor() {
for (var i = 0; i < dian.length; i++) {
if (i == index) {
dian[i].className = "bgcolor";
}
else {
dian[i].className = "";
}
}
}
window.onload = function () {
animate();
addColor();
//事件
par.onmouseenter = function () {
clearInterval(timer);
}
par.onmouseleave = function () {
animate();
}
for (var i = 0; i < dian.length; i++) {
dian[i].index = i;
dian[i].onmouseenter = function () {
count = this.index - 1;
transanimte();
index = this.index;
addColor();
}
}
//两箭头事件
btnspan[0].onclick = function () {
transanimte();
}
//右边 比较难写
btnspan[1].onclick = function () {
count--;
if (count < 0) {
nav.className = "nav";
count = nav.children.length - 2;
nav.style.marginLeft = "-3600px";
}
//稍微上下代码有点延迟
setTimeout(function () {
nav.className = "nav tras";
nav.style.marginLeft = (-600 * count) + "px";
//动画完成之后
setTimeout(function () {
index = count;
addColor();
}, 500)
}, 1);
}
}
</script>
</body>
</html>
三、案例:淡入淡出
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.par {
width: 600px;
height: 400px;
border: 1px solid black;
margin: auto;
position: relative;
}
.par > div.items {
position: absolute;
width: 100%;
height: 100%;
opacity: 0;
transition: opacity 0.6s ease-in-out;
}
.par > div > a img {
width: 100%;
height: 100%;
border: 0;
}
.nav {
position: absolute;
z-index: 10;
width: 100%;
left: 0;
bottom: 0;
text-align: center;
}
.nav > div {
width: 12px;
height: 12px;
border-radius: 50%;
border: 2px solid #fff;
box-shadow: 0 0 10px #fff;
display: inline-block;
}
.bgcolor {
background: red;
}
.btn {
position: absolute;
top: 180px;
z-index: 11;
width: 100%;
}
.btn > span {
display: block;
width: 30px;
height: 30px;
background: rgba(0, 0, 0, 0.69);
color: #fff;
text-align: center;
line-height: 30px;
}
.btn > span:nth-child(1) {
border-radius: 0 50% 50% 0;
float: left;
}
.btn > span:nth-child(2) {
border-radius: 50% 0 0 50%;
float: right;
}
</style>
</head>
<body>
<div class="par">
<div class="items"><a href="#"><img src="./img/1.jpg" alt=""/></a></div>
<div class="items"><a href="#"><img src="./img/2.jpg" alt=""/></a></div>
<div class="items"><a href="#"><img src="./img/3.jpg" alt=""/></a></div>
<div class="items"><a href="#"><img src="./img/4.jpg" alt=""/></a></div>
<div class="items"><a href="#"><img src="./img/5.jpg" alt=""/></a></div>
<div class="items"><a href="#"><img src="./img/6.jpg" alt=""/></a></div>
<div class="nav">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div class="btn">
<span><</span>
<span>></span>
</div>
</div>
<script>
/*
* 淡入淡出动画
* 1.设置层的情况
* 2.写层的动画
* 3.添加淡入淡出动画
* 4.写圈圈动画 默认第一个有颜色 ---对应的图片对应的圈 变化
*5.添加事件 鼠标进入和离开
* 6.给点添加事件
* 7.当前点变色 显示当前对应的图片
* 8.给两个箭头添加点击事件操作动画
* */
var items = document.getElementsByClassName("items");
var dian = document.getElementsByClassName("nav")[0].children;
var btnspan = document.getElementsByClassName("btn")[0].children;
var parentEle = document.getElementsByClassName("par")[0];
var speed = 2000;
items[0].style.opacity = 1;
var index = 0;
var timer = null;
//设置层
function attrZindex() {
for (var i = 0; i < items.length; i++) {
items[i].style.zIndex = items.length - i - 1;
}
}
//动画方法
function animate() {
timer = setInterval(function () {
setindexopacity("right", 1);
addColor();
}, speed);
}
function setindexopacity(res, count) {
for (var k = 0; k < count; k++) {
for (var i = 0; i < items.length; i++) {
var zindex = parseInt(items[i].style.zIndex);
if (res == "right") {
zindex += 1;
if (zindex >= items.length) {
items[i].style.opacity = 0;
zindex = 0;
}
if (zindex == items.length - 1) {
items[i].style.opacity = 1;
//动态修改index 的值
index = i;
}
}
else {
zindex -= 1;
if (zindex == -1) {
items[i].style.opacity = 1;
zindex = items.length - 1;
//动态修改index 的值
index = i;
}
else if (zindex == items.length - 2) {
items[i].style.opacity = 0;
}
}
items[i].style.zIndex = zindex;
}
}
}
//点颜色的相关操作
function addColor() {
for (var i = 0; i < dian.length; i++) {
if (i == index) {
dian[i].className = "bgcolor";
}
else {
dian[i].className = "";
}
}
}
window.onload = function () {
attrZindex();
animate();
addColor();
//操作动画 停止开启
parentEle.onmouseenter = function () {
clearInterval(timer);
}
parentEle.onmouseleave = function () {
animate();
}
//给点添加事件
for (var i = 0; i < dian.length; i++) {
dian[i].index = i;
dian[i].onmouseenter = function () {
//鼠标进入当前点变色
//使用当前的索引 -之前的索引
var count = this.index - index > 0 ? this.index - index : dian.length + (this.index - index);
setindexopacity("right", count);
index = this.index;
addColor();
}
}
//给left right btn 事件
//left
btnspan[0].onclick = function () {
setindexopacity("left", 1);
addColor();
}
//right
btnspan[1].onclick = function () {
setindexopacity("right", 1);
addColor();
}
}
</script>
</body>
</html>