(难度:13%)动态背景的实现

看起来好炫酷, 其实抓住了要点, 其实现实起来还是挺简单的. 而且, 关键是浏览器兼容性还不错 ^_^

<!DOCTYPE html>
<!-- 又是上面这句坑爹的话搞到不能对齐 -->
<html>
<head>
	<meta charset='utf-8'>
	<!-- http://code.jquery.com/jquery-2.1.1.min.js
	这个版本的jquery与ie8不兼容 -->
	<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

	<style>
		body{
			margin:30px auto;
			width: 900px;
		}
		h2{
			/*这种字体和颜色比较可爱一点*/
			font-family: Lucida Sans Unicode, Trebuchet MS, Verdana, Arial, Helvetica, sans-serif;
			color: #396A88;
		}
		.container1{
			margin:30px auto;
			background: url('./images/gradient.jpg');
			height: 358px;
			width: 899px;

		}
		.container1 .head_title{
			padding: 150px 0px 0px 0px;
			background: url('./images/overlay.png');
			font-size: 120px;
			color: white;
			line-height: 100px;
			text-align: right;
			/*两个一起加才行*/
			font-weight: bold;
			font-family: Arial, Helvetica, Verdana, sans-serif;
		}
		.container2{
			background: url('./images/gradient.jpg');
			width: 640px;
			margin:30px auto;
		}
		.container2 .head_title{
			background: url('./images/overlay.png') 50% 10px;
		}
		.container2 img{
		}
	</style>
</head>
<body>
	<h2>Example 1(动态背景, 鼠标控制移动方向)</h2>
	<div class="container1">
		<div class='head_title' >
			Hello<br>World!
		</div>
	</div>
	<br/>
	<br/>
	<h2>Example 2(动态字体)</h2>
	<div class='container2'>
		<div class='head_title' >
			<img src='./images/example2.png'>
		</div>
	</div>
</body>
</html>
<script>
	//h1和show1()的定义必须放在$(function())的外面
	//不然jquery初始化完了, h1和show1就会被删掉了,就没效果了
	var h1=Math.floor(Math.random()*2000);
	var dir1=1;
	var $con1=$(".container1");
	function show1(){
		h1+=4*dir1;
		h1%=2000;
		$con1.css("background-position","50% "+h1+"px");
	}
	var h2=Math.floor(Math.random()*2000);
	var $con2=$(".container2");
	function show2(){
		h2+=21;
		h2%=2000;
		$con2.css("background-position","50% "+h2+"px");
	}
	$(function(){
		$con1.css("background-position","50% "+h1+"px");
		setInterval("show1()", 50);
		$con2.css("background-position","50% "+h2+"px");
		setInterval("show2()", 50);
		$con1.mouseover(function(){
			dir1=-1;
		}).mouseleave(function(){
			dir1=1;
		})
	});
</script>


效果如下(动态的效果在图片显示不出来...):


更多实际运行的动态效果请点击:在线演示


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值