jQuery 能做什么
jQuery库为Web脚本编程提供了通用(跨浏览器)的抽象层,使得它几乎适用于任何脚本编程的情形。
- 便捷的获取DOM元素
- 动态修改页面样式
- 动态改变DOM内容
- 响应用户的页面操作
- 为页面添加动态效果
- 同意AJAX操作
- 简化常见的JavaScript任务
jQuery 对象和 DOM 原生对象有什么区别?如何转化?
- jQuery对象是通过jQuery包装DOM对象后产生的对象;DOM对象就是一个对象,二jQuery对象是一个类数组对象;jQuery对象不能使用DOM对象的方法进行操作,DOM对象不能使用jQuery对象的方法进行操作
- $(DOM对象) ===> jQuery对象
- (jQuery对象).[0]或者(jQuery对象).get(0) ===> DOM对象
jQuery中如何绑定事件?bind、unbind、delegate、live、on、off都有什么作用?推荐使用哪种?使用on绑定事件使用事件代理的写法?
- bind 为一个元素绑定一个事件处理程序
- unbind 从元素上删除一个以前附加事件处理程序
- delegate 为所有匹配选择器(selector参数)的元素绑定一个或多个事件处理函数,基于一个指定的根元素的子集,匹配的元素包括那些目前已经匹配到的元素,也包括那些今后可能匹配到的元素
- live 附加一个事件处理器到匹配目前选择器的所有元素,现在和未来
- on 在选定的元素上绑定一个或多个事件处理函数
- off 移除一个事件处理函数
现在推荐使用on/off
来添加/移除事件
事件代理示例:
$('table').on('click', 'th', function(e){
console.log(this);
})
jQuery 如何展示/隐藏元素?
1、show()方法
$('.target').show();
匹配的元素将被立即显示,没有动画。
对应的隐藏方法是 .hide();
2、fadeIn() 动画表现形式淡入
.fadeIn( [duration ] [, complete ] )
第一个参数 动画用时
第二个参数 动画完成后执行的函数
$('.target').fadeIn(1000,function(){});
对应的隐藏方法是 fadeOut(),互相切换是 fadeToggle()
3、slideDown() 动画表现形式下拉
.slideDown( [duration ] [, complete ] )
$('.target').slideDown(1000,function(){});
对应的隐藏方法是slideUp(),互相切换是slideToggle()
jQuery 动画如何使用?
.animate( properties [, duration ] [, easing ] [, complete ] )
描述: 根据一组 CSS 属性,执行自定义动画。
animate({left:'80',top},5000,)
properties:一个CSS属性和值的对象,动画将根据这组对象移动。
duration: 动画持续多久,1000,2000...
easing: 过渡使用哪种缓动函数,jQuery自身提供 linear swing 2种
complete: 动画完成时执行的函数
如何设置和获取元素内部 HTML 内容?如何设置和获取元素内部文本?
- 设置元素内部HTML内容:
$("target").html("content")
- 获取元素内部HTML内容:
$("target").html()
- 设置元素内部文本内容:
$("target").text("content")
- 获取元素内部文本内容:
$("target").text()
如何设置和获取表单用户输入或者选择的内容?如何设置和获取元素属性?
- 设置表单用户输入或者选择的内容
$("target").val("content")
- 获取表单用户输入或者选择的内容
$("target").val()
- 设置元素属性
$("target").attr("attributeName","value")
- 获取元素属性
$("target").attr("attributeName")
使用 jQuery实现如下效果
<!doctype html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<style>
ul,li {
margin: 0;
padding: 0;
list-style: none;
}
a {
margin: 0;
padding: 0;
text-decoration: none;
}
.nav>li {
position: relative;
background: red;
height: 45px;
width: 150px;
color: white;
border-bottom: 1px solid white;
}
.nav>li>a {
padding: 0 40px 0 15px;
vertical-align: center;
color: white;
}
span {
line-height: 45px;
vertical-align: center;
}
.child-nav {
position: absolute;
top: 0;
left: 150px;
width: 110px;
border: 1px solid grey;
display: none;
}
.child-nav>ul:after {
content: "";
display: block;
clear: both;
}
.child-nav li {
float: left;
padding: 10px;
}
</style>
<body>
<ul class="nav">
<li>
<a href="">珠宝玉器</a>
<span>></span>
<ul class="child-nav">
<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>
</li>
<li>
<a href="">珠宝玉器</a>
<span>></span>
<ul class="child-nav">
<li><a href="">翡翠</a></li>
<li><a href="">玉石</a></li>
<li><a href="">珍珠</a></li>
<li><a href="">玛瑙</a></li>
</ul>
</li>
<li>
<a href="">珠宝玉器</a>
<span>></span>
<ul class="child-nav">
<li><a href="">翡翠</a></li>
<li><a href="">玉石</a></li>
<li><a href="">珍珠</a></li>
<li><a href="">玛瑙</a></li>
</ul>
</li>
<li>
<a href="">珠宝玉器</a>
<span>></span>
<ul class="child-nav">
<li><a href="">翡翠</a></li>
<li><a href="">玉石</a></li>
<li><a href="">珍珠</a></li>
<li><a href="">玛瑙</a></li>
</ul>
</li>
<li>
<a href="">珠宝玉器</a>
<span>></span>
<ul class="child-nav">
<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>
</li>
</ul>
<script>
$(".nav").on("mouseenter", "li", function(){
$(this).children("ul").css("display", "block")
})
$(".nav").on("mouseleave", "li", function(){
$(this).children("ul").css("display", "none")
})
</script>
</body>
</html>
使用 jQuery 实现如下效果
<!doctype html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<style>
body {
font-size: 12px;
color: #b2b4b7;
}
ul,li,h4,p {
margin: 0;
padding: 0;
}
li {
list-style: none;
}
a {
text-decoration: none;
color: #b2b4b7;
}
.clearfix:after {
content: "";
display: block;
clear: both;
}
.title {
border: 1px solid #b2b4b7;
border-radius: 3px 3px 0px 0px;
margin: 0px;
display: inline-block;
margin-bottom: -4px;
border-bottom: none;
}
.title .btn {
float: left;
padding: 8px 0px;
}
.title .btn a {
display: inline-block;
padding: 0px 8px;
border-right: 1px solid #b2b4b7;
}
.title .btn:last-child a {
border-right: none;
}
.container {
border: 1px solid #b2b4b7;
width: 676px;
margin-bottom: 30px;
}
.container .panel {
margin-left: -20px;
display: none;
}
.container .item {
float: left;
border: 1px solid #b2b4b7;
width: 170px;
padding: 20px;
margin-left: 20px;
margin-bottom: 20px;
position: relative;
}
.container .item img {
display: block;
width: 130px;
margin: 0 auto;
}
.container .item p {
color: red;
}
.container .last {
margin-bottom: 0px;
}
.container .cover {
background-color: rgba(0,0,0,0.2);
z-index: 1;
position: absolute;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
text-align: center;
display: none;
}
.container .cover:after {
content: "";
display: inline-block;
width: 0;
height: 100%;
vertical-align: middle;
}
.container .cover a {
border: 1px solid red;
border-radius: 2px;
color: red;
padding: 15px;
background-color: white;
display: inline-block;
vertical-align: middle;
}
.container .active {
display: block;
}
.hover {
background-color: #b2b4b7;
color: red;
}
</style>
<body>
<div class="wrap">
<ul class="title clearfix">
<li class="btn"><a href="">热门</a></li>
<li class="btn"><a href="">珠宝首饰</a></li>
<li class="btn"><a href="">奢侈品</a></li>
</ul>
<div class="container">
<ul class="panel clearfix active">
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item last">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item last">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item last">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
</ul>
<ul class="panel clearfix">
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-4280e1b80d0974fa.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥3699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item last">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item last">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥199</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item last">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1639</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
</ul>
<ul class="panel clearfix">
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-633911a609643739.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥3699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥2699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item last">
![](http://upload-images.jianshu.io/upload_images/4394888-633911a609643739.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1299</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item last">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥2199</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item last">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1639</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
</ul>
</div>
</div>
<div class="wrap">
<ul class="title clearfix">
<li class="btn"><a href="">热门</a></li>
<li class="btn"><a href="">珠宝首饰</a></li>
<li class="btn"><a href="">奢侈品2</a></li>
</ul>
<div class="container">
<ul class="panel clearfix active">
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item last">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item last">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item last">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
</ul>
<ul class="panel clearfix">
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-4280e1b80d0974fa.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥3699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item last">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item last">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥199</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item last">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1639</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
</ul>
<ul class="panel clearfix">
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-633911a609643739.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥3699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥2699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item last">
![](http://upload-images.jianshu.io/upload_images/4394888-633911a609643739.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1299</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item last">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥2199</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item last">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1639</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
</ul>
</div>
</div>
<script>
$(".item").on("mouseenter", function(){
$(this).children(".cover").addClass("active")
}).on("mouseleave", function(){
$(this).children(".cover").removeClass("active")
})
$(".title .btn").on("click", function(e){
e.preventDefault()
$(this).siblings().removeClass("hover")
$(this).siblings().children("a").removeClass("hover")
$(this).addClass("hover")
$(this).children("a").addClass("hover")
var index = $(this).index()
$(this).parents(".wrap").find(".panel").removeClass("active")
$(this).parents(".wrap").find(".panel").eq(index).addClass("active")
})
</script>
</body>
</html>
实现如下效果
<!doctype html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<style>
body {
font-size: 12px;
color: #b2b4b7;
}
ul,li,h4,p {
margin: 0;
padding: 0;
}
li {
list-style: none;
}
a {
text-decoration: none;
color: #b2b4b7;
}
.clearfix:after {
content: "";
display: block;
clear: both;
}
.container {
width: 676px;
}
.container .panel {
margin-left: -20px;
display: none;
}
.container .item {
float: left;
border: 1px solid #b2b4b7;
width: 170px;
padding: 20px;
margin-left: 20px;
margin-bottom: 20px;
position: relative;
}
.container .item img {
display: block;
width: 130px;
margin: 0 auto;
}
.container .item p {
color: red;
}
.container .cover {
background-color: rgba(0,0,0,0.2);
z-index: 1;
position: absolute;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
text-align: center;
display: none;
}
.container .cover:after {
content: "";
display: inline-block;
width: 0;
height: 100%;
vertical-align: middle;
}
.container .cover a {
border: 1px solid red;
border-radius: 2px;
color: red;
padding: 15px;
background-color: white;
display: inline-block;
vertical-align: middle;
}
.container .active {
display: block;
}
.hover {
background-color: #b2b4b7;
color: red;
}
.btn {
display: inline-block;
padding: 10px;
border: 1px solid red;
color: red;
border-radius: 2px;
}
</style>
<body>
<div class="container">
<h1>珠宝首饰</h1>
<ul class="panel clearfix active">
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
<li class="item">
![](http://upload-images.jianshu.io/upload_images/4394888-2eb84f81ceba3acd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<h4 class="name">藏美千足金和田玉金镶玉路路通吊坠</h4>
<p class="price">¥1699</p>
<div class="cover">
<a href="">立即抢购</a>
</div>
</li>
</ul>
<a href="" class="btn">添加</a>
</div>
<script>
var products = [
{
img: 'http://img10.360buyimg.com/N3/jfs/t2242/92/1446546284/374195/9196ac66/56af0958N1a723458.jpg',
name: '珂兰 黄金手 猴哥款',
price: '¥405.00'
},{
img: 'http://img10.360buyimg.com/N3/jfs/t2242/92/1446546284/374195/9196ac66/56af0958N1a723458.jpg',
name: '珂兰 黄金转运珠 猴哥款',
price: '¥100.00'
},{
img: 'http://img10.360buyimg.com/N3/jfs/t2242/92/1446546284/374195/9196ac66/56af0958N1a723458.jpg',
name: '珂兰 黄金手链 3D猴哥款',
price: '¥45.00'
}
]
$(".btn").on("click", function(e){
e.preventDefault()
addHTML(products)
})
showCover()
function addHTML(json){
var html = ""
for (i=0; i<json.length; i++){
html += '<li class="item">'
html += '![](' + json[i].img + ')'
html += '<h4 class="name">' + json[i].name + '</h4>'
html += '<p class="price">' + json[i].price + '</p>'
html += '<div class="cover">'
html += '<a href="">立即抢购</a>'
html += '</div></li>'
}
console.log(html)
$(".panel").append(html)
}
function showCover(){
$(".panel").on("mouseenter", ".item", function(){
$(this).children(".cover").addClass("active")
}).on("mouseleave", ".item", function(){
$(this).children(".cover").removeClass("active")
})
}
</script>
</body>
</html>
完成 左右切换的 Tab 效果
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Task14-4</title>
<style>
ul,li{
margin: 0;
padding: 0;
list-style: none;
}
.row1{
display: flex;
margin-top: 30px;
height: 30px;
border: 1px solid lightgray;
border-right: none;
}
.tab{
width: 33.33%;
line-height: 30px;
text-align: center;
border-right: 1px solid lightgray;
cursor: pointer;
}
.active {
background: #eee;
}
.row2{
height: 181px;
overflow: hidden;
}
.content{
position: relative;
top: 0;
padding: 20px 0 0 20px;
height: 160px;
background: red;
border: 1px solid lightgray;
border-top: none;
}
.content2{
background: green;
}
.content3{
background: blue;
}
</style>
</head>
<body>
<ul class="row1">
<li class="tab active">tab1</li>
<li class="tab">tab2</li>
<li class="tab">tab3</li>
</ul>
<ul class="row2">
<li class="content">content 1</li>
<li class="content content2">content 2</li>
<li class="content content3">content 3</li>
</ul>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
$('.tab').on('click',function(){
var $cur = $(this);
var idx = $cur.index();
var height = $('.row2').height();
$cur.addClass('active').siblings().removeClass('active');
$cur.parents('body').find('.content').animate({top:-height*idx});
});
</script>
</body>
</html>