文章目录
- 初识JS
- JS的引入
- JS输入输出语句
- 变量的命名规范
- 运算符的优先级
- 断点调试
- arguments的使用
- JS预解析
- 对象
- Web APIs
- 获取元素
- 事件
- 排它思想
- 例:百度换肤
- getAttribute和setAttribute
- tab栏切换
- 自定义属性
- 节点操作
- 例:下拉菜单
- 评论留言案例
- 动态生成表格案例
- 方法监听注册事件
- 事件的删除
- DOM事件流
- 事件对象
- 事件委托
- 其他常用的鼠标事件
- 跟随鼠标移动的图片
- 常见的键盘事件
- 模拟京东快递单号查询
- BOM基本概念
- 窗口加载事件
- 调整窗口大小事件
- 定时器
- 倒计时效果
- JS执行机制
- location对象
- 5s之后自动跳转页面案例
- 获取url参数
- navigator对象
- history对象
- 元素偏移量offset系列
- 拖动模块框
- client
- scroll
- 立即执行函数
- 仿淘宝侧边栏
- 三大系列对比
- 动画
- 网页轮播图
- 本地存储
- 记住用户名案例
- jQuery概述
- jQuery选择器
- jQuery修改样式
- 淘宝服饰精品案例
- jQuery效果
- 滑动效果
- 事件切换
- 淡入淡出效果
- 高亮显示案例
- 自定义动画
- 王者荣耀手风琴效果
- jQuery属性操作
- jQuery内容文本值
- 增减商品数量案例
- jQuery元素操作
- jQuery元素操作
- jQuery尺寸、位置操作
- jQuery事件
- jQuery对象拷贝
- jQuery多库共存
- jQuery插件
初识JS
JS的引入
1.行内式的js 直接写到元素的内部
<body>
<input type="button" value="唐伯虎" onclick="alert('秋香姐')">
</body>
2.内嵌式的js
<script>
alert('zyj');
</script>
3.外部js
<script src="my.js"></script>
JS输入输出语句
变量的命名规范
运算符的优先级
断点调试
arguments的使用
JS预解析
对象
创建对象的第一种方法
创建对象的第二种方法
创建对象的第三种方法
new关键字的执行过程
对象的遍历
Web APIs
DOM
获取元素
querySelector获取
事件
事件三要素:
1.事件源:事件被触发的对象
2.事件类型:如何触发 什么事件
3.事件处理程序:通过一个函数赋值的方式完成
例:
排它思想
例:百度换肤
getAttribute和setAttribute
tab栏切换
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.tab{
margin: 100px auto;
width: 800px;
height: 100px;
}
.tab_list{
width: 800px;
height: 40px;
background-color: #ccc;
}
.tab_list ul li{
display: block;
height: 40px;
line-height: 40px;
float: left;
margin-left: 50px;
list-style: none;
}
.current{
background-color: red;
color: #fff;
}
.tab_con div{
display: none;
}
</style>
</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>
var tab_list=document.querySelector('.tab_list');
var lis =tab_list.querySelectorAll('li');
var items=document.querySelectorAll('.item')
for(var i=0;i<lis.length;i++){
lis[i].setAttribute('index',i)
lis[i].onclick=function(){
for(var i=0;i<lis.length;i++){
lis[i].className='';
}
this.className='current';
var index=this.getAttribute('index');
for(var i=0;i<items.length;i++)
{
items[i].style.display='none'
}
items[index].style.display='block'
}
}
</script>
</body>
</html>
自定义属性
节点操作
例:下拉菜单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.nav{
margin: 100px auto;
height: 100px;
background-color: pink;
}
.nav>li{
float: left;
margin-left: 30px;
}
.nav li ul{
display: none;
}
</style>
</head>
<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>
<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>
var nav=document.querySelector('.nav');
var lis=nav.children;
for(var i=0;i<lis.length;i++){
lis[i].onmouseover=function(){
this.children[1].style.display='block';
}
lis[i].onmouseout=function(){
this.children[1].style.display='none';
}
}
</script>
</body>
</html>
评论留言案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
ul li{
width: 200px;
}
ul li a{
float: right;
}
</style>
</head>
<body>
<textarea name="" id=""cols="30" rows="10">请输入评论内容</textarea>
<button>发布</button>
<ul>
</ul>
<script>
//1.获取元素
var btn=document.querySelector('button');
var text=document.querySelector('textarea');
var ul=document.querySelector('ul');
//2.注册事件
text.onfocus=function(){
text.value='';
}
btn.onclick=function(){
if(text.value==''){
alert('您没有输入内容');
return false;
}
//1.创建元素
var li=document.createElement('li');
//先有li 才能赋值
li.innerHTML=text.value+"<a href='javascript:;'>删除</a>";
//2.添加元素
ul.insertBefore(li,ul.children[0]);
var del=document.querySelectorAll('a');
//3.删除元素 删除的是当前链接的li 它的父亲
for(var i=0;i<del.length;i++){
del[i].onclick=function(){
//node.removeChild(child);删除的是li 当前a所在的li this.parentNode
ul.removeChild(this.parentNode);
}
}
}
</script>
</body>
</html>
动态生成表格案例
var tbody=document.querySelector('tbody');
for(var i=0;i<datas.length;i++){
var tr=document.createElement('tr');
tbody.appendChild(tr);
for(var k in datas[i]){
var td=document.createElement('td');
td.innerHTML=datas[i][k];
tr.appendChild(td);
}
var td=document.createElement('td');
td.innerHTML='<a href="javascript:;">删除</a>';
tr.appendChild(td);
}
var as=document.querySelectorAll('a');
for(var i=0;i<as.length;i++){
as[i].οnclick=function(){
tbody.removeChild(this.parentNode.parentNode);
}
}
方法监听注册事件
事件的删除
DOM事件流
事件对象
事件委托
其他常用的鼠标事件
跟随鼠标移动的图片
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
img{
position: absolute;
width: 300px;
height: 200px;
}
</style>
</head>
<body>
<img src="resourse/background.jpg" alt="">
<script>
var pic=document.querySelector('img');
document.addEventListener('mousemove',function(e){
var x=e.pageX;
var y=e.pageY;
pic.style.left=x-150+'px';
pic.style.top=y-100+'px';
});
</script>
</body>
</html>
常见的键盘事件
在这里插入图片描述
模拟京东快递单号查询
BOM基本概念
窗口加载事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
window.onload=function(){
var btn=document.querySelector('button');
btn.addEventListener('click',function(){
alert('点击我');
})
}
</script>
<button>点击</button>
</body>
</html>
调整窗口大小事件
定时器
倒计时效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
margin: 100px 300px;
}
div span{
display: block;
float: left;
width: 50px;
height: 50px;
margin-left: 5px;
line-height: 50px;
text-align: center;
background-color: black;
color: white;
}
</style>
</head>
<body>
<div>
<span class="hour"></span>
<span class="minute"></span>
<span class="second"></span>
</div>
<script>
//1.获取元素
var hour=document.querySelector('.hour')
var minute=document.querySelector('.minute')
var second=document.querySelector('.second')
var inputTime=+new Date('2022-5-12 00:00:00');//返回的是用户输入时间总的毫秒数
conutDown();//我们先调用一次这个函数,防止第一次刷新页面有空白
//2.开启定时器
setInterval(conutDown,1000);
function conutDown(){
var nowTime=+new Date(); //返回当前时间总的毫秒数
var times=(inputTime-nowTime)/1000;//times是剩余时间总的秒数
var h=parseInt(times/60/60%24);//时
h=h<10?'0'+h:h;
hour.innerHTML=h;//把剩余的小时给小时盒子
var m=parseInt(times/60%60);//分
m=m<10?'0'+m:m;
minute.innerHTML=m;//把剩余的分钟给分钟盒子
var s=parseInt(times%60);//当前的秒
s=s<10?'0'+s:s;
second.innerHTML=s;//把剩余的秒数给秒数盒子
}
</script>
</body>
</html>
JS执行机制
location对象
5s之后自动跳转页面案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button>点击</button>
<div></div>
<script>
var btn=document.querySelector('button');
var div=document.querySelector('div');
btn.addEventListener('click',function(){
location.href='http://www.baidu.com'
})
var timer=5;
setInterval(function(){
if(timer==0){
location.href='http://www.baidu.com';
}else{
div.innerHTML='您将在'+timer+'秒钟之后跳转百度';
timer--;
}
},1000)
</script>
</body>
</html>
获取url参数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="index.html">
用户名:<input type="text" name="uname">
<input type="submit" value="登录">
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div></div>
<script>
console.log(location.search);//?uname=andy
//1.先去掉? substr('起始位置',截取几个字符)
var params=location.search.substr(1);
console.log(params);
//2.利用=把字符串分割成数组 split('=');
var arr=params.split('=');
//3.把数据写入div中
var div=document.querySelector('div');
div.innerHtml=arr[1]+'欢迎您';
</script>
</body>
</html>
navigator对象
history对象
元素偏移量offset系列
拖动模块框
<script>
//1.获取元素
var login=document.querySelector('.login');
var mask=document.querySelector('.login-bg');
var link=document.querySelector('#link');
var closeBtn=document.querySelector('#closeBtn');
var title=document.querySelector('#title');
//2.点击弹出层这个链接 link 让mask和login显示出来
link.addEventListener('click',function(){
mask.style.display='block';
login.style.display='block';
})
//3.点击closeBtn就隐藏mask和login
closeBtn.addEventListener('click',function(){
mask.style.display='none';
login.style.display='none';
})
//4.开始拖拽
//(1)当我们鼠标按下,就获得鼠标在盒子内的坐标
title.addEventListener('mousedown',function(e){
var x=e.pageX-login.offsetLeft;
var y=e.pageY-login.offsetTop;
//(2)鼠标移动的时候,把鼠标在页面中的坐标,减去鼠标在盒子内的坐标就是模态框的left和top值
document.addEventListener('mousemove',move)
function move(e){
login.style.left=e.pageX-x+'px';
login.style.top=e.pageY-y+'px';
}
//(3)鼠标弹起,就让鼠标移动事件移除
document.addEventListener('mouseup',function(){
document.removeEventListener('mousemove',move);
})
})
</script>
client
scroll
立即执行函数
立即执行函数的写法:
1.(function(){})();
2.(function(){}());
立即执行函数最大的作用就是 独立创建了一个作用域,里面的所有变量都是局部变量 不会有命名冲突的情况
仿淘宝侧边栏
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.w{
height: 5000px;
width: 1300px;
}
.banner{
height: 1000px;
background-color: red;
}
.header{
height: 172px;
background-color: green;
}
.main{
height: 1000px;
background-color: yellowgreen;
}
.slider_bar{
width:45px;
height:130px;
position:absolute;
left:1310px;
top:300px;
background-color: pink;
}
span{
display: none;
position: absolute;
bottom: 0;
}
</style>
</head>
<body>
<div class="slider_bar">
<span class="goBack">返回顶部</span>
</div>
<div class="header w">头部区域</div>
<div class="banner w">banner区域</div>
<div class="main w">主体区域</div>
<script>
//1.获取元素
var slider_bar=document.querySelector('.slider_bar');
var banner=document.querySelector('.banner');
var bannerTop=banner.offsetTop;
//当我们的侧边栏固定定位之后应该变化的数值
var slider_barTop=slider_bar.offsetTop-bannerTop;
var main=document.querySelector('.main');
var goBack=document.querySelector('.goBack');
var mainTop=main.offsetTop;
//2.页面滚动事件 scroll
document.addEventListener('scroll',function(){
//3.当我们页面被卷进去的头部大于等于了172此时 侧边栏就要改为固定定位
if(window.pageYOffset>=bannerTop){
slider_bar.style.position ='fixed';
slider_bar.style.top=slider_barTop+'px';
}else{
slider_bar.style.position='absolute';
}
//4.当我们页面滚动到main盒子 就显示goback模块
if(window.pageYOffset>=mainTop){
goBack.style.display='block'
}else{
goBack.style.display='none';
}
})
//3.当我们点击了返回顶部模块,就让窗口滚动到页面的最上方
goBack.addEventListener('click',function(){
window.scroll(0,0);
})
</script>
</body>
</html>
三大系列对比
动画
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
position:absolute;
height: 200px;
width: 300px;
background-color: pink;
}
span{
position: absolute;
top: 200px;
left: 0;
display: block;
width: 150px;
height: 150px;
background-color: purple;
}
</style>
</head>
<body>
<div></div>
<span></span>
<script>
//动画函数的封装
function animate(obj,target){
clearInterval(obj.timer)
obj.timer= setInterval(function(){
if(obj.offsetLeft>=target){
//停止动画 本质是停止定时器
clearInterval(obj.timer);
}
obj.style.left=target+'px';
},10);
}
var div=document.querySelector('div');
var span=document.querySelector('span');
//调用函数
animate(div,400,function(){})
animate(span,400,function(){})
</script>
</body>
</html>
网页轮播图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
list-style: none;
}
.w{
width: 720px;
height: 455px;
margin: 0 auto;
}
.main{
width: 720px;
height: 455px;
}
.focus{
overflow: hidden;
position: relative;
width: 720px;
height: 500px;
}
img{
width: 720px;
height: 455px;
}
.focus ul{
position: absolute;
top: 0;
left: 0;
width: 500%;
}
.focus ul li{
float: left;
}
.arrow-l,.arrow-r{
display: none;
position: absolute;
top: 50%;
margin-top: -20px;
width: 24px;
height: 40px;
background: rgba(0,0,0,0.3);
text-align: center;
line-height: 40px;
color: #fff;
font-size: 18px;
z-index: 2;
}
.arrow-r{
right: 0;
}
.circle li{
z-index: 2;
float: left;
width: 8px;
height: 8px;
border: 2px solid rgba(22, 21, 21, 0.5);
margin: 460px 50px;
border-radius: 50%;
cursor: pointer;
}
.current{
background-color: red;
}
</style>
<script>
function animate(obj,target){
clearInterval(obj.timer)
obj.timer= setInterval(function(){
if(obj.offsetLeft>=target){
//停止动画 本质是停止定时器
clearInterval(obj.timer);
}
obj.style.left=target+'px';
},10);
}
window.addEventListener('load',function(){
var arrow_l=document.querySelector('.arrow-l');
var arrow_r=document.querySelector('.arrow-r');
var focus=document.querySelector('.focus');
var focusWidth=focus.offsetWidth;
var circle=0;
var num=0;
//鼠标经过就显示左右按钮
focus.addEventListener('mouseenter',function(){
arrow_l.style.display='block';
arrow_r.style.display='block';
clearInterval(timer);
timer=null;//停止自动播放
})
focus.addEventListener('mouseleave',function(){
arrow_l.style.display='none';
arrow_r.style.display='none';
timer=setInterval(function(){
arrow_r.click();
},5000)
})
//动态生成小圆圈 有几张图片,我就生成几个小圆圈
var ul=focus.querySelector('ul');
var ol=focus.querySelector('.circle');
for(var i=0;i<ul.children.length;i++){
//创建li 然后将li插入到ol中
var li=document.createElement('li');
//记录当前小圆圈的索引号 通过自定义属性来做
li.setAttribute('index',i);
ol.appendChild(li);
li.addEventListener('click',function(){
for(var i=0;i<ol.children.length;i++){
ol.children[i].className='';
}
this.className='current';
//点击小圆圈,移动图片 当然移动的是ul
//ul的移动距离就是小圆圈的索引号乘图片的宽度的负值
//当我们点击了某个li 就拿到当前li的索引号
circle=this.getAttribute('index');
circle=parseInt(circle);
num=circle;
animate(ul,-circle*focusWidth);
})
}
//把ol里面的第一个li设置类名为current
ol.children[0].className='current';
//点击左右按钮,图片滚动一张
arrow_r.addEventListener('click',function(){
num=(num+1)%5;
animate(ul,-num*focusWidth);
circle=(circle+1)%5;
for(var i=0;i<ol.children.length;i++){
ol.children[i].className='';
}
ol.children[circle].className='current';
})
arrow_l.addEventListener('click',function(){
num=(num+4)%5;
console.log(num);
animate(ul,-num*focusWidth);
circle=(circle+4)%5;
console.log(circle);
for(var i=0;i<ol.children.length;i++){
ol.children[i].className='';
}
ol.children[circle].className='current';
})
// 自动播放轮播图
var timer=setInterval(function(){
//手动调动点击事件
arrow_r.click();
},5000);
})
</script>
</head>
<body>
<div class="w">
<div class="main">
<div class="focus f1">
<!--左侧按钮-->
<a href="#" class="arrow-l">
<
</a>
<!--右侧按钮-->
<a href="#" class="arrow-r">
>
</a>
<!-- 核心的滚动区域 -->
<ul>
<li>
<a href="#"><img src="resourse/background.jpg" alt=""></a>
</li>
<li>
<a href="#"><img src="resourse/bg.png" alt=""></a>
</li>
<li>
<a href="#"><img src="resourse/bg2.png" alt=""></a>
</li>
<li>
<a href="#"><img src="resourse/background.jpg" alt=""></a>
</li>
<li>
<a href="#"><img src="resourse/bg.png" alt=""></a>
</li>
</ul>
<!-- 小圆圈 -->
<ol class="circle"> </ol>
</div>
<div class="newsflash fr">
</div>
</div>
</div>
</body>
</html>
本地存储
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text">
<button class="set">存储数据</button>
<button class="get">获取数据</button>
<button class="remove">删除数据</button>
<button class="del">清空所有数据</button>
<script>
var input=document.querySelector('input');
var set=document.querySelector('.set');
var get=document.querySelector('.get');
var remove=document.querySelector('.remove');
var clear=document.querySelector('.del');
set.addEventListener('click',function(){
var value=input.value;
sessionStorage.setItem('uname',value);
});
get.addEventListener('click',function(){
var val=input.value;
sessionStorage.getItem('uname');
});
remove.addEventListener('click',function(){
var val=input.value;
sessionStorage.removeItem('uname');
});
clear.addEventListener('click',function(){
sessionStorage.clear();
})
</script>
</body>
</html>
记住用户名案例
jQuery概述
jQuery与DOM的区别
jQuery选择器
jQuery修改样式
排它思想
$(function(){
//1.隐式迭代 给所有的按钮都绑定了点击事件
$("button").click(function(){
//2.当前的元素变化背景颜色
$(this).css("background","pink");
//3.其余的兄弟去掉背景颜色
$(this).siblings("button").css("background","");
});
})
淘宝服饰精品案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="jquery.min.js"></script>
<script>
$(function(){
//1.鼠标经过左侧的li
$("#left li").mouseover(function(){
//2.得到当前li的索引号
var index=$(this).index();
console.log(index);
//3.让我们右侧的盒子相应索引号的图片显示出来就好了
$("#content div").eq(index).show();
//4.让其余的图片隐藏起来
$("#content div").eq(index).siblings().hide();
})
})
</script>
</head>
<body>
<div class="wrapper">
<ul id="left">
<li><a href="#">女靴</a></li>
<li><a href="#">雪地靴</a></li>
<li><a href="#">冬裙</a></li>
<li><a href="#">呢大衣</a></li>
<li><a href="#">毛衣</a></li>
<li><a href="#">棉服</a></li>
<li><a href="#">女裤</a></li>
</ul>
<div id="content">
<div><a href="#"><img src="resourse/background.jpg" width="200" height="250" alt=""></a></div>
<div><a href="#"><img src="resourse/bg.png" width="200" height="250" alt=""></a></div>
<div><a href="#"><img src="resourse/background.jpg" width="200" height="250" alt=""></a></div>
<div><a href="#"><img src="resourse/background.jpg" width="200" height="250" alt=""></a></div>
<div><a href="#"><img src="resourse/background.jpg" width="200" height="250" alt=""></a></div>
<div><a href="#"><img src="resourse/background.jpg" width="200" height="250" alt=""></a></div>
<div><a href="#"><img src="resourse/background.jpg" width="200" height="250" alt=""></a></div>
</div>
</div>
</body>
</html>
jQuery效果
滑动效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="jquery.min.js"></script>
<style>
div{
display: none;
width: 100px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<button>下拉滑动</button>
<button>上拉滑动</button>
<button>切换滑动</button>
<div></div>
<script>
$("button").eq(0).click(function(){
$("div").slideDown();
})
$("button").eq(1).click(function(){
$("div").slideUp();
})
$("button").eq(2).click(function(){
$("div").slideToggle();
})
</script>
</body>
</html>
事件切换
淡入淡出效果
高亮显示案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
list-style:none;
}
.wrap{
width: 800px;
height: 400px;
}
.wrap ul li a img{
width: 200px;
height: 200px;
}
.wrap ul li{
float: left;
}
</style>
<script src="jquery.min.js"></script>
<script>
$(function(){
//鼠标进入的时候,其他的li标签透明度:0.5
$(".wrap li").hover(function(){
$(this).siblings().stop().fadeTo(400,0.5);
},function(){
$(this).siblings().stop().fadeTo(400,1);
})
//鼠标离开,其他li的透明度改为1
})
</script>
</head>
<body>
<div class="wrap">
<ul>
<li><a href="#"><img src="resourse/background.jpg" alt=""></a></li>
<li><a href="#"><img src="resourse/background.jpg" alt=""></a></li>
<li><a href="#"><img src="resourse/background.jpg" alt=""></a></li>
<li><a href="#"><img src="resourse/background.jpg" alt=""></a></li>
<li><a href="#"><img src="resourse/background.jpg" alt=""></a></li>
<li><a href="#"><img src="resourse/background.jpg" alt=""></a></li>
<li><a href="#"><img src="resourse/background.jpg" alt=""></a></li>
<li><a href="#"><img src="resourse/background.jpg" alt=""></a></li>
</ul>
</div>
</body>
</html>
自定义动画
王者荣耀手风琴效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="jquery.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
ul {
list-style: none;
}
.king {
width: 852px;
margin: 100px auto;
background: url(resourse/back.png) no-repeat;
padding: 10px;
overflow: hidden;
}
.king ul{
overflow: hidden;
}
.king li {
position: relative;
float: left;
width: 69px;
height: 69px;
margin-right:10px;
}
.king li .current{
width: 224px;
}
.king li .current .big{
display: block;
}
.king li .current .small{
display: none;
}
.big{
width: 224px;
display: none;
}
.small{
position: absolute;
top:0;
left:0;
width: 69px;
height: 69px;
border-radius: 5px;
}
</style>
<script>
$(function(){
$(".king li").mouseenter(function(){
//当前li的宽度变为224px,同时里面的小图片淡出,大图片淡入
$(this).stop().animate({
width:224
},200).find(".small").stop().fadeOut().siblings(".big").stop().fadeIn();
//其余兄弟li宽度变为69px 小图片淡入 大图片淡出
$(this).stop().siblings("li").animate({
width:69
}).find(".small").stop().fadeIn().siblings(".big").stop().fadeOut();
})
});
</script>
</head>
<body>
<div class="king">
<ul>
<li><a href="#">
<img src="resourse/zhaoyun1.jpg" alt="" class="small">
<img src="resourse/zhangfei2.png" alt="" class="big">
</a></li>
<li><a href="#">
<img src="resourse/damo1.jpg" alt="" class="small">
<img src="resourse/damo2.png" alt="" class="big">
</a></li>
<li><a href="#">
<img src="resourse/diaochan1.jpg" alt="" class="small">
<img src="resourse/diaochan2.png" alt="" class="big">
</a></li>
<li><a href="#">
<img src="resourse/chengyaojin1.jpg" alt="" class="small">
<img src="resourse/chengyaojin2.png" alt="" class="big">
</a></li>
<li><a href="#">
<img src="resourse/jiangziya1.jpg" alt="" class="small">
<img src="resourse/jiangziya2.png" alt="" class="big">
</a></li>
<li><a href="#">
<img src="resourse/zhangfei1.jpg" alt="" class="small">
<img src="resourse/zhangfei2.png" alt="" class="big">
</a></li>
<li><a href="#">
<img src="resourse/chengjisihan1.jpg" alt="" class="small">
<img src="resourse/chengjisihan2.png" alt="" class="big">
</a></li>
</ul>
</div>
</body>
</html>
jQuery属性操作
全选按钮
jQuery内容文本值
增减商品数量案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="jquery.min.js"></script>
<style>
.box{
height: 40px;
float: left;
background-color: #ccc;
}
.increme{
float: left;
height: 40px;
width: 40px;
}
.number{
float: left;
width: 50px;
height: 40px;
line-height: 40px;
text-align: center;
}
.decreme{
float: left;
height: 40px;
width: 40px;
}
</style>
</head>
<body>
<div class="box">
<button class="increme">+</button>
<input type="text" class="number" value="1">
<button class="decreme">-</button>
</div>
<script>
$(".increme").click(function(){
var n=$(this).siblings(".number").val();
$(this).siblings(".number").val(parseInt(n)+1);
})
$(".decreme").click(function(){
var n=$(this).siblings(".number").val();
$(this).siblings(".number").val(parseInt(n)-1);
})
</script>
</body>
</html>
jQuery元素操作
jQuery元素操作
jQuery尺寸、位置操作
jQuery事件
jQuery对象拷贝
jQuery多库共存
jQuery插件