Example 1:
I want to make sure you read the 3rd paragraph that I am referring to now, but which you cannot see because it is lower down the page
So ... I create a keyword by emphasising the word paragraph, in bold, and make it a link.
When you click the link the page scrolls down to bring the paragraph into view, the background of the paragraph flashes, and the page scrolls back up.
Example 2:
I want to point out a specific word in that paragraph
The target is simply:
<span id="target1">Highlighted Text</span>
Each separate caller and individual target must have its own unique identity, but you can call the same target with more than one caller.
IE:
Caller1 - Target1, Caller2 - Target1
<script type="text/javascript">
<!--
// Realised by apacheJeff
// www.huntingground.freeserve.co.uk
flashme=""
step=5
pause=3000
flashrate=250
function hlite_Init(){ // get page height and set top and bottom limit positions, prevent over-run
Top_Limit=10
Bottom_Limit=document.body.scrollHeight-(document.body.clientHeight+Top_Limit)
}
function whichway(callerID,targetID,dir,col){
if(dir=="down"){
CallerID=callerID
TargetID=targetID
Dir=dir
Col=col
scrolldown(CallerID,TargetID,Dir,Col)
}
else{
CallerID=targetID
TargetID=callerID
Dir=dir
Col=col
scrollup(CallerID,TargetID,Dir,Col)
}
}
function scrolldown(callerID,targetID,dir,col){ //scroll down
while(document.body.scrollTop >-5&&document.body.scrollTop<target_pos){ // scroll to target element
window.scrollBy(0,step)
}
if(dir=="down"){
Dir=dir
CallerID=callerID
TargetID=targetID
setTimeout("document.getElementById(TargetID).style.backgroundColor='';clearInterval(flashme); scrollup(CallerID,TargetID,Dir)",pause)
}
}
function scrollup(callerID,targetID,dir,col){ //scroll up
caller_pos=document.getElementById(callerID).offsetTop-(document.body.clientHeight/2)
target_pos=document.getElementById(targetID).offsetTop-(document.body.clientHeight/2)
if(caller_pos<5){caller_pos=Top_Limit} // prevent scrolling past top of page
if(target_pos>document.body.scrollHeight-document.body.clientHeight){target_pos=Bottom_Limit} // stop at page bottom
clearInterval(flashme)
flash(callerID,col)
while(document.body.scrollTop >= caller_pos+5){
window.scrollBy(0,-step)
}
if(dir=="up"){
Dir=dir
CallerID=callerID
TargetID=targetID
setTimeout("document.getElementById(callerID).style.backgroundColor='';clearInterval(flashme); scrolldown(CallerID,TargetID,Dir)",pause)
}
}
function flash(whichID,col){
if(document.getElementById(whichID).style.backgroundColor==""){
document.getElementById(whichID).style.backgroundColor=col
}
else{
document.getElementById(whichID).style.backgroundColor=""
}
WhichID=whichID
Col=col
flashme=setTimeout("flash(WhichID,Col)",flashrate)
}
// add onload="hlite_Init()" to the opening BODY tag
// -->
</script>
<a id="caller1" href="#null" onclick="whichway('caller1','target1','down','gold')">Keyword</a>
<span id="target1">Highlighted text</span>
<a id="caller2" href="#null" onclick="whichway('caller2','target1','up','blue')">Keyword</a>
Note:
In order to test the script yourself, once you have copied the above into your page you need to make your page long enough to scroll. To do this add about 20 line breaks <BR> between caller1, target1, and caller2.
The script ended up longer than anticipated because I had to include some preventative measures to stop the browser locking up.
This happened because I am using the while statement, and through experimental mishaps (positioning the caller and/or target at the extreme top or bottom of the page) I found that if the scroll went over the upper and lower limits of the page the while loop would not terminate, hence a lockup.
Function Init and two if statements in each function prevent this from happening.
If you use this script and should have the misfortune to experience a lockup just increase the variable Top_Limit value