首页 > 其他 > 详细

position:sticky 定位 position:fixed

时间:2016-01-28 15:07:31      阅读:156      评论:0      收藏:0      [点我收藏+]

它的表现类似position:relative和position:fixed的合体,当目标区域在屏幕中可见时,它的行为就像position:relative; 而当页面滚动超出目标区域时,它的表现就像position:fixed,它会固定在目标位置。

 
.sticky {
  position: -webkit-sticky;
  position:sticky;
  top: 15px;
}
 
不支持的fall back:
<div class="header"></div>
.sticky {
  position: fixed;
  top: 0;
}
.header {
  width: 100%;
  background: #F6D565;
  padding: 25px 0;
}
var header = document.querySelector(‘.header‘);
var origOffsetY = header.offsetTop;
function onScroll(e) {
  window.scrollY >= origOffsetY ? header.classList.add(‘sticky‘) :
                                  header.classList.remove(‘sticky‘);
}
document.addEventListener(‘scroll‘, onScroll);
 
 
position:fixed
Android 2.2+ 需要添加下面的meta才正常:
<meta name="viewport" content="width=device-width, user-scalable=no">
在旧的IOS  Safari 上也有问题
1、页面滚动的时候fixed元素颤抖
2、旋转屏幕的时候飘走了
3、fixed元素里有focusable的元素(例如input),会让这个fixed元素移位

position:sticky 定位 position:fixed

原文:http://www.cnblogs.com/chuangweili/p/5166212.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!