滚动后页面有问题

来源:3-2 JavaScript技术实现(下)

剪不断理还乱

2016-09-27 19:59

<script type="text/javascript">

/*var jWindow = $(window);

//滚动事件

jWindow.scroll(function(){

//窗口滚动的高度

var scrollHeight = jWindow.scrollTop();

//屏幕可减去

var screenHeight = jWindow.height();

//左侧滚动栏的高度

var sideHeight = $('#side').height();


if(scrollHeight + screenHeight > sideHeight){//滚动的高度加上屏幕的高度大于左侧栏的高度

$('#side').css({

'position':'fixed',

'top':-(sideHeight-screenHeight)+1,

'right':0

});

}else{

$('#side').css({

'position':'static',

});

}

});

window.onload = function(){

jWindow.trigger('scroll');

};

jWindow.resize(function(){

jWindow.trigger('scroll');//jquery方法

});*/

//返回的动引用

var $  = function(id){

return document.getElementById(id);

}

//一个元素绑定多个函数

var addEvent = function (obj,event,fn){

if(obj.addEventListener){

obj.addEventListener(event,fn,false);//冒泡或捕获的方式

}else if(obj.attachEvent){

obj.attachEvent('on'+event,fn);//ie 接受的事件模型

}

}



var domSider = $('side');

var scrollEvent  = function(){

var sideHeight = domSider.offsetHeight; //offsetHeight直接获取侧栏的高度

var screenHeight  = document.documentElement.clientHeight || document.body.clientHeight;

var scrollHeight = document.documentElement.scrollTop || document.body.scrollTop;

if(screenHeight+scrollHeight > sideHeight){

domSider.style.cssText = 'position:fixed;right:0px;top:'+(-(sideHeight-screenHeight))+'px';

}else{

domSider.style.position = 'static';

}

}


addEvent(window,'scroll',function(){

scrollEvent();

});

addEvent(window,'resize',function(){

scrollEvent();

});

滚动后::::

57ea5e830001dab605000243.jpg

滚动前:::57ea5e840001b18405000242.jpg

</script>


写回答 关注

2回答

  • Css_1
    2017-10-18 18:51:00
    测试了你的代码,并没有错误,应该是你css的布局问题了
  • 慕粉3835875
    2016-09-28 18:08:39

    var scrollEvent = function(){
       var sideHeight = domSider.offsetHeight;
       var screenHeight =document.documentElement.clientHeight||document.body.clientHeight;
       var scrollHeight = document.documentElement.scrollTop||document.body.scrollTop;
       //滚动高度+屏幕高度>边栏高度。,此时要设置position:fixed
       if(scrollHeight+screenHeight>sideHeight){
           domSider.style.cssText = 'position:fixed;right:0px;top:'+(-(sideHeight-screenHeight))+'px';
       }else{
           domSider.style.position='static';
       }
    }

固定边栏滚动特效

剖析淘宝首页固定边栏滚动特效,通过JavaScript、jQuery等前端技术实现

38892 学习 · 54 问题

查看课程

相似问题