视差滚动(Parallax Scrolling)是指让多层背景以不同的速度移动,形成立体的运动效果,带来非常出色的视觉体验。作为今年网页设计的热点趋势,越来越多的网站应用了这项技术。
原理
通过前景与背景在场景移动时产生不同的视差,从而达到简单的立体效果
页面上很多的元素在相互独立地滚动着,如果我们来对其它分层的话,可以有两到三层 :背景层,内容层,贴图层
例:QQtim
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--[if lt IE 9]>
<script src="js/html5shiv.min.js"></script>
<![endif]-->
<link rel="stylesheet" href="index.css">
</head>
<body>
<!--导航-->
<nav class="qq_nav">
<div class="wrapper">
<a href="#" class="logo">logo</a>
<ul class="nav">
<li><a href="#" class="active">首页</a></li>
<li><a href="#">下载</a></li>
<li><a href="#">动态</a></li>
</ul>
<div class="right">
<a href="#" class="register">注册</a>
<ul>
<li><a href="#">登录</a></li>
<li><a href="#">QQ会员</a></li>
<li><a href="#">QQ安全</a></li>
</ul>
<p>当前在线人数:151,655644</p>
</div>
</div>
</nav>
<!--展示-->
<section class="qq_banner">
<img src="images/banner-logo.png" alt="">
<img src="images/banner-download.png" alt="">
<img src="images/banner-list.png" alt="">
<img src="images/banner-product.png" alt="">
</section>
<!--语音-->
<section class="qq_sound">
<h1></h1>
<div class="bg" data-stellar-background-ratio="0.1"></div>
<div class="content">
<div class="phone"></div>
<h2>沟通,是跨越<br>
万水千山的亲切声音
</h2>
<p>
无论何时何地,你都能自由享受QQ在各类终端</br>上带来的高清通话,与
好友想聊多久就聊多久
</p>
</div>
</section>
<!--文件-->
<section class="qq_file">
<div class="bg" data-stellar-background-ratio="0.1"></div>
<div class="content">
<div class="mac"></div>
<h2>沟通,是随时随地<br>
爽快收发
</h2>
<p>
无论何时何地,你都能自由享受QQ在各类终端</br>上带来的高清通话,与
好友想聊多久就聊多久
</p>
</div>
</section>
<!--兴趣-->
<section class="qq_hobby">
<div class="bg" data-stellar-background-ratio="0.1"></div>
<div class="content">
<div class="phone"></div>
<h2>沟通,是志同道合<br>
的他们放肆青春
</h2>
<p>
无论何时何地,你都能自由享受QQ在各类终端</br>上带来的高清通话,与
好友想聊多久就聊多久
</p>
</div>
</section>
<!--底部-->
<section class="qq_footer">
<div class="content">
<div class="plats">
<a href="#" class="phone">QQ手机版</a>
<a href="#" class="pc">QQ PC版</a>
<a href="#" class="mac">QQ mac版</a>
<a href="#" class="pad">QQ pad版</a>
</div>
<p> Copyright 1998-2019 Tencent. All Rights Reserved.</p>
<p>腾讯公司 版权所有</p>
</div>
</section>
<!-- 1、引入插件库
2、对要做视差的背景图片的元素操作:
在结构中添加 data-stellar-background-ratio=0.5
在样式中添加 background-attachment:fixed-->
<script src="js/jquery.min.js"></script>
<script src="js/jquery.stellar.js"></script>
<script>
$(function () {
$.stellar({
responsive:true,
horizontalScrolling:false
});
})
</script>
</body>
</html>
css:
/*重置样式 reset.css==================*/
*{
padding: 0;
margin: 0;
}
body{
font-size: 14px;
font-family: 'Microsoft Yahei';
color: #333;
}
ul {
list-style:none;
}
a{
color:#333;
text-decoration: none;
}
/*模块样式 module.css=================*/
.bg {
background-attachment: fixed;
}
.qq_nav{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 150px;
background: url("images/nav_bg.png");
}
.qq_nav .wrapper{
width: 90%;
height: 75px;
margin: 0 auto;
}
.qq_nav .wrapper .logo{
width: 100px;
height: 75px;
background: url("images/nav_brand.png") no-repeat center;
text-indent: -9999em;
float: left;
}
.qq_nav .wrapper .nav{
float: left;
overflow: hidden;
}
.qq_nav .wrapper .nav li{
float: left;
font-size: 18px;
height:75px;
line-height: 75px;
margin-left:20px;
}
.qq_nav .wrapper .nav li a{
display: block;
padding: 0 20px;
}
.qq_nav .wrapper .nav li a.active,
.qq_nav .wrapper .nav li a:hover{
background-color: #12B7f5;
}
.qq_nav .wrapper .right {
float: right;
}
.qq_nav .wrapper .right .register{
float: left;
height: 36px;
width: 96px;
background-color: #24c1fc;
border-radius: 18px;
text-align: center;
line-height: 36px;
color:#fff;
margin-top: 20px;
}
.qq_nav .wrapper .right ul{
float: left;
overflow: hidden;
margin-top: 10px;
}
.qq_nav .wrapper .right ul li{
float: left;
margin-left: 35px;
}
.qq_nav .wrapper .right ul li a{
text-shadow: 0 0 3px #333;
}
.qq_nav .wrapper .right p{
margin-left: 149px;
margin-top: 44px;
}
/*展示部分*/
.qq_banner {
height: 880px;
padding-top: 150px;
background: url("images/banner-bg.jpg") no-repeat center top;
}
.qq_banner img{
display: block;
margin: 20px auto;
}
.qq_banner img:last-child{
width: 100%;
}
/*语音部分*/
.qq_sound{
}
.qq_sound h1{
width: 100%;
height: 330px;
background: url("images/sound-text.png") no-repeat center/cover;
}
.qq_sound .bg{
height: 500px;
background: url("images/sound-bg.jpg") no-repeat center/cover;
background-attachment: fixed;
}
.qq_sound .content{
height: 500px;
width: 90%;
margin: 0 auto;
}
.qq_sound .content .phone{
width: 700px;
height: 700px;
background: url("images/sound-phone.png") no-repeat center top/cover;
position: absolute;
margin-top: -200px;
}
.qq_sound .content h2{
font-weight: normal;
font-family: '幼圆';
float: right;
font-size: 40px;
text-align: right;
padding-top: 100px;
width: 60%;
}
.qq_sound .content p{
font-weight: normal;
font-family: '幼圆';
float: right;
text-align: right;
width: 60%;
padding-top: 50px;
font-size: 18px;
text-shadow: 0 0 2px #333 ;
}
/*文件部分*/
.qq_file .bg{
height: 500px;
background: url("images/file-bg.jpg") no-repeat center /cover;
}
.qq_file .content{
height: 500px;
width: 90%;
margin: 0 auto;
position: relative;
}
.qq_file .content .mac {
width: 600px;
height: 340px;
position: absolute;
background: url("images/file-mac.png") no-repeat center /cover;
right:-60px;
}
.qq_file .content h2,
.qq_file .content p{
font-weight: normal;
font-family: '幼圆';
float: left;
text-align:left;
width: 60%;
}
.qq_file .content h2{
padding-top: 100px;
font-size: 40px;
}
.qq_file .content p{
font-size: 18px;
padding-top: 50px;
text-shadow: 0 0 2px #333;
}
/*兴趣部分样式*/
.qq_hobby .bg {
height: 500px;
background: url('images/interest-bg.jpg') no-repeat center /cover;
}
.qq_hobby .content {
height: 500px;
width: 95%;
margin: 0 auto;
position: relative;
/*border:1px solid red;*/
}
.qq_hobby .content .phone {
width: 350px;
height: 500px;
background: url('images/interest-phone.png') no-repeat center top/cover;
position: absolute;
left: 0;
top: 0;
}
.qq_hobby .content h2 {
float: right;
font-size: 40px;
text-align: right;
padding-top: 100px;
width: 60%;
font-weight: normal;
font-family: '幼圆';
}
.qq_hobby .content p {
float: right;
text-align: right;
width: 60%;
padding-top: 50px;
font-size: 18px;
text-shadow: 0 0 2px #333;
font-weight: normal;
font-family: '幼圆';
}
/*尾部部分样式*/
.qq_footer {
height: 250px;
background: #2a2a2a;
font-size: 12px;
text-align: center;
padding-top: 50px;
color: #666;
}
.qq_footer .plats {
width: 75%;
height: 150px;
margin: 0 auto;
display: flex;
justify-content: space-around;
}
.qq_footer .plats a {
display: block;
background: url(images/footer.png) no-repeat;
width: 80px;
/*设置字体*/
color: #666;
height: 10px;
padding-top: 60px;
}
.qq_footer .plats a:hover {
color: rgb(18, 183, 245);
}
.qq_footer .plats .phone:hover {
background-position: 0 -93px;
}
.qq_footer .plats .pc {
background-position: -106px 0;
}
.qq_footer .plats .pc:hover {
background-position: -106px -93px;
}
.qq_footer .plats .mac {
background-position: -212px 0;
}
.qq_footer .plats .mac:hover {
background-position: -212px -93px;
}
.qq_footer .plats .pad {
background-position: -318px 0;
}
.qq_footer .plats .pad:hover {
background-position: -318px -93px;
}