
Originally Posted by
organiclemonade
I've been working on a rough template for wordpress. I'd like to include three javascript elements: an accordion menu, a smoothscroll effect, and a style switcher. I can get all of them to work, but not together. The main conflict, I think, is between my styleswitcher, which uses an href="#" tag in the links, and my smoothscroll, which uses an href="#header" and the like.
So, first question, Have I correctly identified the conflict?
Having decoded the smoothscroll script I can see that it creates several unnecessary global variables, despite being written as a static object.
Try this amended version:
Code:
var Scroller=
{
speed:10,
offsetParent:function(d)
{
offsetParent=d.offsetTop;
if(d.offsetParent)
{
while((d=d.offsetParent))
{
offsetParent+=d.offsetTop;
}
}
return offsetParent;
},
scrollTop:function()
{
var body=document.body;
var d=document.documentElement;
if(body&&body.scrollTop)
{
return body.scrollTop;
}
if(d&&d.scrollTop)
{
return d.scrollTop;
}
if(window.pageYOffset)
{
return window.pageYOffset;
}
return 0;
},
attachEvent:function(a,b,d)
{
if(a.addEventListener)
{
return a.addEventListener(b,d,false);
}
if(a.attachEvent)
{
return a.attachEvent("on"+b,d);
}
},
end:function(e)
{
if(window.event)
{
window.event.cancelBubble=true;
window.event.returnValue=false;
return;
}
if(e.preventDefault&&e.stopPropagation)
{
e.preventDefault();
e.stopPropagation();
}
},
scroll:function(d)
{
var i=window.innerHeight||document.documentElement.clientHeight;
var h=document.body.scrollHeight;
var a=Scroller.scrollTop();
if(d>a)
{
if(h-d>i)
{
a+=Math.ceil((d-a)/Scroller.speed);
}
else
{
a+=Math.ceil((d-a-(h-d))/Scroller.speed);
}
}
else
{
a=a+(d-a)/Scroller.speed;
}
window.scrollTo(0,a);
if(a==d||Scroller.offsetTop==a)
{
clearInterval(Scroller.interval);
}
Scroller.offsetTop=a;
},
init:function()
{
Scroller.attachEvent(window,"load",Scroller.load);
},
load:function()
{
var a=document.getElementsByTagName("a");
Scroller.end(this);
for(var i=0;i<a.length;i++)
{
var l=a[i];
var d=location.pathname;
if(l.href&&l.href.indexOf("#")!=-1&&(l.pathname==d||"/"+l.pathname==d))
{
Scroller.attachEvent(l,"click",Scroller.end);
l.onclick=function()
{
Scroller.end(this);
l=this.hash.substr(1);
a=document.getElementsByTagName("a");
for(i=0;i<a.length;i++)
{
if(a[i].name==l)
{
clearInterval(Scroller.interval);
Scroller.interval=setInterval("Scroller.scroll("+Scroller.offsetParent(a[i])+")",10);
}
}
}
}
}
}
};
Scroller.init();
If it still doesn't work with the other scripts, try SoftScroll instead.
Bookmarks