【问题标题】:Sticky div script not working in chrome粘性 div 脚本在 chrome 中不起作用
【发布时间】:2011-09-07 08:11:32
【问题描述】:

我正在尝试使用以下粘性 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

【问题讨论】:

  • 好吧,该页面中的演示似乎根本无法在 chrome 中运行...我建议您在这里查看我的答案,尤其是答案的底部。 stackoverflow.com/questions/7215728/…
  • 顺便说一句,它在 safari 中也不起作用

标签: javascript jquery html css google-chrome


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-08-22
    • 2015-01-14
    • 2013-07-09
    • 2012-05-11
    • 1970-01-01
    • 2013-05-23
    相关资源
    最近更新 更多