jQuery
jquery动画
通过animate方法可以设置元素某属性值上的动画,可以设置一个或多个属性值,动画执行完成后会执行一个函数。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
$('#btn').click(function(){
$('.box').animate({
'width':600},1000,function(){
$('.box').animate({
'height':400},1000,function(){
$('.box').animate({
'opacity':0});
});
});
})
/*参数可以写成数字表达式:*/
$('#btn2').click(function(){
$('.box2').stop().animate({
'width':'+=100'});
})
})
</script>
<style type="text/css">
.box,.box2{
width:100px;
height:100px;
background-color:gold;
}
</style>
</head>
<body>
<input type="button" name="" value="动画" id="btn">
<div class="box"></div>
<br />
<br />
<input type="button" name="" value="动画" id="btn2">
<div class="box2"></div>
</body>
</html>
滑动选项卡案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.btns input{
width:100px;
height:40px;
background-color:#ddd;
border:0;
outline:none;
}
.btns .current{
background-color:gold;
}
.cons{
width:500px;
height:300px;
overflow:hidden;
position:relative;
}
.slides{
width:1500px;
height:300px;
position:absolute;
left:0;
top:0;
}
.cons .slides div{
width:500px;
height:300px;
background-color:gold;
text-align:center;
line-height:300px;
font-size:30px;
float:left;
}
.cons .active{
display: block;
}
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
var $btn = $('.btns input');
var $slides = $('.cons .slides');
$btn.click(function(){
// this 指的是原生的this,它表示当前点击的对象,使用jquery的对象需要用$(this)
// 当前点击的按钮加上current样式后,除了当前,其他的按钮去掉current样式
$(this).addClass('current').siblings().removeClass('current');
$slides.stop().animate({
'left':-500*$(this).index()});
})
})
</script>
</head>
<body>
<div class="btns">
<input type="button" name="" value="01" class="current">
<input type="button" name="" value="02">
<input type="button" name="" value="03">
</div>
<div class="cons">
<div class="slides">
<div>选项卡一的内容</div>
<div>选项卡二的内容</div>
<div>选项卡三的内容</div>
</div>
</div>
</body>
</html>
尺寸相关、滚动事件
1、获取和设置元素的尺寸
width()、height() 获取元素width和height
innerWidth()、innerHeight() 包括padding的width和height
outerWidth()、outerHeight() 包括padding和border的width和height
outerWidth(true)、outerHeight(true) 包括padding和border以及margin的width和height
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
var $div = $('.box');
// 盒子内容的尺寸
console.log($div.width());
console.log($div.height());
// 盒子内容加上padding的尺寸
console.log($div.innerWidth());
console.log($div.innerHeight());
//盒子的真实尺寸,内容尺寸+padding+border
console.log($div.outerWidth());
console.log($div.outerHeight());
// 盒子的真实尺寸再加上margin
console.log($div.outerWidth(true));
console.log($div.outerHeight(true));
})
</script>
<style type="text/css">
.box{
width:300px;
height:200px;
padding:20px;
border:10px solid #000;
margin:20px;
background-color:gold;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
2、获取元素相对页面的绝对位置
offset()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
var $div = $('.box');
$div.click(function(){
var oPos = $div.offset();
document.title = oPos.left + "|" + oPos.top;
})
//console.log(oPos);
})
</script>
</head>
<style type="text/css">
.box{
width:200px;
height:200px;
background-color:gold;
margin:50px auto 0;
}
</style>
<body>
<div class="box">
</div>
</body>
</html>
加入购物车案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.chart{
width:150px;
height:50px;
border:2px solid #000;
text-align:center;
line-height:50px;
float:right;
margin-right:100px;
margin-top:50px;
}
.chart em{
font-style: normal;
color:red;
font-weight:bold;
}
.add{
width:100px;
height:50px;
background-color:green;
border:0;
color:#fff;
float:left;
margin-top:300px;
margin-left:300px;
}
.point{
width:16px;
height:16px;
background-color:red;
position:fixed;
left:0;
top:0;
display:none;
z-index:9999;
border-radius:50%;
}
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
var $chart = $('.chart');
var $count = $('.chart em');
var $btn = $('.add');
var $point = $('.point');
var $w01 = $btn.outerWidth();
var $h01 = $btn.outerHeight();
var $w02 = $chart.outerWidth();
var $h02 = $chart.outerHeight();
$btn.click(function(){
var oPos01 = $btn.offset();
var oPos02 = $chart.offset();
$point.css({
'left':oPos01.left+parseInt($w01/2)-8,'top':oPos01.top+parseInt($h01/2)-8});
$point.show();
$point.stop().animate({
'left':oPos02.left+parseInt($w02/2)-8,'top':oPos02.top+parseInt($h02/2)-8},800,function(){
$point.hide();
var iNum = $count.html();
$count.html(parseInt(iNum)+1);
});
})
});
</script>
</head>
<body>
<div class="chart">购物车<em>0</em></div>
<input type="button" name="" value="加入购物车" class="add">
<div class="point"></div>
</body>
</html>
3、获取浏览器可视区宽度高度
$(window).width();
$(window).height();
4、获取页面文档的宽度高度
$(document).width();
$(document).height();
5、获取页面滚动距离
$(document).scrollTop();
$(document).scrollLeft();
6、页面滚动事件
$(window).scroll(function(){
......
})
菜单吸顶案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
body{
margin:0;
}
.banner{
width:960px;
height:200px;
background-color:cyan;
margin:0 auto;
}
.menu{
width:960px;
height:80px;
background-color:gold;
margin:0 auto;
text-align:center;
line-height:80px;
}
.menu_back{
width:960px;
height:80px;
margin:0 auto;
display:none;
}
p{
text-align:center;
color:red;
}
.totop{
width:60px;
height:60px;
background-color:#000;
color:#fff;
position:fixed;
right:20px;
bottom:50px;
line-height:60px;
text-align:center;
font-size:40px;
border-radius:50%;
cursor:pointer;
display:none;
}
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
$menu = $('.menu');
$menu_back = $('.menu_back');
$totop = $('.totop');
$(window).scroll(function(){
//console.log('abc');
var iNum = $(document).scrollTop();
//document.title = iNum;
if(iNum>200)
{
$menu.css({
'position':'fixed',
'left':'50%',
'top':0,
'marginLeft':-480
});
$menu_back.show();
}
else
{
$menu.css({
'position':'static',
'marginLeft':'auto'
});
$menu_back.hide();
}
if(iNum>400){
$totop.fadeIn();
}
else
{
$totop.fadeOut();
}
})
$totop.click(function(){
$('html,body').animate({
'scrollTop':0});
})
})
</script>
</head>
<body>
<div class="banner"></div>
<div class="menu">菜单</div>
<div class="menu_back"></div>
<div class="totop">↑</div>
<p>文档内容</p>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<p>文档内容</p>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<p>文档内容</p>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<p>文档内容</p>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
</body>
</html>
jquery属性操作
1、html() 取出或设置html内容
// 取出html内容
var $htm = $('#div1').html();
// 设置html内容
$('#div1').html('<span>添加文字</span>');
2、prop() 取出或设置某个属性的值
// 取出图片的地址
var $src = $('#img1').prop('src');
// 设置图片的地址和alt属性
$('#img1').prop({src: "test.jpg", alt: "Test Image" });
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
var $a = $('.link');
var $img = $('#img01');
var $div = $('#div1');
// 读取class属性值
console.log( $a.prop('class') );
// 没有设置的属性读取为空
console.log( $a.prop('title') );
// 获取是图片的绝对地址
console.log($img.prop('src'));
//alert($img.prop('src'));
// 设置属性
$a.prop({
'href':'http://www.baidu.com','title':'百度网链接'});
//console.log( $a.prop('title') );
//读取标签内包含的内容
console.log( $a.html() );
$div.html('<span>div里面的span元素</span>');
})
</script>
</head>
<body>
<a href="#" class="link">这是一个链接</a>
<img src="images/002.jpg" id="img01" alt="水果">
<div id="div1"></div>
</body>
</html>
jquery循环
对jquery选择的对象集合分别进行操作,需要用到jquery循环操作,此时可以用对象上的each方法:
$(function(){
$('.list li').each(function(i){
$(this).html(i);
})
})
......
<ul class="list">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
var $li = $('.list li');
//$li.css({'backgroundColor':'gold'});
$li.each(function(a){
//alert(a);
//alert( $(this).html() );
//alert($(this).index());
if($(this).index()%2==0)
{
$(this).css({
'backgroundColor':'gold'});
}
})
})
</script>
</head>
<body>
<ul class="list">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
</ul>
</body>
</html>
手风琴格局案例
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<style>
*{
margin:0;padding:0;}
body{
font-size:12px;}
#accordion{
width:727px; height:350px; margin:100px auto 0 auto; position:relative; overflow:hidden; border:1px solid #CCC;}
#accordion ul{
list-style:none;}
#accordion ul li{
width:643px;height:350px; position:absolute; background:#FFF;}
#accordion ul li span{
display:block;width:20px; height:350px; float:left; text-align:center; color:#FFF; padding-top:5px; cursor:pointer;}
#accordion ul li img{
display:block; float:right;}
.bar01{
left:0px;}
.bar02{
left:643px;}
.bar03{
left:664px;}
.bar04{
left:685px;}
.bar05{
left:706px;}
.bar01 span{
background:#09E0B5;}
.bar02 span{
background:#3D7FBB;}
.bar03 span{
background:#5CA716;}
.bar04 span{
background:#F28B24;}
.bar05 span{
background:#7C0070;}
</style>
<script type="text/javascript">
$(function(){
var $li = $('#accordion li');
$li.click(function(){
//alert($(this).html());
$(this).animate({
'left':21*$(this).index()});
//点击的li前面的li向左运动到各自的位置
$(this).prevAll().each(function(){
//这里的$(this)指的是循环选择的每个li
$(this).animate({
'left':21*$(this).index()});
})
// 第5个li在右边的left值 727-21*1 等同于 727-21*(5-$(this).index())
// 第4个li在右边的left值 727-21*2 等同于 727-21*(5-$(this).index())
// 第3个li在右边的left值 727-21*3 等同于 727-21*(5-$(this).index())
$(this).nextAll().each(function(){
$(this).animate({
'left':727-21*(5-$(this).index())});
})
})
})
</script>
<title>手风琴效果</title>
</head>
<body>
<div id="accordion">
<ul>
<li class="bar01"><span>非洲景色01</span><img src="images/001.jpg" /></li>
<li class="bar02"><span>非洲景色02</span><img src="images/002.jpg" /></li>
<li class="bar03"><span>非洲景色03</span><img src="images/003.jpg" /></li>
<li class="bar04"><span>非洲景色04</span><img src="images/004.jpg" /></li>
<li class="bar05"><span>非洲景色05</span><img src="images/005.jpg" /></li>
</ul>
</div>
</body>
</html>
jquery事件
事件函数列表:
blur() 元素失去焦点
focus() 元素获得焦点
click() 鼠标单击
mouseover() 鼠标进入(进入子元素也触发)
mouseout() 鼠标离开(离开子元素也触发)
mouseenter() 鼠标进入(进入子元素不触发)
mouseleave() 鼠标离开(离开子元素不触发)
hover() 同时为mouseenter和mouseleave事件指定处理函数
ready() DOM加载完成
resize() 浏览器窗口的大小发生改变
scroll() 滚动条的位置发生变化
submit() 用户递交表单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
// 在获得焦点的时候做什么事情
/*$('#input01').focus(function(){
alert('获得焦点')
})*/
//focus 一般用来让input元素开始就获取焦点,只能是一个元素获得焦点
$('#input01').focus();
$('#input01').blur(function(){
// 获取input元素的value值用 val()
var sVal = $(this).val();
alert(sVal);
})
$('#form1').submit(function(){
//alert('提交');
// 阻止默认的提交行为
return false;
})
})
</script>
</head>
<body>
<form id="form1" action="http://www.baidu.com">
<input type="text" name="dat01" id="input01">
<input type="text" name="dat02" id="input02">
<input type="submit" name="" value="提交" id="sub">
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
// 鼠标移入,移入的子元素也会触发
$('.con').mouseover(function(){
alert('移入');
})
$('.con').mouseout(function(){
alert('移出');
})
// 鼠标移入,移入的子元素不会触发
/*
$('.con2').mouseenter(function(){
alert('移入');
})
$('.con2').mouseleave(function(){
alert('移出');
})
合并成下面的写法:
*/
$('.con2').hover(function(){
alert('移入')
},function(){
alert('移出')
})
})
</script>
<style type="text/css">
.con,.con2{
width:200px;
height:200px;
background-color:gold;
}
.box,.box2{
width:100px;
height:100px;
background-color:green;
}
</style>
</head>
<body>
<div class="con">
<div class="box"></div>
</div>
<br />
<br />
<br />
<br />
<div class="con2">
<div class="box2"></div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(window).resize(function(){
var $w = $(window).width();
document.title = $w;
});
</script>
</head>
<body>
</body>
</html>
绑定事件的其他方式
$(function(){
$('#div1').bind('mouseover click', function(event) {
alert($(this).html());
});
});
取消绑定事件
$(function(){
$('#div1').bind('mouseover click', function(event) {
alert($(this).html());
// $(this).unbind();
$(this).unbind('mouseover');
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
/*
$('#btn').click(function(){
alert('click事件')
})
*/
// 点击或者鼠标移入的时候都执行绑定的函数
$('#btn').bind('click mouseover',function(){
alert('触发事件绑定的函数');
$(this).unbind('mouseover');
})
})
</script>
</head>
<body>
<input type="button" name="" value="按钮" id="btn">
</body>
</html>
事件冒泡
什么是事件冒泡
在一个对象上触发某类事件(比如单击onclick事件),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事件返回true,那么这个事件会向这个对象的父级对象传播,从里到外,直至它被处理(父级对象所有同类事件都将被激活),或者它到达了对象层次的最顶层,即document对象(有些浏览器是window)。
事件冒泡的作用
事件冒泡允许多个操作被集中处理(把事件处理器添加到一个父级元素上,避免把事件处理器添加到多个子级元素上),它还可以让你在对象层的不同级别捕获事件。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
// event 是发生事件的时候的事件对象,使用的时候,通过第一个参数传进来
$('.son').click(function(event){
alert(1);
//通过event对象上的stopPropagation的方法阻止事件冒泡
//event.stopPropagation();
})
$('.father').click(function(event){
alert(2);
event.stopPropagation();
})
$('.grandfather').click(function(){
alert(3);
// 阻止事件冒泡和阻止默认行为的统一写法:
return false;
})
$(document).click(function(){
alert(4);
})
})
</script>
<style type="text/css">
.grandfather{
width:300px;
height:300px;
background-color:green;
position:relative;
}
.father{
width:200px;
height:200px;
background-color:gold;
}
.son{
width:100px;
height:100px;
background-color: red;
position:absolute;
left:0;
top:400px;
}
</style>
</head>
<body>
<div class="grandfather">
<div class="father">
<div class="son"></div>
</div>
</div>
</body>
</html>
阻止事件冒泡
事件冒泡机制有时候是不需要的,需要阻止掉,通过 event.stopPropagation() 来阻止
阻止默认行为
阻止表单提交
$('#form1').submit(function(event){
event.preventDefault();
})
合并阻止操作
实际开发中,一般把阻止冒泡和阻止默认行为合并起来写,合并写法可以用
// event.stopPropagation();
// event.preventDefault();
// 合并写法:
return false;
阻止事件冒泡完成弹窗案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
$('#btn').click(function(){
$('.pop_con').fadeIn();
return false;
})
$(document).click(function(){
$('.pop_con').fadeOut();
})
$('.pop').click(function(){
return false;
})
$('#close').click(function(){
$('.pop_con').fadeOut();
})
})
</script>
<style type="text/css">
.pop_con{
display:none;
}
.pop{
position:fixed;
width:500px;
height:300px;
background-color:#fff;
border:3px solid #000;
left:50%;
top:50%;
margin-left:-250px;
margin-top:-150px;
z-index:9999;
}
.mask{
position:fixed;
width:100%;
height:100%;
background-color:#000;
opacity:0.3;
filter:alpha(opacity=30);
left:0;
top:0;
z-index:9990;
}
.close{
float:right;
font-size:30px;
}
</style>
</head>
<body>
<input type="button" name="" value="弹出" id="btn">
<div class="pop_con">
<div class="pop">
弹框里面文字
投资金额:
<input type="text" name="">
<a href="#" id="close" class="close">×</a>
</div>
<div class="mask"></div>
</div>
</body>
</html>
事件委托
事件委托就是利用冒泡的原理,把事件加到父级上,通过判断事件来源的子集,执行相应的操作,事件委托首先可以极大减少事件绑定次数,提高性能;其次可以让新加入的子元素也可以拥有相同的操作。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.list{
background-color:gold;
list-style:none;
padding:10px;
}
.list li{
height:30px;
background-color:green;
margin:10px;
}
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
/* 一般绑定事件的写法
$('.list li').click(function(){
$(this).css({'backgroundColor':'red'});
});
*/
// 新建一个li元素赋值给$li变量
//var $li = $('<li>9</li>');
//让新加的li有相同的事件,需要单独绑定
//$li.click(....)
// 把新建的li元素放到ul子集的最后面
//$('.list').append($li);
//事件委托,将li要发生的事件委托给li的父级
$('.list').delegate('li','click',function(){
//$(this) 指的是委托的子元素
$(this).css({
'backgroundColor':'red'});
})
var $li = $('<li>9</li>');
$('.list').append($li);
})
</script>
</head>
<body>
<ul class="list">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
</ul>
</body>
</html>