导航栏固定 和 回到顶部 防抖动兼容ie6
2018-01-04 13:52
399 查看
js:
//固定定位和回到顶部
angular.element($window).bind('load', function() {
$scope.aa = function(obj, type, fn) { //窗口加载运行为指定的函数
if (obj.attachEvent) {
obj.attachEvent('on' + type, function() {
fn.call(obj);
})
} else {
obj.addEventListener(type, fn, false);
}
}
$scope.aa(window, 'scroll', function() {
var top = document.documentElement.scrollTop || document.body.scrollTop; //滚轮滚动的距离
var fixed_nav = document.getElementById("header_nav"); //获取到导航栏id
var brand_nav = document.getElementById('brandNav');
if (top > 130) { //当滚动距离大于130px时执行下面的东西
fixed_nav.style.position = 'fixed';
fixed_nav.style.top = '0';
fixed_nav.style.backgroundColor = 'rgba(253, 253, 253, 0.9)';
fixed_nav.style.boxShadow = '0 0 5px #888';
fixed_nav.style.zIndex = '9999';
brand_nav.style.position = 'fixed';
brand_nav.style.top = '78px';
brand_nav.style.width = '100%';
brand_nav.style.zIndex = '9999';
} else { //当滚动距离小于130的时候执行下面的内容,也就是让导航栏恢复原状
fixed_nav.style.position = 'static';
fixed_nav.style.boxShadow = 'none';
brand_nav.style.position = 'static';
}
});
var oTop = document.getElementById("to_top");
var screenw = document.documentElement.clientWidth || document.body.clientWidth;
var screenh = document.documentElement.clientHeight || document.body.clientHeight;
$scope.aa(window, 'scroll', function() {
var scrolltop = document.documentElement.scrollTop || document.body.scrollTop;
if (scrolltop > 666) {
oTop.style.display = 'block';
} else {
oTop.style.display = 'none';
}
});
var timer = null;
oTop.onclick = function() {
cancelAnimationFrame(timer);
timer = requestAnimationFrame(function fn() {
var oTops = document.body.scrollTop || document.documentElement.scrollTop;
if (oTops > 0) {
document.body.scrollTop = document.documentElement.scrollTop = oTops - 50;
timer = requestAnimationFrame(fn);
} else {
cancelAnimationFrame(timer);
}
});
}
});
css:
body,
html {
width: 100%;
overflow-y: auto;
overflow-x: hidden;
font-size: 14px;
z-index: 0;
background: #ededed;
}
body{
/*background-image:url(about:blank); /* for IE6 */ */
background-attachment:fixed; /*必须*/
}
#to_top{
width:40px;
height:40px;
position: fixed;
z-index: 999;
right:0;
bottom:0;
_position:absolute;
_top:expression(documentElement.scrollTop + documentElement.clientHeight-this.offsetHeight);
overflow:hidden;
*zoom:1;
cursor:pointer;
background-color: rgba(255,255,255,0.6);
display: none;
}
html:
<!--回到顶部<-->
<div id="to_top"></div>
//固定定位和回到顶部
angular.element($window).bind('load', function() {
$scope.aa = function(obj, type, fn) { //窗口加载运行为指定的函数
if (obj.attachEvent) {
obj.attachEvent('on' + type, function() {
fn.call(obj);
})
} else {
obj.addEventListener(type, fn, false);
}
}
$scope.aa(window, 'scroll', function() {
var top = document.documentElement.scrollTop || document.body.scrollTop; //滚轮滚动的距离
var fixed_nav = document.getElementById("header_nav"); //获取到导航栏id
var brand_nav = document.getElementById('brandNav');
if (top > 130) { //当滚动距离大于130px时执行下面的东西
fixed_nav.style.position = 'fixed';
fixed_nav.style.top = '0';
fixed_nav.style.backgroundColor = 'rgba(253, 253, 253, 0.9)';
fixed_nav.style.boxShadow = '0 0 5px #888';
fixed_nav.style.zIndex = '9999';
brand_nav.style.position = 'fixed';
brand_nav.style.top = '78px';
brand_nav.style.width = '100%';
brand_nav.style.zIndex = '9999';
} else { //当滚动距离小于130的时候执行下面的内容,也就是让导航栏恢复原状
fixed_nav.style.position = 'static';
fixed_nav.style.boxShadow = 'none';
brand_nav.style.position = 'static';
}
});
var oTop = document.getElementById("to_top");
var screenw = document.documentElement.clientWidth || document.body.clientWidth;
var screenh = document.documentElement.clientHeight || document.body.clientHeight;
$scope.aa(window, 'scroll', function() {
var scrolltop = document.documentElement.scrollTop || document.body.scrollTop;
if (scrolltop > 666) {
oTop.style.display = 'block';
} else {
oTop.style.display = 'none';
}
});
var timer = null;
oTop.onclick = function() {
cancelAnimationFrame(timer);
timer = requestAnimationFrame(function fn() {
var oTops = document.body.scrollTop || document.documentElement.scrollTop;
if (oTops > 0) {
document.body.scrollTop = document.documentElement.scrollTop = oTops - 50;
timer = requestAnimationFrame(fn);
} else {
cancelAnimationFrame(timer);
}
});
}
});
css:
body,
html {
width: 100%;
overflow-y: auto;
overflow-x: hidden;
font-size: 14px;
z-index: 0;
background: #ededed;
}
body{
/*background-image:url(about:blank); /* for IE6 */ */
background-attachment:fixed; /*必须*/
}
#to_top{
width:40px;
height:40px;
position: fixed;
z-index: 999;
right:0;
bottom:0;
_position:absolute;
_top:expression(documentElement.scrollTop + documentElement.clientHeight-this.offsetHeight);
overflow:hidden;
*zoom:1;
cursor:pointer;
background-color: rgba(255,255,255,0.6);
display: none;
}
html:
<!--回到顶部<-->
<div id="to_top"></div>
相关文章推荐
- CSS_导航栏+固定浮动的回到顶部按钮
- jQuery&CSS 顶部和底部固定浮动工具栏 兼容IE6 (转)
- 无抖动层回到顶部(兼容所有主流浏览器)
- CSS固定DIV,导航条顶部固定fixed(兼容IE6) 推荐
- jQuery&CSS 顶部和底部固定浮动工具栏 兼容IE6
- css返回顶部图标固定在浏览器右下角且兼容ie6
- Ie6下fixed兼容回到顶部
- 用css固定div层在页面顶部和底部(兼容IE6)
- 用CSS固定DIV层在页面顶部和底部(兼容IE6)
- 回到顶部代码(兼容IE6)
- IE6解决无法实现position:fixed浮动层固定在滚动页面(无抖动)
- Bootstrap实现渐变顶部固定自适应导航栏
- 基于jquery的返回顶部效果(兼容IE6)
- 返回顶部JS(完美兼容IE6)
- CSS行为expression轻松实现IE6无抖动固定定位
- 当导航栏滚动到浏览器顶部时,固定导航栏
- expression解决IE6下固定定位的兼容
- 固定不动的层(兼容IE6)
- 固定顶部导航栏和左侧边栏滚动条为右下方的一类布局分析
- Jquery实现侧边栏跟随滚动条固定(兼容IE6)