jQuery练习

电影排行榜

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src='../jQuery/jquery-1.12.4.js'></script>
		<style type="text/css">
			.box {
				width: 300px;
				height: 450px;
				margin: 50px auto;
				border: 1px solid #000;
			}

			.box h1 {
				font-size: 20px;
				line-height: 35px;
				color: deeppink;
				padding-left: 10px;
				border-bottom: 1px dashed #ccc;
			}

			li {
				list-style: none;
				padding: 5px 10px;
				border: 1px dashed #ccc;
			}

			li span {
				display: inline-block;
				width: 20px;
				height: 20px;
				background: #ccc;
				text-align: center;
				line-height: 20px;
				margin-right: 10px;
			}

			* {
				margin: 0;
				padding: 0;
			}

			.content {
				overflow: hidden;
				margin-top: 5px;
				display: none;
			}

			.current .content {
				display: block;
			}

			.content img {
				float: left;
				width: 80px;
				height: 120px;
			}

			.content p {
				float: right;
				width: 180px;
				height: 120px;
				float: right;
				font-size: 12px;
				line-height: 20px;
			}
		</style>
		<script>
			$(function() {
				$('li').hover(function(){
					$(this).addClass('current')
				},function(){
					$(this).removeClass('current')
				})
			})
		</script>
	</head>
	<body>
		<div class="box">
			<h1>电影排行榜</h1>
			<ul>
				<li >
					<span>1</span>战狼
					<div class='content'>
						<img src='zl.jpg' />
						<p>简介:故事发生在非洲附近的大海上,主人公冷锋(吴京 饰)遭遇人生滑铁卢,被“开除军籍”,本想漂泊一生的他,正当他打算这么做的时候,一场突如其来的意外打破了他的计划,突然被卷入了一场...</p>
					</div>
				</li>
				<li>
					<span>2</span>战狼
					<div class='content'>
						<img src='zl.jpg' />
						<p>简介:故事发生在非洲附近的大海上,主人公冷锋(吴京 饰)遭遇人生滑铁卢,被“开除军籍”,本想漂泊一生的他,正当他打算这么做的时候,一场突如其来的意外打破了他的计划,突然被卷入了一场...</p>
					</div>
				</li>
				<li>
					<span>3</span>战狼
					<div class='content'>
						<img src='zl.jpg' />
						<p>简介:故事发生在非洲附近的大海上,主人公冷锋(吴京 饰)遭遇人生滑铁卢,被“开除军籍”,本想漂泊一生的他,正当他打算这么做的时候,一场突如其来的意外打破了他的计划,突然被卷入了一场...</p>
					</div>
				</li>
				<li>
					<span>4</span>战狼
					<div class='content'>
						<img src='zl.jpg' />
						<p>简介:故事发生在非洲附近的大海上,主人公冷锋(吴京 饰)遭遇人生滑铁卢,被“开除军籍”,本想漂泊一生的他,正当他打算这么做的时候,一场突如其来的意外打破了他的计划,突然被卷入了一场...</p>
					</div>
				</li>
				<li>
					<span>5</span>战狼
					<div class='content'>
						<img src='zl.jpg' />
						<p>简介:故事发生在非洲附近的大海上,主人公冷锋(吴京 饰)遭遇人生滑铁卢,被“开除军籍”,本想漂泊一生的他,正当他打算这么做的时候,一场突如其来的意外打破了他的计划,突然被卷入了一场...</p>
					</div>
				</li>
				<li>
					<span>6</span>战狼
					<div class='content'>
						<img src='zl.jpg' />
						<p>简介:故事发生在非洲附近的大海上,主人公冷锋(吴京 饰)遭遇人生滑铁卢,被“开除军籍”,本想漂泊一生的他,正当他打算这么做的时候,一场突如其来的意外打破了他的计划,突然被卷入了一场...</p>
					</div>
				</li>
			</ul>
		</div>
	</body>
</html>

Tab选项卡

