ath.floor() 返回小于或等于一个给定数字的最大整数。
可以理解 Math.floor()为向下取整。
与其相对的是 Math.ceil() ,这个是向上取整。
如下面的代码:
Math.floor( 45.95);
// 45
Math.floor( 45.05);
// 45
Math.floor( 4 );
// 4
Math.floor(-45.05);
// -46
Math.floor(-45.95);
// -46
上图演示了这个函数的一些小对比。
CSS 是什么?
CSS是Cascading Style Sheets的简称,中文称为层叠样式表。
属性和属性值用冒号隔开,以分号结尾。
CSS 四种引入方式:
1.行内式
行内式是在标签的style属性中设定CSS样式。
<div style="..."></div>
2.嵌入式
嵌入式是将CSS样式集中写在网页的<head>标签的<style></style>标签对中。
<head>
...
<style type="text/css">
...此处写CSS样式
</style>
</head>
3.导入式
将一个独立的.css文件引入HTML文件中,导入式使用@import 引入外部CSS文件,<style>标记也是写在<head>标记中。
导入式会在整个网页装载完后再装载CSS文件。
<head>
...
<style type="text/css">
@import "My.css"; 此处注意.css文件的路径
</style>
</head>
4.链接式
将一个独立的.css文件引入到HTML文件中,使用<link>标记写在<head>标记中。
链接式会以网页文件主体装载前装载CSS文件。
<head>
...
<link href="My.css" rel="stylesheet" type="text/css">
</head>
样式应用顺序:
.nick {
color: yellow !important;
}
基本选择器:
1.通用元素选择器
* 表示应用到所有的标签。
* {color: yellow}
2.标签选择器
匹配所有使用 div 标签的元素(可以匹配所有标签)
div {color: yellow}
3.类选择器
匹配所有class属性中包含info的元素。
语法:.类名{样式}(类名不能以数字开头,类名要区分大小写。)
.Mycolor {color: yellow}
<h3 class="Mycolor">nick</h3>
4.ID选择器
使用id属性来调用样式,在一个网页中id的值都是唯一的(是W3C规范而不是规则,所以不会报错)。
语法:#ID名{样式}(ID名不能以数字开头)
#Mycolor {color: yellow}
<h3 id="Mycolor">Nick.</h3>
组合选择器:
1.多元素选择器
同时匹配h3,h4标签,之间用逗号分隔。
h3,h4 {color: yellow;}
<h3>Nick</h3>
<h4>Jenny</h4>
2.后代元素选择器
匹配所有div标签里嵌套的P标签,之间用空格分隔。
div p {color: yellow;}
<div>
<p>Nick</p>
<div>
<p>Nick</p>
</div>
</div>
3.子元素选择器
匹配所有div标签里嵌套的子P标签,之间用>分隔。
div > p {color: yellow;}
<div>
<p>Nick</p>
<p>Nick</p>
</div>
4.毗邻元素选择器
匹配所有紧随div标签之后的同级标签P,之间用+分隔(只能匹配一个)。
div + p {color: yellow;}
<div>Nick</div>
<p>Nick</p>
属性选择器:
1.[title] & P[title]
设置所有具有title属性的标签元素;
设置所有具有title属性的P标签元素。
[title]
{
color: yellow;
}
p[title]
{
color: yellow;
}
<div title>Nick</div>
<p title>Nick</p>
2.[title=Nick]
设置所有title属性等于“Nick”的标签元素。
[title="Nick"]
{
color: yellow;
}
<p title="Nick">Nick</p>
3.[title~=Nick]
设置所有title属性具有多个空格分隔的值、其中一个值等于“Nick”的标签元素。
[title~="Nick"]
{
color: yellow;
}
<p title="Nick Jenny">Nick</p>
<p title="Jenny Nick">Nick</p>
4.[title|=Nick]
设置所有title属性具有多个连字号分隔(hyphen-separated)的值、其中一个值以"Nick"开头的标签元素。
例:lang属性:"en"、"en-us"、"en-gb"等等
[title|="Nick"]
{
color: yellow;
}
<p title="Nick-Jenny">Nick</p>
5.[title^=Nick]
设置属性值以指定值开头的每个标签元素。
[title^="Nick"]
{
color: yellow;
}
<p title="NickJenny">Nick</p>
6.[title$=Nick]
设置属性值以指定值结尾的每个标签元素。
[title$="Nick"]
{
color: yellow;
}
<p title="JennyNick">Nick</p>
7.[title*=Nick]
设置属性值中包含指定值的每个元素
[title*="Nick"]
{
color: yellow;
}
<p title="SNickJenny">Nick</p>
伪类选择器:
1. link、hover、active、visited
a:link{color: black}
a:hover{color: yellow}
a:active{color: blue}
a:visited{color: red}
<a href="#">Nick</a>
2. before、after
p {
color: yellow;
}
p:before{
content: "before...";
}
p:after{
content: "after...";
}
<p> Nick </p>
1. 颜色属性:
color
transparent
opacity
2. 字体属性:
font-style: 用于规定斜体文本
font-weight: 设置文本的粗细
font-size: 设置字体的大小
font-family:字体名称
font:简写属性
3. 文本属性:
white-space: 设置元素中空白的处理方式
direction: 规定文本的方向
text-align: 文本的水平对齐方式
line-height: 文本行高
vertical-align: 文本所在行高的垂直对齐方式
text-indent: 文本缩进
letter-spacing: 添加字母之间的空白
word-spacing: 添加每个单词之间的空白
text-transform: 属性控制文本的大小写
text-overflow: 文本溢出样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--<link href="cc2.css" rel="stylesheet" type="text/css">-->
<style>
div {
width: 100px;
height: 100px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
</head>
<body>
<div>索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁</div>
</body>
</html>
text-decoration: 文本的装饰
text-shadow:文本阴影
word-wrap:自动换行
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
p {
width: 150px;
height: 160px;
background-color: #FFA500;
/*边框阴影*/
box-shadow: 10px 10px 5px #888;
/*自动换行*/
word-wrap: break-word;
}
h1 {
text-shadow: 5px 5px 5px #888;
}
</style>
</head>
<body>
<p>
When you are old and grey and full of sleep,And nodding by the fire, take down this book,And slowly read, and dream of the soft look
</p>
<h1>索宁</h1>
</body>
</html>
a {
text-decoration: none;
/*去除a标签下划线*/
}
4. 背景属性
background-color: 背景颜色
background-image 设置图像为背景
background-position 设置背景图像的位置坐标
background-repeat 设置背景图像不重复平铺
background-attachment 背景图像是否固定或者随着页面的其余部分滚动
background 简写
5. 列表属性
list-style-type: 列表项标志的类型
list-style-image:将图象设置为列表项标志
list-style-position:列表项标志的位置
list-style:缩写
1. 边框
border-style:边框样式
border-color:边框颜色
border-width:边框宽度
border-radius:圆角
border: 简写
box-shadow:边框阴影
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
border:2px solid;
border-radius:25px;
width: 140px;
}
</style>
</head>
<body>
<div>
点赞哦!dear.
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.radius1 {
display: inline-block;
width: 100px;
height: 100px;
background-color: yellow;
border-radius: 20px;
}
.radius2 {
display: inline-block;
width: 100px;
height: 100px;
background-color: red;
border-radius: 20px 35px;
}
.radius3 {
display: inline-block;
width: 100px;
height: 100px;
background-color: blue;
border-radius: 20px 35px 50px;
}
.radius4 {
display: inline-block;
width: 100px;
height: 100px;
background-color: green;
border-radius: 20px 35px 50px 60px;
}
</style>
</head>
<body>
<div>
<span class="radius1"></span>
<span class="radius2"></span>
<span class="radius3"></span>
<span class="radius4"></span>
</div>
</body>
</html>
边框实现各种三角符号:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.triangle-one {
display: inline-block;
border-top: 50px red solid;
border-right: 50px green solid;
border-bottom: 50px yellow solid;
border-left: 50px blue solid;
}
.triangle-two {
display: inline-block;
border-top: 0 red solid;
border-right: 50px green solid;
border-bottom: 50px yellow solid;
border-left: 50px blue solid;
}
.triangle-stree {
display: inline-block;
border-top: 50px red solid;
border-right: 0 green solid;
border-bottom: 50px yellow solid;
border-left: 50px blue solid;
}
.triangle-four {
display: inline-block;
border-top: 50px red solid;
border-right: 0 green solid;
border-bottom: 0 yellow solid;
border-left: 50px blue solid;
}
.triangle-five {
display: inline-block;
border: 50px transparent solid;
border-top: 50px red solid;
}
.triangle-six {
display: inline-block;
border: 50px transparent solid;
border-bottom: 50px yellow solid;
}
.triangle-seven {
display: inline-block;
border: 50px transparent solid;
border-top: 60px red solid;
border-right: 0;
}
.triangle-eight {
display: inline-block;
border: 50px transparent solid;
border-left: 30px yellow solid;
border-bottom: 0;
}
</style>
</head>
<body>
<div class="triangle-one"></div>
<div class="triangle-two"></div>
<div class="triangle-stree"></div>
<div class="triangle-four"></div>
<div class="triangle-five"></div>
<div class="triangle-six"></div>
<div class="triangle-seven"></div>
<div class="triangle-eight"></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.back {
width: 1000px;
height: 1000px;
margin: 0 auto;
background-color: #ddd;
position: relative;
}
.back-in {
position: absolute;
width: 1020px;
height: 45px;
left: -20px;
top: 50px;
background-color: #2F4F4F;
}
.back-img {
border: 20px solid transparent;
border-top: 10px solid dimgrey;
border-right: 0;
display: inline-block;
position: absolute;
top: 95px;
left: -20px;
}
.back-font {
line-height: 9px;
margin-left: 30px;
color: white;
}
</style>
</head>
<body>
<div class="back">
<div class="back-in"><h3 class="back-font">妹子求关注 ^.^</h3></div>
<div class="back-img"></div>
</div>
</body>
</html>
2.★ 盒子模型
一个标准的盒子模型:
padding:用于控制内容与边框之间的距离;
margin: 用于控制元素与元素之间的距离;
一个参数,应用于四边。
两个参数,第一个用于上、下,第二个用于左、右。
三个参数,第一个用于上,第二个用于左、右,第三个用于下。
边框在默认情况下会定位于浏览器窗口的左上角,但是并没有紧贴着浏览器的窗口的边框,这是因为body本身也是一个盒子,外层还有html,
在默认情况下,body距离html会有若干像素的margin,所以body中的盒子不会紧贴浏览器窗口的边框了。
解决方法:
body {
margin: 0;
}
3.★ display
4. visibility
5.★ float 浮动
让一行显示两个块级标签,会脱离文档流
clear 清除浮动:
6. clip 剪裁图像
rect 剪裁定位元素:
7. overflow 设置当对象的内容超过其指定高度及宽度时如何显示内容
8.★ position 规定元素的定位类型
9. z-index 元素层叠顺序
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.z-index1 {
width: 100px;
height: 100px;
background-color: yellow;
position: absolute;
z-index: -1;
}
.z-index2 {
width: 100px;
height: 100px;
background-color: red;
position: absolute;
top: 20px;
left: 20px;
z-index: 5;
}
</style>
</head>
<body>
<div class="z-index1"></div>
<div class="z-index2"></div>
</body>
</html>
10. outline 边框轮廓
11. zoom 缩放比例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.zoom1 {
zoom: 100%;
}
.zoom2 {
zoom: 150%;
}
.zoom3 {
zoom: 200%;
}
</style>
</head>
<body>
<div class="zoom1">Nick 100%</div>
<div class="zoom2">Nick 200%</div>
<div class="zoom3">Nick 300%</div>
</body>
</html>
12. cursor 鼠标的类型形状
鼠标放在以下单词上,There will be a miracle:
url: 自定义光标
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--<link href="cc2.css" rel="stylesheet" type="text/css">-->
<style>
body {
cursor: url("mouse.png"), auto;
/*图片地址:http://images.cnblogs.com/cnblogs_com/suoning/845162/o_mouse.png*/
}
</style>
</head>
<body>
<div><img src="http://images.cnblogs.com/cnblogs_com/suoning/845162/o_ns.png" height="100%" width="100%"></div>
</body>
</html>
Auto: 默认
Default: 默认
e-resize
ne-resize
nw-resize
n-resize
se-resize
sw-resize
s-resize
w-resize
Crosshair
Pointer
Move
text
wait
help
not-allowed
13. transform、transition 动画效果
transform 转换,变形
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>nick</title>
<meta charset="utf-8" />
<style type="text/css">
div {
border: 1px solid black;
height: 30px;
width: 30px;
background-color: yellow;
/*transform-origin: 50px 50px;*/
transform-origin: left;
transform: rotate(50deg);
/*transform: skew(50deg,50deg);*/
/*transform: translate(50px,50px);*/
/*transform: scale(2);*/
}
</style>
</head>
<body>
<div></div>
</body>
</html>
Transition 平滑过渡
#property 指定属性对应类型
1、color: 通过红、绿、蓝和透明度组件变换(每个数值单独处理),如:background-color,border-color,color,outline-color等CSS属性;
2、length:真实的数字,如:word-spacing,width,vertical- align,top,right,bottom,left,padding,outline-width,margin,min-width,min- height,max-width,max-height,line-height,height,border-width,border- spacing,background-position等属性;
3、percentage:真实的数字,如:word-spacing,width,vertical- align,top,right,bottom,left,min-width,min- height,max-width,max-height,line-height,height,background-position等属性;
4、integer 离散步骤(整个数字),在真实的数字空间,以及使用floor()转换为整数时发生,如:outline-offset,z-index等属性;
5、number真实的(浮点型)数值,如:zoom,opacity,font-weight等属性;
6、transform list。
7、rectangle:通过x、 y、 width和height(转为数值)变换,如:crop;
8、visibility:离散步骤,在0到1数字范围之内,0表示“隐藏”,1表示完全“显示”,如:visibility;
9、shadow:作用于color、x、y、和blur(模糊)属性,如:text-shadow;
10、gradient:通过每次停止时的位置和颜色进行变化。它们必须有相同的类型(放射状的或是线性的)和相同的停止数值以便执行动画,如:background-image;
11、paint server (SVG):只支持下面的情况:从gradient到gradient以及color到color,然后工作与上面类似;
12、space-separated list of above:如果列表有相同的项目数值,则列表每一项按照上面的规则进行变化,否则无变化;
13、a shorthand property:如果缩写的所有部分都可以实现动画,则会像所有单个属性变化一样变化。
#支持执行transition效果的属性
Property Name Type
background-color as color
background-position as repeatable list of simple list of length, percentage, or calc
border-bottom-color as color
border-bottom-width as length
border-left-color as color
border-left-width as length
border-right-color as color
border-right-width as length
border-spacing as simple list of length
border-top-color as color
border-top-width as length
bottom as length, percentage, or calc
clip as rectangle
color as color
font-size as length
font-weight as font weight
height as length, percentage, or calc
left as length, percentage, or calc
letter-spacing as length
line-height as either number or length
margin-bottom as length
margin-left as length
margin-right as length
margin-top as length
max-height as length, percentage, or calc
max-width as length, percentage, or calc
min-height as length, percentage, or calc
min-width as length, percentage, or calc
opacity as number
outline-color as color
outline-width as length
padding-bottom as length
padding-left as length
padding-right as length
padding-top as length
right as length, percentage, or calc
text-indent as length, percentage, or calc
text-shadow as shadow list
top as length, percentage, or calc
vertical-align as length
visibility as visibility
width as length, percentage, or calc
word-spacing as length
z-index as integer
鼠标放在以下图片上,There will be a miracle:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>nick</title>
<meta charset="utf-8" />
<style type="text/css">
.img-see-2016-7-2 {
background-image: url("http://images.cnblogs.com/cnblogs_com/suoning/845162/o_sea.jpg");
background-size: 660px;
background-repeat: no-repeat;
height: 300px;
width: 600px;
transition-duration: 30s;
transition-timing-function: ease;
transition-property: background-size;
}
.img-see-2016-7-2:hover {
background-size: 2000px;
}
</style>
</head>
<body>
<div class="img-see-2016-7-2"></div>
</body>
</html>
作者:suoning
原文链接:https://www.cnblogs.com/suoning/p/5625582.html
前面两篇文章,学习了JavaScript中DOM、BOM相关基础知识后,今天我们将结合前面所学内容,利用DOM操作实现常见的网页动画效果;结合知识点有相关案例效果演示,另外本文所设计的源码和配套素材可在文末获取;
获得元素距离带有定位父元素的位置
获得元素自身的width和height
思路:盒子内的坐标可以通过鼠标事件的e.pageX获取到页面坐标再减去element.offsetLeft来得到;
var box=document.querySelector('.box');
box.addEventListener('click',function(e){
var x=e.pageX - this.offsetLeft;
var y=e.pageY - this.offsetTop;
console.log('x:'+x+',y:'+y);
})
需求:
(1)点击弹出层,会淡出模块框,并且显示灰色半透明的遮挡层;
(2)点击关闭按钮,可以关闭模态框,并且同时关闭灰色半透明遮挡层;
(3)鼠标放到模态框最上面一行,可以按住鼠标拖拽模态框在页面中移动;
(4)鼠标松开,可以停止拖动模态框移动;
分析:
(1)拖拽事件,鼠标是按着的状态,故mousemove和mouseup须卸载mousedown事件里面;
(2)在拖拽的过程中,鼠标的位置相对于组件的坐标是不变的,因此为了改变组件的位置,需要将页面坐标减去相对组件坐标来获取;
var link=document.querySelector('#link');
var login_window=document.querySelector('#login');
var close_button=document.querySelector('#closeBtn');
var bg=document.querySelector('#bg');
link.addEventListener('click',function(){
login_window.style.display='block';
bg.style.display='block';
})
close_button.addEventListener('click',function(){
login_window.style.display='none';
bg.style.display='none';
})
// 拖动事件三个过程:mousedown、mousemove、mouseup
var title=document.querySelector('#title');
title.addEventListener('mousedown',function(e){
var x=e.pageX - login_window.offsetLeft;
var y=e.pageY - login_window.offsetTop;
document.addEventListener('mousemove',move);
function move(e){
login_window.style.left=e.pageX - x + 'px';
login_window.style.top=e.pageY -y + 'px';
}
document.addEventListener('mouseup',function (e) {
document.removeEventListener('mousemove',move);
});
});
分析:
(1)mask界面与big界面可以通过鼠标事件设置dispaly属性即可;mouseover显示mask,mouseleave隐藏mask、mousemove移动mask
(2)mask的left与top的值可以通过鼠标在prew_img中的位置来获取;
(3)为了让mask界面中央跟着鼠标指针走,则需要减去mask尺寸一半的偏移量;
(4)防止mask页面跑出prew_img,需要对偏移量进行判断;
(5)大图片移动距离怎么计算?mask移动距离/mask最大移动距离=大图片移动距离/大图片最大移动距离;所以大图片移动距离=mask移动距离 * 大图片移动最大距离 / 遮挡层最大移动距离;
window.addEventListener('load',function(){
var prew_img=document.querySelector('.preview_img');
var mask=document.querySelector('.mask');
var big=document.querySelector('.big');
prew_img.addEventListener('mouseover',function(){
mask.style.display='block';
big.style.display='block';
})
prew_img.addEventListener('mousemove',function(e){
// 获取鼠标在prew_img元素内的坐标
var x=e.pageX - this.offsetLeft;
var y=e.pageY - this.offsetTop;
// 防止mask页面跑出prew_img盒子
var maskX=x - mask.offsetWidth / 2;
// 遮挡层的最大移动距离maskMax
var maskMax=this.offsetWidth - mask.offsetWidth;
if(maskX <=0){
maskX=0;
}else if(maskX >=maskMax){
maskX=maskMax;
}
var maskY=y - mask.offsetHeight / 2;
if(maskY <=0){
maskY=0;
}else if(maskY >=maskMax){
maskY=maskMax;
}
// 让mask界面中央跟着鼠标指针走
mask.style.left=maskX + 'px';
mask.style.top=maskY + 'px';
// 让big界面内容跟着mask位置移动,
// 大图片移动距离=mask移动距离 * 大图片移动最大距离 / mask最大移动距离
var big_img=document.querySelector('.bigImg');
var bigMax=big_img.offsetWidth - big.offsetWidth;
// 大图片移动距离
var bigMoveX=maskX * bigMax / maskMax;
var bigMoveY=maskY * bigMax / maskMax;
big_img.style.left=-bigMoveX + 'px';
big_img.style.top=-bigMoveY + 'px';
});
prew_img.addEventListener('mouseleave',function(){
mask.style.display='none';
big.style.display='none';
});
});
不需要调用,立马能自己执行的函数;
// 写法一:
(function(){})()
// 写法二:
(function(){}())
立即执行函数也可以传递参数:
(function(a,b){
console.log(a+b);
})(1,2)
立即执行函数的作用是独立创建了一个作用域,里面所有的变量都是局部变量,不会有命名冲突的情况;
使用scroll系列相关属性可以动态得到元素的大小、滚动距离;
scroll系列属性 作用 备注 element.scrollTop 返回被卷去的上侧距离 返回的数值不带单位 element.scrollLeft 返回被卷去的左侧距离 返回的数值不带单位 element.scrollWidth 返回自身的实际宽度,不含边框 返回的数值不带单位 element.scrollHeight 返回自身的实际高度,不含边框 返回的数值不带单位
页面被卷去的头部可以通过:window.pageYOffset来获得
// 1.获取元素
var sliderBar=document.querySelector('.slider-bar');
var topBar=document.querySelector('.header');
var banner=document.querySelector('.banner');
var main=document.querySelector('.main');
var goBack=document.querySelector('.goBack');
var barHeight=banner.offsetTop;
var sliderBarTop=sliderBar.offsetTop;
var fixTop=sliderBarTop - barHeight;
var mainTop=main.offsetTop;
// 2.页面滚动事件
document.addEventListener('scroll',function(){
if(window.pageYOffset >=barHeight){
sliderBar.style.position='fixed';
sliderBar.style.top=fixTop + 'px';
}else{
sliderBar.style.position='absolute';
sliderBar.style.top=sliderBarTop + 'px';
}
if(window.pageYOffset >=mainTop){
goBack.style.display='block';
}else{
goBack.style.display='none';
}
})
当鼠标移动到元素上时就会触发mouseenter事件,类似mouseenter
两者的区别是:mouseover鼠标经过自身盒子会触发、经过子盒子也会触发;而mouseenter只有经过自身盒子才会触发;
原因是mouseenter不会冒泡;跟mouseenter搭配的通常是mouseleave
通过定时器setInterval()不断移动盒子位置;
实现步骤:
//利用setInterval设置一个box元素从左向右移动400像素的动画
var box=document.querySelector('.box');
var timer=setInterval(function(){
if(box.offsetLeft > 200){
// 停止动画,本质上是清除定时器
clearInterval(timer);
}
box.style.left=box.offsetLeft + 5 + 'px';
},50);
将动画函数封装起来,利用JS是动态语言的特性,通过设置对象的方式,给每个不同的元素指定不同的定时器;
// 简单动画函数的封装,目标对象obj,目标位置target
function animate(obj,target){
// 如果设置一个按钮启动动画,会存在一个bug,只要不断点击按钮,动画会越走越快;
// 原因是启动了太多定时器,解决方案是,让元素只有一个定时器执行
clearInterval(obj.timer);
obj.timer=setInterval(function(){
if(obj.offsetLeft > target){
// 停止动画,本质上是清除定时器
clearInterval(obj.timer);
}
obj.style.left=obj.offsetLeft + 5 + 'px';
},50);
}
//利用setInterval设置动画
var btn=document.querySelector('button');
var box1=document.querySelector('.box1');
btn.addEventListener('click',function(){
animate(box1,400);
});
缓动动画就是让元素运动速度有所变换,最常见的就是让速度慢慢停下来;
思路:让盒子每次移动的距离慢慢变小;
核心算法:每次移动的步长=(目标值-现在的位置)/10这里的10是份数可以是9也可以是8;
停止的条件是:当前盒子的位置等于目标位置就停止定时器;
// 缓动动画
function animate(obj,target){
// 清除定时器
clearInterval(obj.timer);
obj.timer=setInterval(function() {
// 每次运行定时器都要重新计算步长值,所以步长值写在定时器里面
var step=(target - obj.offsetLeft)/10;
// 如果step是正值则往大取整,如果step是负值则往小取整
step=step >=0 ? Math.ceil(step) : Math.floor(step);
if(obj.offsetLeft==target){
clearInterval(obj.timer);
}
// 缓动动画核心算法:前进步长=(目标-当前)/ 10
obj.style.left=obj.offsetLeft + step + 'px';
},50);
}
回调函数原理:函数可以作为一个参数,将这个函数作为参数传入到另一个函数里面,当这个函数执行完成后,再执行传进去的这个函数,这个过程叫做回调。
回调函数位置:写到定时器结束的位置
// 缓动动画
function animate(obj,target,callback){
// 清除定时器
clearInterval(obj.timer);
obj.timer=setInterval(function() {
// 每次运行定时器都要重新计算步长值,所以步长值写在定时器里面
var step=(target - obj.offsetLeft)/10;
// 如果step是正值则往大取整,如果step是负值则往小取整
step=step >=0 ? Math.ceil(step) : Math.floor(step);
if(obj.offsetLeft==target){
clearInterval(obj.timer);
if(callback){
// 调用函数
callback();
}
}
// 缓动动画核心算法:前进步长=(目标-当前)/ 10
obj.style.left=obj.offsetLeft + step + 'px';
},50);
}
在head里引入JS文件
<script src="animate.js"></script>
实际调用过程中可以直接调用JS中定义的函数
var sliderbar=document.querySelector('.sliderbar');
var con=document.querySelector('.con');
sliderbar.addEventListener('mouseenter',function(){
animate(con,-160,function () {
// 动画执行完毕,将左箭头转为右箭头
sliderbar.children[1].innerHTML='→';
});
})
sliderbar.addEventListener('mouseleave',function(){
animate(con,0,function () {
sliderbar.children[1].innerHTML='←';
});
})
功能需求:
window.addEventListener('load', function () {
//===0.定义变量,获取元素
var focus=document.querySelector('.focus');
var arrowL=document.querySelector('.arrow-l');
var arrowR=document.querySelector('.arrow-r');
//===1.鼠标经过轮播图模块,左右按钮显示,离开隐藏左右按钮;=====focus.addEventListener('mouseenter', function () {
arrowL.style.display='block';
arrowR.style.display='block';
clearInterval(timer);
timer=null;
});
focus.addEventListener('mouseleave', function () {
arrowL.style.display='none';
arrowR.style.display='none';
timer=window.setInterval(function () {
arrowR.click();
}, 3000);
});
//===2.根据轮播图的数量自动生成小圆圈
var ul=focus.querySelector('ul');
var ol=focus.querySelector('.circle');
var pictureWidth=focus.offsetWidth;
// 获取到轮播图图片的数量length
var length=ul.children.length;
// 显示当前图片的索引
var circle=0;
// 显示当前图片的索引
var num=0;
// 创建元素插入到ol中
for (var i=0; i < length; i++) {
var li=document.createElement('li');
// 给没一个小圆圈添加一个index属性,方便点击事件的ul移动长度的确定
li.setAttribute('index', i);
// 给每一个小圆圈添加点击事件
li.addEventListener('click', function () {
// 移除其他小圆圈的选中属性
for (var i=0; i < length; i++) {
ol.children[i].className='';
}
// 设置当前选中小圆圈的选中属性
this.className='current';
// 定义选中的小圆圈的索引
var index=this.getAttribute('index');
num=index;
circle=index;
animate(ul, -pictureWidth * index);
});
ol.appendChild(li);
}
ol.children[0].className='current';
//===3.鼠标点击右侧按钮一次,图片往左播放一张,以此内推,左侧按钮同理;==// 为了实现无缝滚动,需要将第一张图添加到最后一张图中
var lastLi=ul.children[0].cloneNode(true);
ul.appendChild(lastLi);
// 设置一个节流阀,防止按钮在短时间内点击过多
var flag=true;
arrowR.addEventListener('click', function () {
if (flag) {
flag=false;
if (num==ul.children.length - 1) {
ul.style.left=0;
num=0;
}
num++;
animate(ul, -pictureWidth * num, function () {
flag=true;
});
circle++;
if (circle==ol.children.length) {
circle=0;
}
activateCircle(circle);
}
});
arrowL.addEventListener('click', function () {
if (flag) {
flag=false;
if (num==0) {
num=ul.children.length - 1;
ul.style.left=-num * pictureWidth + 'px';
}
num--;
animate(ul, -num * pictureWidth, function () {
flag=true;
});
if (circle==0) {
circle=ol.children.length;
}
circle--;
activateCircle(circle);
}
});
// 排他法思想点亮小圆圈
function activateCircle(index) {
for (var i=0; i < ol.children.length; i++) {
ol.children[i].className='';
}
ol.children[index].className='current';
}
// 实现轮播图自动播放
var timer=window.setInterval(function () {
arrowR.click();
}, 3000);
});
显示效果:
轮播图自动播放的原理是:设置一个定时器使用click()函数模拟按钮点击事件;
轮播图实现无缝滚动的原理是:将第一个图动态加在最后一张图上,当切到最后一张图时,重新设置元素布局距离,再执行滑动动画。
防止同一个按钮在短时间内点击次数过多,可以设置一个节流阀,将启动节流阀的操作写在动画回调函数里,这样只有当动画执行完毕后才才可以重新恢复按钮的点击功能;
页面滚动动画可以使用函数:window.scroll(x,y)可以滚动至页面的指定坐标位置;
最后:
本文配套源码与素材获取:https://gitee.com/yushengtan/jscode
关于JavaScript在网页动画中的使用就介绍到这里,下一节我们将继续深入介绍JavaScript的异步调用相关知识的学习~
*请认真填写需求信息,我们会在24小时内与您取得联系。