javascript - 粘性 div 脚本在 chrome 中不起作用

标签 javascript jquery html css google-chrome

我正在尝试使用以下粘性 div 脚本,它在除 chrome 之外的所有浏览器中都能正常工作。到目前为止,我已经在 IE6、7、8 和 Firefox 上对其进行了测试。

有人知道为什么吗?

下面是JavaScript代码...

var offsetfromedge=0      //offset from window edge when content is "docked". Change if desired.
var dockarray=new Array() //array to cache dockit instances
var dkclear=new Array()   //array to cache corresponding clearinterval pointers

function dockit(el, duration){
this.source=document.all? document.all[el] : document.getElementById(el);
this.source.height=this.source.offsetHeight;
this.docheight=truebody().clientHeight;
this.duration=duration;
this.pagetop=0;
this.elementoffset=this.getOffsetY();
dockarray[dockarray.length]=this;
var pointer=eval(dockarray.length-1);
var dynexpress='dkclear['+pointer+']=setInterval("dockornot(dockarray['+pointer+'])",100);';
dynexpress=(this.duration>0)? dynexpress+'setTimeout("clearInterval(dkclear['+pointer+']); dockarray['+pointer+'].source.style.top=0", duration*1000)' : dynexpress;
eval(dynexpress);
}

dockit.prototype.getOffsetY=function(){
var totaloffset=parseInt(this.source.offsetTop);
var parentEl=this.source.offsetParent;
while (parentEl!=null){
totaloffset+=parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function dockornot(obj){
obj.pagetop=truebody().scrollTop;
if (obj.pagetop>obj.elementoffset) //detect upper offset
obj.source.style.top=obj.pagetop-obj.elementoffset+offsetfromedge+"px";
else if (obj.pagetop+obj.docheight<obj.elementoffset+parseInt(obj.source.height)) //lower offset
obj.source.style.top=obj.pagetop+obj.docheight-obj.source.height-obj.elementoffset-offsetfromedge+"px";
else
obj.source.style.top=0;
}

function truebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

我正在使用以下教程 http://www.dynamicdrive.com/dynamicindex17/dockcontent.htm

最佳答案

尝试将 JS 代码放在 html 文件的末尾,而不是放在 head 标签中,有时浏览器会顺序加载页面,有时会出错

关于javascript - 粘性 div 脚本在 chrome 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7330709/

相关文章:

javascript - 为什么 CSS 自定义属性的级联不起作用?

javascript - 如何使用 javascript 将滚动事件记录到控制台

jquery - 使用 jQuery 获取所有 mailto 链接

javascript - HTML5 Boilerplate jQuery 库回退如何工作?

javascript - 堆栈和嵌套无法将 v3 转换为 v4

jquery:使用多个动画(jquery 和 css3)会减慢动画速度吗?

javascript - 如何在基于 JQM 的网站中使用 Jquery 在列表中选择一个选项

php - 如何在下拉列表中显示选中的项目名称?

javascript - 如何在我的博主博客上更好地显示代码?

javascript - 设置背景颜色,但不是整个背景