通过addClass和removeClass设置鼠标移入选项卡时的动态效果
siblings()是获取同类其他元素
index()可以获取当前li的索引
通过display是none和block控制元素显示和隐藏。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src='../jQuery/jquery-1.12.4.js'></script>
		<script>
			$(function(){
				$('.nav>li').mouseenter(function(){
					//console.log($(this).siblings())
					$(this).addClass('current')//给当前移入的nav设置背景严肃
				    var index = $(this).index()//获取当前li的索引
					$(this).siblings().removeClass('current')
					var $li = $('.content>li').eq(index)//根据nav中当前li的索引找到content中对应的li的索引
					$li.addClass('show')
					$li.siblings().removeClass('show')
					})
			})
		</script>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}

			.box {
				width: 440px;
				height: 298px;
				border: 1px solid #000;
				margin: 50px auto;
			}

			.nav li {
				list-style: none;
				width: 110px;
				height: 50px;
				background: orange;
				text-align: center;
				line-height: 50px;
				float: left;
			}
			
			.nav .current{
				background-color: #ccc;
			}
			.content li {
				list-style: none;
				display: none;
			}

			.content .show {
				display: block;
			}
		</style>
	</head>
	<body>
		<div class='box'>
			<ul class='nav'>
				<li class='current'>h5+c3</li>
				<li>jQuery</li>
				<li>C语言</li>
				<li>Go语言</li>
			</ul>
			<ul class='content'>
				<li class='show'><img src='11.jpg' /></li>
				<li><img src='12.jpg' /></li>
				<li><img src='13.jpg' /></li>
				<li><img src='14.jpg' /></li>
			</ul>
		</div>
	</body>
</html>

折叠菜单

children()方法获取当前元素的子元素
transform能让元素旋转

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src='../jQuery/jquery-1.12.4.js'></script>
		<script>
			$(function(){
				$('.nav>li').click(function(){
					 $(this).children('.sub').slideDown(1000);
					 $(this).siblings().children('.sub').slideUp(1000)
					 $(this).addClass('current')
					 $(this).siblings().removeClass('current')
				})
			})
		</script>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}

			.nav {
				width: 300px;
				margin: 100px auto;
			}

			.nav>li {
				border: 1px solid black;
				list-style: none;
				border-bottom: none;
				line-height: 35px;
				text-indent: 2em;
				position: relative;
			}

			.nav>li:last-child {
				border-bottom: 1px solid black;
				border-bottom-left-radius: 10px;
				border-bottom-right-radius: 10px;
			}

			.nav>li:first-child {
				border-top-left-radius: 10px;
				border-top-right-radius: 10px;
			}

			.nav>li>span {
				background: url('arrow_right.png') no-repeat center;
				display: inline-block;
				height: 32px;
				width: 32px;
				position: absolute;
				right: 10px;
				top: 5px;
			}
			
			.nav>.current>span{
				transform:rotate(90deg);
			}

			.sub {
				display: none;
			}

			.sub>li {
				list-style: none;
				background: green;
				line-height: 30px;
				border-bottom: 1px white solid;
				text-indent: 2em;
			}

			.sub>li:last-child {
				border-bottom: none;
			}

			.sub>li:hover {
				background: red;
			}
		</style>
	</head>
	<body>
		<ul class='nav'>
			<li>一级菜单<span></span>
				<ul class='sub'>
					<li>二级菜单</li>
					<li>二级菜单</li>
					<li>二级菜单</li>
					<li>二级菜单</li>
				</ul>
			</li>
			<li>一级菜单<span></span>
				<ul class='sub'>
					<li>二级菜单</li>
					<li>二级菜单</li>
					<li>二级菜单</li>
					<li>二级菜单</li>
				</ul>
			</li>
			<li>一级菜单<span></span>
				<ul class='sub'>
					<li>二级菜单</li>
					<li>二级菜单</li>
					<li>二级菜单</li>
					<li>二级菜单</li>
				</ul>
			</li>
			<li>一级菜单<span></span>
				<ul class='sub'>
					<li>二级菜单</li>
					<li>二级菜单</li>
					<li>二级菜单</li>
					<li>二级菜单</li>
				</ul>
			</li>
			<li>一级菜单<span></span>
				<ul class='sub'>
					<li>二级菜单</li>
					<li>二级菜单</li>
					<li>二级菜单</li>
					<li>二级菜单</li>
				</ul>
			</li>
		</ul>

	</body>
