事件冒泡
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>事件冒泡</title>
<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>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
$('body').click(function() {
alert(4);
});
$('.grandfather').click(function() {
alert(3);
});
$('.father').click(function() {
alert(2);
});
$('.son').click(function(event) {//event代表当前事件
alert(1);
// console.log(event);//显示很多属性,其中clientX、clientY就是点击的坐标
// alert("X轴坐标:" + event.clientX);
// //阻止事件冒泡
// event.stopPropagation();
//合并阻止操作:把阻止冒泡和阻止默认行为合并
return false;
});
//阻止右键菜单
$(document).contextmenu(function(event){
// //阻止默认行为(原来右键能弹出菜单,阻止后无法弹出)
// event.preventDefault();
//合并阻止
return false;
})
})
</script>
</head>
<body>
<div class="grandfather">
<div class="father">
<div class="son"></div>
</div>
</div>
</body>
</html>
定时器弹框
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>定时器弹框</title>
<style type="text/css">
.pop_con{
display: none;/*默认不显示,用定时器显示*/
}
.pop{
width: 400px;
height: 300px;
background-color: #fff;
border: 1px solid #000;
position: fixed;/*使用固定定位*/
left: 50%;/*左上角位于页面中心*/
top: 50%;
margin-left: -200px;/*让div向左偏移半个宽度、向上偏移半个高度,使div位于页面中心*/
margin-top: -150px;
z-index: 9999;/*弹窗在最前面*/
}
/*遮罩样式*/
.mask{
position: fixed;
width: 100%;
height: 100%;
background-color: #000;
left: 0;
top: 0;
/*设置透明度30%,兼容IE6、7、8*/
opacity: 0.3;
filter: alpha(opacity=30);
z-index: 9990;/*遮罩在弹窗后面*/
}
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
$('#btn').click(function() {
$('#pop').show();
return false;
});
$('#shutoff').click(function() {
$('#pop').hide();
});
//点弹框以外的地方,也能让弹框消失
$(document).click(function(){
// setTimeout(function(){
// $('#pop').hide();
// },2000);
$('#pop').hide();
});
$('.pop').click(function() {
return false;
});
//阻止默认行为(原来超链接可跳转到百度,阻止后无法跳转)
$('#link1').click(function() {
return false;
});
})
</script>
</head>
<body>
<h1>首页标题</h1>
<p>页面内容</p>
<a href="http://www.baidu.com" id="link1">百度网</a>
<input type="button" name="" value="弹出" id="btn">
<div class="pop_con" id="pop">
<div class="pop">
<h3>提示信息!</h3>
<a href="#" id="shutoff">关闭</a>
<input type="text" name="">
</div>
<!-- 遮罩层 -->
<div class="mask"></div>
</div>
</body>
</html>
事件委托
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>事件委托</title>
<style type="text/css">
.list{
list-style: none;
}
.list li{
height: 30px;
background-color: green;
margin-bottom: 10px;
color: #fff;
}
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
/*
给每个li绑定事件,一共绑定了8次,性能不高
$('.list li').click(function() {
alert($(this).html());
});
*/
/*
事件委托:方法delegate,只绑定一次事件,冒泡触发
参数:
selector选择器:写入ul下面的所有要发生事件的元素,多个元素用空格隔开,例如‘li a span’
eventType事件
function要执行的操作
$('.list').delegate('li', 'click', function() {
//$(this)指发生事件的子集,即每个li
alert($(this).html());
//全部取消委托
$('.list').undelegate();
});
})
</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>
幻灯片
HTML
<!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" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>幻灯片</title>
<link rel="stylesheet" type="text/css" href="css/reset.css" />
<link rel="stylesheet" type="text/css" href="css/slide.css">
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="js/slide.js"></script>
</head>
<body>
<div class="center_con">
<div class="slide fl">
<ul class="slide_pics">
<li><a href=""><img src="images/slide01.jpg" alt="幻灯片" /></a></li>
<li><a href=""><img src="images/slide02.jpg" alt="幻灯片" /></a></li>
<li><a href=""><img src="images/slide03.jpg" alt="幻灯片" /></a></li>
<li><a href=""><img src="images/slide04.jpg" alt="幻灯片" /></a></li>
</ul>
<div class="prev"></div>
<div class="next"></div>
<ul class="points">
<!-- 动态创建小圆点
<li class="active"></li>
<li></li>
<li></li>
<li></li> -->
</ul>
</div>
</div>
</body>
</html>
CSS
body,ul{
margin:0;
padding:0;
}
ul{list-style:none;}
.pages_con{
position:fixed;
left:0;
top:0;
width:100%;
overflow:hidden;
}
.pages{
height:600px;/*每个屏幕高度都不相同,先给个预设值600*/
position:relative;
}
.page1{ background-color:orange;}
.page2{ background-color:lightgreen;}
.page3{ background-color:cyan;}
.page4{ background-color:pink;}
.page5{ background-color:lightblue;}
.points{
width:16px;
height:176px;
position:fixed;
right:20px;
top:50%;
margin-top:-88px;
}
.points li{
width:13px;
height:13px;
margin:16px 0;
border-radius:50%;
border:1px solid #666;
cursor:pointer;
}
.points li.active{
background-color:#666;
}
.main_con{
width:900px;
height:400px;
position:absolute;
left:50%;
top:50%;
margin-left:-450px;
margin-top:-200px;
}
.main_con .left_img{
width:363px;
height:400px;
float:left;
position:relative;
left:-50px;
opacity:0;
filter:alpha(opacity=0);
/*css3过渡动画:所有属性同时变 时长 运动曲线 延迟*/
transition:all 1000ms ease 300ms;
}
.main_con .right_info{
width:500px;
height:300px;
margin-top:50px;
float:right;
font-family:'Microsoft Yahei';
font-size:20px;
line-height:50px;
color:#666;
text-indent:2em;
text-align:justify;
position:relative;
right:-50px;
opacity:0;
filter:alpha(opacity=0);
transition:all 1000ms ease 300ms;
}
.moving .main_con .left_img{
left:0;
opacity:1;
filter:alpha(opacity=100);
}
.moving .main_con .right_info{
right:0;
opacity:1;
filter:alpha(opacity=100);
}
.main_con .right_img{
width:522px;
height:400px;
float:right;
position:relative;
top:-50px;
opacity:0;
filter:alpha(opacity=0);
transition:all 1000ms ease 300ms;
}
.main_con .left_info{
width:350px;
height:300px;
margin-top:50px;
float:left;
font-family:'Microsoft Yahei';
font-size:20px;
line-height:50px;
color:#666;
text-indent:2em;
text-align:justify;
position:relative;
bottom:-50px;
opacity:0;
filter:alpha(opacity=0);
transition:all 1000ms ease 300ms;
}
.moving .main_con .right_img{
top:0;
opacity:1;
filter:alpha(opacity=100);
}
.moving .main_con .left_info{
bottom:0;
opacity:1;
filter:alpha(opacity=100);
}
.main_con .center_img{
width:611px;
height:337px;
position:absolute;
left:50%;
margin-left:-305px;
bottom:-50px;
opacity:0;
filter:alpha(opacity=0);
transition:all 1000ms ease 300ms;
}
.moving .main_con .center_img
{
bottom:0;
opacity:1;
filter:alpha(opacity=100);
}
JS
$(function(){
var $li = $('.slide_pics li');
var $prev = $('.prev');
var $next = $('.next');
// alter($li.)
var len = $li.length;
// 当前是4张图片
var nextli = 0;
// 将要运动过来的li
var nowli = 0;
//将要离开的li
var timer = null;
// 定时器循环播放
$li.not(':first').css({left:600});
// 除第一个li,都是定位到右侧
// 动态创建小圆点
$li.each(function(index(){
// 创建li
var $sli = $('<li></li>');
// 第一个li添加选中的样式
if(index == 0){
$sli.addClass('active');
}
// 将i添加到ul中
$sli.appendTo('.points');
})
$points = $('.points li');
$points.click(function(){
nextli = $($(this).index();
// 当点击当前张的小圆点时,不做任何操作,防止跳变得Bug
if(nextli == nowli){
return;
}
move();
$(this).addClass('active').siblings().removeClass('active')
})
$prev.click(function() {
nextli--;
move();
// 改变圆点样式
$points.eq(nextli).addClass('active').siblings().removeClass('active')
});
$next.click(function() {
nextli++;
move();
// 改变圆点样式
$points.eq(nextli).addClass('active').siblings().removeClass('active')
});
$('.slide').mouseenter(function(event) {
ClearInterval(timer);
});
$('.slide').mouseleave(function(event) {
timer = setInterval(autoplay,3000);
});
// 定时器循环自动播放
timer = setInterval(autoplay,3000);
// 自动播放的逻辑与点击下一张是相同的
function autoplay(){
nextli++;
move();
// 改变圆点样式
$points.eq(nextli).addClass('active').siblings().removeClass('active')
}
function move(){
// 走到第一张,再继续走时
if(nextli < 0){
nextli = len -1;
// 将要来的是最后一张
nowli = 0;
$li.eq(nextli).css({left:-600})
// 把最后一张定位到左侧,准备
$li.eq(nowli).stop().animate({left:600});
//离开地第一张走到右侧
$li.eq(nextli).stop().animate({left:600});
// 进入的最后一张走进来
nowli = nextli;
return;
// 小边代码是正常情况的,极端情况下不执行,直接返回
}
// 走到最后一张,再继续走时
if(nextli >len -1){
nextli =0;
// 将要来的是第一张
nowli = len -1;
// 要离开的最后一张
$li.eq(nextli).css({left:600})
// 把第一张定位到右侧,准备
$li.eq(nowli).stop().animate({left:-600});
//离开地最后一张走到左侧
$li.eq(nextli).stop().animate({left:600});
// 进入的最后一张走进来
nowli = nextli;
return;
// 小边代码是正常情况的,极端情况下不执行,直接返回
}
if(nextli>nowli){
$li.eq(nextli).css({left:600});
// 当前张要离开
$li.eq(nowli).stop().animate({left:-600});
}else{
$li.eq(nextli).css({left:-600});
$li.eq(nextli).stop().animate({left:600});
nowli = nextli;
}
//要进入
$li.eq(nextli).stop().animate({left:0});
nowli = nextli;
}
})