根据设计图设定每屏的高度,js会自动缩放到全屏尺寸,效果要大尺寸才能看的出来

demo :http://runjs.cn/detail/uvizsekd

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
html,body{
margin:0;padding:0;
}
.pages{
width:100%;
height:100vh;
outline: 1px solid blue;
}
#a,#b,#c{
width:50%;
height:200px;
}
#a ,#c{
background:rgba(212,132,11,.5);
}
#b{
background:rgba(22,32,11,.5);
}
</style>
</head>
<body>
<div class="pages">
<div id="a">123</div>
<div id="c">123</div>
</div>
<div class="pages">
<div id="b">bbb</div>
</div>
<div class="pages">
</div>
</body>
</html>
<script src="https://code.jquery.com/jquery-2.2.4.js" integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI=" crossorigin="anonymous"></script>
<script>
var design_size = {"width" : 1920 }
var pages = [
{
"height" : 4000 //* radio
,"background" : "http://image.tianjimedia.com/uploadImages/2012/221/960UAS60H799.jpg"
,"elems" : [
{"id" : "#a"
,"start" : {"left" : 0 , "top" : 0}
,"end" : {"left": 200 , "top" : 3600}
}
,{"id" : "#c"
,"start" : {"right" : 0 , "bottom" : 4000}
,"end" : {"right": 200 , "bottom" : 0}
}
]
},
{
"height" : 3500 //* radio
,"background" : "http://image.tianjimedia.com/uploadImages/2012/072/S056Y53BS3Q6_1000x500.jpg"
,"elems" : [
{"id" : "#b"
,"start" : {"right" : 0 , "top" : 0}
,"end" : {"right": 500 , "top" : 3200}
}
]
},
{
"height" : '3.0' //* radio
,"background" : "http://image.tianjimedia.com/uploadImages/2013/065/85GSC473318U.jpg"
,"elems" : [
]
},
] </script>
<script>
~function (pg_conf , pg_doms){
var window_height = window.innerHeight
var radio = $(window).width() / design_size.width pg_doms = $(pg_doms)
var h_stack = [0]
,_pos = 0
,_play = 0
,_direct var scene , scene_dom , scene_elems
var animating = false
scrollTo(document.body , 0 ,100)
function scrollTo(element, to, duration) {
animating = true
if (duration <= 0) return animating = false;
var difference = to - element.scrollTop;
var perTick = difference / duration * 10; setTimeout(function() {
element.scrollTop = element.scrollTop + perTick;
if (element.scrollTop == to) return animating = false;
scrollTo(element, to, duration - 10);
}, 10);
}
function play(index ,opt){
if (!pg_conf[index]) {
return
}
opt = opt || {}
_play = index
scene = pg_conf[index]
scene_dom = pg_doms.eq(index)
scene_elems = scene.elems
if (0 && opt.movein){
if ('top' == opt.movein){
var to = h_stack[_play] }else {
var to = h_stack[_play+1] - window_height
}
if (to) scrollTo(document.body , to ,200)
}
///window.scrollTo(0, h_stack[_play])
/*
scene_elems.forEach(function(e_conf){
e_conf.dom.css('position','absolute').css(e_conf.start)
})
*/
}
function roll(s){
if (!scene) return
if (s > scene.scrTotal) return
var process = s / scene.scrTotal //console.log(process , s, scene.scrTotal , scene.height , window.innerHeight)
scene_elems.forEach(function(e_conf){
var ret = {}
//console.log(e_conf)
for (var key in e_conf.end){
if (key in e_conf.start) {
var m = (e_conf.end[key] - e_conf.start[key] )*process + e_conf.start[key]
ret[key] = m
}
}
//console.log(ret)
e_conf.dom.css(ret) }) }
var t = 0
pg_conf.forEach(function(p , i){
var dom = pg_doms.eq(i)
if (/^\d*\.\d*$/.test(p.height)){
var height = p.height * window_height
}else {
var height = p.height * radio
} t += height
h_stack.push(t)
p.height = height
p.scrTotal = height - window_height
dom.css({width:'100%'
,height: height
,position:'relative'
,background: 'url(' + p.background+ ') '
,'background-size':'cover'
,'background-attachment' : 'fixed'
})
p.elems.forEach(function(e_conf){
e_conf.dom = $(e_conf.id).css('position','absolute').css(e_conf.start)
for (var key in e_conf.start){
e_conf.start[key] = e_conf.start[key] * radio
}
for (var key in e_conf.end){
e_conf.end[key] = e_conf.end[key] * radio
}
})
})
play(_play) var _threshold = window_height //- 100
function move(evt){
if (animating) return
var scrl = document.body.scrollTop
if (scrl > _pos) _direct = 'down'
else _direct = 'up'
_pos = scrl var boudary = h_stack[_play]
if ('down' == _direct){
if (_play !== h_stack.length - 2){
var bound_next = h_stack[_play + 1] if (scrl + _threshold > bound_next) {
console.log('next')
play(_play+1 ,{"movein" : "top"})
}
}
}else {
if (_play !== 0){
var bound_next = h_stack[_play ]
if (scrl <= bound_next){
console.log('pre')
play(_play-1 ,{"movein" : "bottom"}) }
}
}
boudary = h_stack[_play]
roll(scrl - boudary) }
window.addEventListener('scroll' , move) }(pages , '.pages')
</script>

最新文章

  1. 前端css兼容性与易混淆的点
  2. 将语音搜索集成到Google Now中
  3. 2018. The Debut Album
  4. C#调用java类、jar包方法(转)
  5. nginx下搭建 Yii框架
  6. AWS助理架构师认证考经
  7. Routine Problem(数学)
  8. hugo-最好用的静态网站生成器
  9. NetCat使用手册
  10. iOS中检测硬件和传感器
  11. ERP
  12. delphi 判断是否出现滚动条
  13. Maven Dependency Scope用法
  14. MVC 4.0语法 自动分页
  15. PHP定义数组常量
  16. 排序算法 - 快速排序(Quick Sort)
  17. jquery.form.js+jquery.validation.js实现表单校验和提交
  18. android universal image loader 缓冲原理详解
  19. Linux之文本编辑器Vim
  20. B - Moo Volume

热门文章

  1. My first blog for java
  2. B - Expression
  3. winform右键菜单
  4. 离线安装ADT和sdk
  5. nginx配置和测试
  6. bootstrap modal 一闪
  7. 在maven项目结构下对于Resources目录下文件的存取
  8. php 单例模式与常驻服务
  9. input type=”file“ change事件只执行一次的问题
  10. 容器化haproxy+keepalived