/    Sign up×
Community /Pin to ProfileBookmark

tracking pageYOffset – there must be a better way.

I’m rather new to writing javascripts, am working on a means to scroll a div containing a small navig menu from an initial, absolutely positioned, location on a page. I’ve got it to work – though the scrolling is a little choppy – probably a lag due to excessive firing of the function. I’m pretty sure the Setimeout() is not the the proper way to implement this, anyone know what is?

[CODE]function shiftY(){
var yVal = getScrollY(); // getScroll() returns the pageYOffset

if (document.getElementById){
if (yVal > 335){
document.getElementById(“databoxLNav”).style.position = “fixed”;
document.getElementById(“databoxLNav”).style.top = “12px”;
}
else if (yVal <= 335 && document.getElementById(“databoxLNav”).style.position == “fixed”){
document.getElementById(“databoxLNav”).style.position = “absolute”;
document.getElementById(“databoxLNav”).style.top = “325px”;
}
}
setTimeout(“shiftY()”, 100)
}[/CODE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@mityaJul 10.2008 — A couple of points:

1) The first part of the second condition is redundent (yVal <= 355), i.e. not needed

2) One problem could be that you're effectively telling a function to hit itself. this can cause runtime issues. Time fucntions should be called from outside the function they are calling

3) depending on exactly what you're trying to achieve, sure you're not confusing setTimeOut with setInterval?
Copy linkTweet thisAlerts:
@stride-rauthorJul 10.2008 — Regarding points:

1) yeah, i see that now (yVal <= 355)

2) Thanks!

3) Probably! :o
×

Success!

Help @stride-r spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...