一、CSS样式
addClass() 添加样式
removeClass() 删除样式
toggleClass() 有就删除,没有就添加样式。
offset() 获取和设置元素的坐标。
练习:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
div{
width:100px;
height:260px;
}
div.border{
border: 2px white solid;
}
div.redDiv{
background-color: red;
}
div.blackDiv{
border: 5px blue solid;
}
</style>
<script type="text/javascript" src="script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
var $divEle = $('div:first');
$('#btn01').click(function(){
$divEle.addClass("blackDiv redDiv");
});
$('#btn02').click(function(){
$divEle.removeClass("blackDiv");
});
$('#btn03').click(function(){
$divEle.toggleClass("blackDiv");
});
$("#btn04").click(function () {
$divEle.offset({
top:10,
left:100
});
});
});
</script>
</head>
<body>
<table align="center">
<tr>
<td>
<div class="border">
</div>
</td>
<td>
<div class="btn">
<input type="button" value="addClass()" id="btn01"/>
<input type="button" value="removeClass()" id="btn02"/>
<input type="button" value="toggleClass()" id="btn03"/>
<input type="button" value="offset()" id="btn04"/>
</div>
</td>
</tr>
</table>
<br /> <br />
<br /> <br />
</body>
</html>
二、jQuery动画
基本动画
show() 将隐藏的元素显示
hide() 将可见的元素隐藏。
toggle() 可见就隐藏,不可见就显示。
以上动画方法都可以添加参数。
1、第一个参数是动画执行的时长(以毫秒为单位)
2、第二个参数是动画的回调函数(动画完成后自动调用的函数)
淡入淡出动画
fadeIn() 淡入(慢慢可见)
fadeOut() 淡出(慢慢消失)
fadeTo() 在指定时长内慢慢的将透明度修改到指定的值。0 透明,1 完成可见 0.5 半透明
fadeToggle() 淡入/淡出切换
练习:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Untitled Document</title>
<link href="css/style.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
$("#btn1").click(function(){
$("#div1").show(1000);
});
$("#btn2").click(function(){
$("#div1").hide(1000);
});
$("#btn3").click(function(){
$("#div1").toggle(1000);
});
$("#btn4").click(function(){
$("#div1").fadeIn(1000);
});
$("#btn5").click(function(){
$("#div1").fadeOut(1000);
});
$("#btn6").click(function(){
$("#div1").fadeTo(1000,Math.random());
});
$("#btn7").click(function(){
$("#div1").fadeToggle(1000);
});
});
</script>
</head>
<body>
<table style="float: left;">
<tr>
<td><button id="btn1">显示show()</button></td>
</tr>
<tr>
<td><button id="btn2">隐藏hide()</button></td>
</tr>
<tr>
<td><button id="btn3">显示/隐藏切换 toggle()</button></td>
</tr>
<tr>
<td><button id="btn4">淡入fadeIn()</button></td>
</tr>
<tr>
<td><button id="btn5">淡出fadeOut()</button></td>
</tr>
<tr>
<td><button id="btn6">淡化到fadeTo()</button></td>
</tr>
<tr>
<td><button id="btn7">淡化切换fadeToggle()</button></td>
</tr>
</table>
<div id="div1" style="float:left;border: 1px solid;background-color: blue;width: 300px;height: 200px;">
jquery动画定义了很多种动画效果,可以很方便的使用这些动画效果
</div>
</body>
</html>
三、 练习:CSS_动画品牌展示
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>品牌展示练习</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
body {
font-size: 12px;
text-align: center;
}
a {
color: #04D;
text-decoration: none;
}
a:hover {
color: #F50;
text-decoration: underline;
}
.SubCategoryBox {
width: 600px;
margin: 0 auto;
text-align: center;
margin-top: 40px;
}
.SubCategoryBox ul {
list-style: none;
}
.SubCategoryBox ul li {
display: block;
float: left;
width: 200px;
line-height: 20px;
}
.showmore , .showless{
clear: both;
text-align: center;
padding-top: 10px;
}
.showmore a , .showless a{
display: block;
width: 120px;
margin: 0 auto;
line-height: 24px;
border: 1px solid #AAA;
}
.showmore a span {
padding-left: 15px;
background: url(img/down.gif) no-repeat 0 0;
}
.showless a span {
padding-left: 15px;
background: url(img/up.gif) no-repeat 0 0;
}
.promoted a {
color: #F50;
}
</style>
<script type="text/javascript" src="script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function() {
$("li:gt(5):not(:last)").hide();
$("div div a").click(function () {
$("li:gt(5):not(:last)").toggle();
if ($("li:gt(5):not(:last)").is(":hidden")) {
$("li:contains('索尼')").removeClass("promoted");
$("div div").removeClass();
$("div div").addClass("showmore");
$("span").text("显示全部品牌");
} else {
$("li:contains('索尼')").addClass("promoted");
$("div div").removeClass();
$("div div").addClass("showless");
$("span").text("显示精简品牌");
}
return false;
});
});
</script>
</head>
<body>
<div class="SubCategoryBox">
<ul>
<li><a href="#">佳能</a><i>(30440) </i></li>
<li><a href="#">索尼</a><i>(27220) </i></li>
<li><a href="#">三星</a><i>(20808) </i></li>
<li><a href="#">尼康</a><i>(17821) </i></li>
<li><a href="#">松下</a><i>(12289) </i></li>
<li><a href="#">卡西欧</a><i>(8242) </i></li>
<li><a href="#">富士</a><i>(14894) </i></li>
<li><a href="#">柯达</a><i>(9520) </i></li>
<li><a href="#">宾得</a><i>(2195) </i></li>
<li><a href="#">理光</a><i>(4114) </i></li>
<li><a href="#">奥林巴斯</a><i>(12205) </i></li>
<li><a href="#">明基</a><i>(1466) </i></li>
<li><a href="#">爱国者</a><i>(3091) </i></li>
<li><a href="#">其它品牌相机</a><i>(7275) </i></li>
</ul>
<div class="showmore">
<a href="more.html"><span>显示全部品牌</span></a>
</div>
</div>
</body>
</html>
四、jQuery事件操作
① $( function(){} ) 和 window.onload = function(){}的区别?
他们分别是在什么时候触发?
1、jQuery 的页面加载完成之后是浏览器的内核解析完页面的标签创建好DOM 对象之后就会马上执行。
2、原生js 的页面加载完成之后,除了要等浏览器内核解析完标签创建好DOM 对象,还要等标签显示时需要的内容加载完成。
他们触发的顺序?
1、jQuery 页面加载完成之后先执行
2、原生js 的页面加载完成之后
他们执行的次数?
1、原生js 的页面加载完成之后,只会执行最后一次的赋值函数。
2、jQuery 的页面加载完成之后是全部把注册的function 函数,依次顺序全部执行。
② jQuery 中其他的事件处理方法:
click() 它可以绑定单击事件,以及触发单击事件
mouseover() 鼠标移入事件
mouseout() 鼠标移出事件
bind() 可以给元素一次性绑定一个或多个事件。
one() 使用上跟bind 一样。但是one 方法绑定的事件只会响应一次。
unbind() 跟bind 方法相反的操作,解除事件的绑定
live() 也是用来绑定事件。它可以用来绑定选择器匹配的所有元素的事件。哪怕这个元素是后面动态创建出来的也有效
练习:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function () {
$("#btn1").click(function () {
console.log("鼠标点击");
});
$("#btn1").mouseover(function () {
console.log("鼠标移入");
});
$("#btn1").mouseout(function () {
console.log("鼠标移出");
});
$("#btn1").bind("click mouseover mouseout",function () {
console.log("bind绑定事件");
});
$("#btn1").one("click mouseover mouseout",function () {
console.log("one绑定事件");
});
$(":button").live("click mouseover mouseout",function () {
console.log("live绑定的事件");
})
$("<button>后创建的button</button>").insertAfter("#btn1");
});
</script>
</head>
<body>
<button id="btn1">button</button>
</body>
</html>
③ 事件的冒泡
什么是事件的冒泡?
事件的冒泡是指,父子元素同时监听同一个事件。当触发子元素的事件的时候,同一个事件也被传递到了父元素的事件里去响应。
那么如何阻止事件冒泡呢?
在子元素事件函数体内,return false; 可以阻止事件的冒泡传递。
练习:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function () {
$("#div1").click(function () {
alert("绑定div1的单机事件");
});
$("button").click(function () {
alert("绑定button的单机事件");
return false;
});
});
</script>
</head>
<body>
<div id="div1"><button>div内部的button</button></div>
</body>
</html>
④ javaScript 事件对象
事件对象,是封装有触发的事件信息的一个javascript 对象。
我们重点关心的是怎么拿到这个javascript 的事件对象。以及使用。
如何获取呢javascript 事件对象呢?
在给元素绑定事件的时候,在事件的function( event ) 参数列表中添加一个参数,这个参数名,我们习惯取名为event。
这个event 就是javascript 传递参事件处理函数的事件对象。
练习:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
window.onload = function () {
document.getElementById("btn1").onclick = function (event) {
console.log(event);
}
}
$(function () {
$("#btn1").click(function (event) {
console.log(event);
});
});
$(function () {
$("#btn1").bind("mouseover mouseout",function (event) {
if (event.type == "mouseover") {
console.log("鼠标移入");
}else if (event.type == "mouseout"){
console.log("鼠标移出");
}
});
});
</script>
</head>
<body>
<button id="btn1">button</button>
</body>
</html>
五、练习:事件图片跟随
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
body {
text-align: center;
}
#small {
margin-top: 150px;
}
#showBig {
position: absolute;
display: none;
}
</style>
<script type="text/javascript" src="script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
$("#small").bind("mouseover mouseout mousemove",function (event) {
if (event.type == "mouseover") {
$("#showBig").show();
} else if (event.type == "mouseout") {
$("#showBig").hide();
} else if (event.type == "mousemove") {
console.log(event);
$("#showBig").offset({
left:event.pageX+10,
top:event.pageY+10
});
}
});
});
</script>
</head>
<body>
<img id="small" src="img/small.jpg" />
<div id="showBig">
<img src="img/big.jpg">
</div>
</body>
</html>