jQuery基本使用
<div></div>
<script>
// 1. 等着页面DOM加载完毕再去执行js 代码
// $(document).ready(function() {
// $('div').hide();
// })
// 2. 等着页面DOM加载完毕再去执行js 代码
$(function() {
$('div').hide();
})
</script>
jQuery的顶级对象$
- $ 是jQuery的别称(另外的名字)
- $同时也是jQuery的 顶级对象
// $(function() {
// alert(11)
// });
jQuery(function() {
// alert(11)
// $('div').hide();
jQuery('div').hide();
});
jQuery对象和DOM对象
1、DOM 对象: 用原生js获取过来的对象就是DOM对象
var myDiv = document.querySelector(‘div’); // myDiv 是DOM对象
2. jQuery对象: 用jquery方式获取过来的对象是jQuery对象。
本质:通过$把DOM元素进行了包装
$(‘div’); // $(‘div’)是一个jQuery 对象
jQuery 对象只能使用 jQuery 方法,DOM 对象则使用原生的 JavaScirpt 属性和方法
DOM对象和jQuery对象相互转换
<video src="mov.mp4" muted></video>
<script>
// 1. DOM对象转换为 jQuery对象
// (1) 我们直接获取视频,得到就是jQuery对象
// $('video');
// (2) 我们已经使用原生js 获取过来 DOM对象
var myvideo = document.querySelector('video');
// $(myvideo).play(); jquery里面没有play 这个方法
// 2. jQuery对象转换为DOM对象
// myvideo.play();
$('video')[0].play()
$('video').get(0).play()
</script>
jQuery基本和层级选择器
$("#id")…ID
$("*")…全选
$(".class")…同一类class
$(“div”)…同一类所有标签
$(“div,p,h”)…并集 多个
$(“li.current”)…交集
$(“ul>li”)…亲儿子
$(“ul li”)…ul下所有li,包括儿子和孙子
jQuery隐式迭代
隐式迭代就是把匹配的所有元素内部进行遍历循环,给每一个元素添加css这个方法
$(“ul li”).css(“color”, “red”);
jQuery筛选选择器
$(“ul li:first”).css(“color”, “red”);
$(“ul li:last”).css(“color”, “red”);
$(“ul li:eq(2)”).css(“color”, “blue”);
$(“ol li:odd”).css(“color”, “skyblue”);
$(“ol li:even”).css(“color”, “pink”);
jQuery筛选方法
1、parent() 返回的是 最近一级的父级元素 亲爸爸
console.log($(".son").parent());
2、children(“p”) 类似子代选择器 ul>li
$(".nav").children(“p”).css(“color”, “red”);
3、find(“p”) 类似于后代选择器 包括儿子和孙子
$(".nav").find(“p”).css(“color”, “red”);
4、siblings(“li”) 除了自身元素之外的所有亲兄弟
$(“ol .item”).siblings(“li”).css(“color”, “red”);
5、eq(2) 第n个元素
var index = 2;
// (1) 我们可以利用选择器的方式选择
// $(“ul li:eq(2)”).css(“color”, “blue”);
// $(“ul li:eq(”+index+")").css(“color”, “blue”);
// (2) 我们可以利用选择方法的方式选择 更推荐这种写法
// $(“ul li”).eq(2).css(“color”, “blue”);
// $(“ul li”).eq(index).css(“color”, “blue”);
6、hasClass(“current”) 判断是否有某个类名
console.log($(“div:first”).hasClass(“current”));
7、nextAll()当前元素之前的所有元素 / prevAll()
操作样式之css方法
$("div").css("width", 300); 属性名一定要加引号
$("div").css({
width: 400,
height: 400,
backgroundColor: "red"
// 如果是复合属性则必须采取驼峰命名法,如果值不是数字,则需要加引号
})
操作样式之类操作
<script>
$(function() {
// 1. 添加类 addClass()
// $("div").click(function() {
// // $(this).addClass("current");
// });
// 2. 删除类 removeClass()
// $("div").click(function() {
// $(this).removeClass("current");
// });
// 3. 切换类 toggleClass()
$("div").click(function() {
$(this).toggleClass("current");
});
})
</script>
类操作与className
原生Js中className会覆盖元素原先的类名。
jQuery中类操作只是对指定类进行操作,不影响原先的类。
<div class="one two"></div>
<script>
// var one = document.querySelector(".one");
// one.className = "two";
// $(".one").addClass("two"); 这个addClass相当于追加类名 不影响以前的类名
$(".one").removeClass("two");
</script>
jQuery效果之显示与隐藏
eg:显示:show([ speed, [easing], [fn] ])
speed: 速度(slow fast 1000)。
easing: 切换效果(swing linear)。
fn: 回调函数,在动画完成时执行的函数,每个函数执行一次。
<body>
<button>显示</button>
<button>隐藏</button>
<button>切换</button>
<div></div>
<script>
$(function() {
$("button").eq(0).click(function() {
$("div").show(1000, function() {
alert(1);
});
})
$("button").eq(1).click(function() {
$("div").hide(1000, function() {
alert(1);
});
})
$("button").eq(2).click(function() {
$("div").toggle(1000);
})
// 一般情况下,我们都不加参数直接显示隐藏就可以了
});
</script>
</body>
jQuery效果之滑动.
<script>
$(function() {
$("button").eq(0).click(function() {
// 下滑动 slideDown()
$("div").slideDown();
})
$("button").eq(1).click(function() {
// 上滑动 slideUp()
$("div").slideUp(500);
})
$("button").eq(2).click(function() {
// 滑动切换 slideToggle()
$("div").slideToggle(500);
});
});
</script>
jQuery停止动画排队stop方法.
// stop 方法必须写到动画的前面
$(this).children(“ul”).stop().slideToggle();
jQuery效果之淡入淡出
<script>
$(function() {
$("button").eq(0).click(function() {
// 淡入 fadeIn()
$("div").fadeIn(1000);
})
$("button").eq(1).click(function() {
// 淡出 fadeOut()
$("div").fadeOut(1000);
})
$("button").eq(2).click(function() {
// 淡入淡出切换 fadeToggle()
$("div").fadeToggle(1000);
});
$("button").eq(3).click(function() {
// 修改透明度 fadeTo() 这个速度和透明度要必须写
$("div").fadeTo(1000, 0.5);
});
});
</script>
jQuery自定义动画效果animate
animate(params, [speed], [easing], [fn] )
params: 想要更改的样式属性,以对象形式传递,必写。
<button>动起来</button>
<div></div>
<script>
$(function() {
$("button").click(function() {
$("div").animate({
left: 500,
top: 300,
opacity: .4,
width: 500
}, 500);
})
})
</script>
案例
新浪下拉菜单.
<body>
<ul class="nav">
<li>
<a href="#">微博</a>
<ul>
<li><a href="">私信</a> </li>
<li><a href="">评论</a> </li>
<li><a href="">@我</a></li>
</ul>
</li>
<li>
<a href="#">微博</a>
<ul>
<li><a href="">私信</a></li>
<li><a href="">评论</a></li>
<li><a href="">@我</a></li>
</ul>
</li>
</ul>
<script>
$(function() {
// 鼠标经过
$(".nav>li").mouseover(function() {
// $(this) jQuery 当前元素 this不要加引号
// show() 显示元素 hide() 隐藏元素
$(this).children("ul").show();
});
// 鼠标离开
$(".nav>li").mouseout(function() {
$(this).children("ul").hide();
})
})
</script>
</body>
css
<style>
* {
margin: 0;
padding: 0;
}
li {
list-style-type: none;
}
a {
text-decoration: none;
font-size: 14px;
}
.nav {
margin: 100px;
}
.nav>li {
position: relative;
float: left;
width: 80px;
height: 41px;
text-align: center;
}
.nav li a {
display: block;
width: 100%;
height: 100%;
line-height: 41px;
color: #333;
}
.nav>li>a:hover {
background-color: #eee;
}
.nav ul {
display: none;
position: absolute;
top: 41px;
left: 0;
width: 100%;
border-left: 1px solid #FECC5B;
border-right: 1px solid #FECC5B;
}
.nav ul li {
border-bottom: 1px solid #FECC5B;
}
.nav ul li a:hover {
background-color: #FFF5DA;
}
</style>
jQuery排他思想 & 链式编程
<body>
<button>快速</button>
<button>快速</button>
<button>快速</button>
<button>快速</button>
<button>快速</button>
<button>快速</button>
<button>快速</button>
<script>
$(function() {
// 1. 隐式迭代 给所有的按钮都绑定了点击事件
$("button").click(function() {
// 2. 当前的元素变化背景颜色
$(this).css("background", "pink");
// 3. 其余的兄弟去掉背景颜色 隐式迭代
$(this).siblings("button").css("background", "");
// 链式编程
//$(this).css("color", "red").siblings().css("color", "");
});
})
</script>
</body>
tab栏切换.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
li {
list-style-type: none;
}
.tab {
width: 978px;
margin: 100px auto;
}
.tab_list {
height: 39px;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
.tab_list li {
float: left;
height: 39px;
line-height: 39px;
padding: 0 20px;
text-align: center;
cursor: pointer;
}
.tab_list .current {
background-color: #c81623;
color: #fff;
}
.item_info {
padding: 20px 0 0 20px;
}
.item {
display: none;
}
</style>
<script src="jquery.min.js"></script>
</head>
<body>
<div class="tab">
<div class="tab_list">
<ul>
<li class="current">商品介绍</li>
<li>规格与包装</li>
<li>售后保障</li>
<li>商品评价(50000)</li>
<li>手机社区</li>
</ul>
</div>
<div class="tab_con">
<div class="item" style="display: block;">
商品介绍模块内容
</div>
<div class="item">
规格与包装模块内容
</div>
<div class="item">
售后保障模块内容
</div>
<div class="item">
商品评价(50000)模块内容
</div>
<div class="item">
手机社区模块内容
</div>
</div>
</div>
<script>
$(function() {
// 1.点击上部的li,当前li 添加current类,其余兄弟移除类
$(".tab_list li").click(function() {
// 链式编程操作
$(this).addClass("current").siblings().removeClass("current");
// 2.点击的同时,得到当前li 的索引号
var index = $(this).index();
console.log(index);
// 3.让下部里面相应索引号的item显示,其余的item隐藏
$(".tab_con .item").eq(index).show().siblings().hide();
});
})
</script>
</body>
</html>
简洁版滑动下拉菜单.
<script>
$(function() {
// 鼠标经过
// $(".nav>li").mouseover(function() {
// // $(this) jQuery 当前元素 this不要加引号
// // show() 显示元素 hide() 隐藏元素
// $(this).children("ul").slideDown(200);
// });
// // 鼠标离开
// $(".nav>li").mouseout(function() {
// $(this).children("ul").slideUp(200);
// });
// 1. 事件切换 hover 就是鼠标经过和离开的复合写法
// $(".nav>li").hover(function() {
// $(this).children("ul").slideDown(200);
// }, function() {
// $(this).children("ul").slideUp(200);
// });
// 2. 事件切换 hover 如果只写一个函数,那么鼠标经过和鼠标离开都会触发这个函数
$(".nav>li").hover(function() {
// stop 方法必须写到动画的前面
$(this).children("ul").stop().slideToggle();
});
})
</script>