滚动视差stellar.js
下载stellar:官网下载
stellar的使用:
stellar 依赖jquery,引入stellar.js前需要先引入jquery
<script src="path/to/jquery/jquery.min.js"></script>
<script src="path/to/jquery.stellar.min.js"></script>
完成后,开始给页面添加视觉差滚动效果。 这个插件允许将效果添加到任何滚动的元素,
例如window
对象,或者其他元素。 要使用jQuery的选择器选中所需要的元素,在绑定stellar()
方法即可。
$('#someElement').stellar();
对于window
对象可以用下面的方法:
$.stellar();
演示:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>视差滚动效果</title>
// 添加一些CSS: 在演示中将使用六个图像,每个重复两次。
//因为要给最后五元素添加data-stellar-background-ratio
属性,所以还要设置background-attachment: fixed;
。
//CSS代码如下所示:
<style>
body {
font-size: 20px;
color: white;
text-shadow: 0 1px 0 black, 0 0 5px black;
text-align: center;
}
p {
padding: 0 0.5em;
margin: 0;
}
.content {
background-attachment: fixed;
/*width: 1000px;*/
height: 400px;
}
#content1 {
background: url("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1582553127201&di=0e015691d1c9d2c7d0a9c88e93227852&imgtype=0&src=http%3A%2F%2Fa3.att.hudong.com%2F68%2F61%2F300000839764127060614318218_950.jpg") no-repeat;
background-size: cover;
}
#content2 {
background: url("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1582553127201&di=85c23a42f388a879208d7193b9410269&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F21%2F09%2F01200000026352136359091694357.jpg") no-repeat;
background-size: cover;
}
#content3 {
background: url("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1582553127201&di=8bf147c05e307fd8705fad502b194db9&imgtype=0&src=http%3A%2F%2Fa2.att.hudong.com%2F36%2F48%2F19300001357258133412489354717.jpg") no-repeat;
background-size: cover;
}
#content4 {
background: url("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1582553127201&di=f3dab15aebd241d6766e83bda518cc10&imgtype=0&src=http%3A%2F%2Fa0.att.hudong.com%2F78%2F52%2F01200000123847134434529793168.jpg") no-repeat;
background-size: cover;
}
#content5 {
background: url("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1582553127200&di=2e6c8c6fdfbd0599c67c9c15887b2bbb&imgtype=0&src=http%3A%2F%2Fa3.att.hudong.com%2F14%2F75%2F01300000164186121366756803686.jpg") no-repeat;
background-size: cover;
}
#content6 {
background: url("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1582553127200&di=1f79f34b41bdf187e480cd89adb220fc&imgtype=0&src=http%3A%2F%2Fa0.att.hudong.com%2F16%2F12%2F01300535031999137270128786964.jpg") no-repeat;
background-size: cover;
}
</style>
</head>
<body>
<div class="content" id="content1">
<p>TEXT HERE</p>
</div>
<div class="content" id="content2" data-stellar-background-ratio="0.5">
<p>TEXT HERE</p>
</div>
<div class="content" id="content3" data-stellar-background-ratio="0.5">
<p>TEXT HERE</p>
</div>
<div class="content" id="content4" data-stellar-background-ratio="0.5">
<p>TEXT HERE</p>
</div>
<div class="content" id="content5" data-stellar-background-ratio="0.5">
<p>TEXT HERE</p>
</div>
<div class="content" id="content6" data-stellar-background-ratio="0.5">
<p>TEXT HERE</p>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/jquery.stellar.min.js/"></script>
<script>
$(function () {
// 让content的高度等于屏幕高度
$(".content").height($(window).height());
$.stellar({
horizontalScrolling: false, // 设置垂直 视察滚动效果,不是水平的
verticalScrolling: true,
});
});
</script>
</body>
</html>
属性的使用