</html>

弹窗广告

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src='../jQuery/jquery-1.12.4.js'></script>
		<script>
			$(function(){
				$('div>span').click(function(){
					$('div').remove()
				})
				$('div').stop().slideDown(1000).fadeOut(1000).fadeIn(1000)
			})
		</script>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}

			div {
				position: fixed;
				display: none;
				right: 0;
				bottom: 0;
			}

			div > span {
				position: absolute;
				display: inline-block;
				width: 20px;
				height: 20px;
				top: 0;
				right: 0;
			}
		</style>
	</head>
	<body>
		<div>
			<img src='ad-pic.png' />
			<span></span>
		</div>
	</body>
</html>

图标特效

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src='../jQuery/jquery-1.12.4.js'></script>
		<script>
			$(function(){
				$('li').each(function(index,ele){
					var url = "url('bg.png') no-repeat 0 " + (index * -24 )+ "px"
					$(this).children('span').css('background',url)
				})
				$('li').mouseenter(function(){
					$(this).children('span').animate({
						top:-50
					},1000,function(){
						$(this).css('top','50px')
						$(this).animate({
							top:0
						},1000)
					})
				})
			})
		</script>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}

			ul {
				list-style: none;
				width: 400px;
				height: 250px;
				border: 1px solid #000;
				margin: 100px auto;
			}

			ul>li>span {
				background: url('bg.png') no-repeat 0 0;
				display: inline-block;
				width: 24px;
				height: 24px;
				position: relative;
			}

			ul>li {
				float: left;
				width: 100px;
				height: 50px;
				margin-top: 50px;
				text-align: center;
				overflow: hidden;
			}
		</style>
	</head>
	<body>
		<ul>
			<li><span></span>
				<p>百度</p>
			</li>
			<li><span></span>
				<p>淘宝</p>
			</li>
			<li><span></span>
				<p>微博</p>
			</li>
			<li><span></span>
				<p>网易</p>
			</li>
			<li><span></span>
				<p>搜狐</p>
			</li>
			<li><span></span>
				<p>腾讯</p>
			</li>
			<li><span></span>
				<p>优酷</p>
			</li>
			<li><span></span>
				<p>京东</p>
			</li>
		</ul>
	</body>
</html>

循环滚动播放

利用定时器和左边距为负值设置图片左移滚动播放

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src='../jQuery/jquery-1.12.4.js'></script>
		<script>
			$(function(){
				var timer
				var offset = 0
				function autoplay(){
					timer = setInterval(function(){
						offset += -10
						if(offset <= -1200){
							offset = 0
						}
						$('ul').css({
							marginLeft:offset
						})
					},50)
				}
				autoplay()
				$('li').hover(function(){
					clearInterval(timer)//停止滚动
					$(this).siblings().fadeTo(1000,0.5)//给未选中的图片设置蒙版
					$(this).fadeTo(1000,1)
				},function(){
					autoplay()//光标移出时开启定时器滚动
					$('li').fadeTo(1000,1)//所有图片恢复原始状态
				})
			})
		</script>
		<style type="text/css">
			* {
				margin: 0 ;
				padding:0;
			}

			div {
				height: 161px;
				width: 600px;
				overflow: hidden;
				margin: 100px auto;
				border: 1px solid black;
			}

			ul {
				list-style: none;
				height: 161px;
				width: 1800px;
			}

			li {
				float: left;
			}
		</style>
	</head>
	<body>
		<div>
			<ul>
				<li><img src='a.jpg' /></li>
				<li><img src='b.jpg' /></li>
				<li><img src='c.jpg' /></li>
				<li><img src='d.jpg' /></li>
				<li><img src='a.jpg' /></li>
				<li><img src='b.jpg' /></li>
			</ul>
		</div>
	</body>
</html>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值