/    Sign up×
Community /Pin to ProfileBookmark

Keep the image position with respect to scroll bar movement

Hi All,

Can any one help me out to solve an issue?

I am having a lengthy HTML page. So I decided to put an image icon (With Go to Top Link) at the Right-Bottom of page.

This image should remain that position relatively even if user move the scroll bar down.

So that at any position of page you can go to top of the page by clicking the image.

Please help me out to do this in JavaScript. If you point to some piece of code will be appriciated.

Thanks,
naga

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@pj59Dec 14.2004 — Hello!

Here is an example:[code=php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Snippet</title>
<script language="JavaScript" type="text/javascript">
<!--
var orgTop;
function init(){
orgTop=document.getElementById('scrImg').offsetTop;
window.onscroll=scrFunc;
}
function scrFunc(){
document.getElementById('scrImg').style.top=document.body.scrollTop+orgTop;
}
//-->
</script>
</head>
<body onload="init()">
<div style="height: 2500px">
<a name="toTop"></a>
<div style="height: 200px">blah 1</div>
<div style="height: 200px">blah 2</div>
<div style="height: 200px">blah 3</div>
<div style="height: 200px">blah 4</div>
<div style="height: 200px">blah 5</div>
<div style="height: 200px">blah 6</div>
<div style="height: 200px">blah 7</div>
<div style="height: 200px">blah 8</div>
<div style="height: 200px">blah 9</div>
<div style="height: 200px">blah 10</div>
<div style="height: 200px">blah 11</div>
<div style="height: 200px">blah 12</div>
<div style="height: 200px">blah 13</div>
</div>
<div id="scrImg" style="position:absolute;right: 20px;bottom:20px">
<img src="yourImage.gif" alt="&nbsp;go to top&nbsp;" border=0 onclick="window.scrollTo(0,0);scrFunc()" style="cursor: pointer">
</div>
</body>
</html>[/code]
Please note: in Mozilla, the onscroll event will not fire if the user scrolls by using the mouse wheel. If you want the code to work in such a case as well, the function scrFunc() should call itself with a setTimeout or setInterval instead of being called onscroll.

Regards PJ
Copy linkTweet thisAlerts:
@naga_citauthorDec 15.2004 — Thanks a lot!!!
Copy linkTweet thisAlerts:
@pj59Dec 15.2004 — You are welcome! :p
×

Success!

Help @naga_cit 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.16,
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,
)...