8

I isolated the most I can my code into this : http://jsfiddle.net/uXVWu/

HTML :

<a id="stopHere" >Hi !</a>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />

JS :

var theAnchor=document.getElementById("stopHere");
var tempX,tempY;

function stopScroll(e)
{
    var c=parseInt(document.getElementById('stopHere').offsetWidth);
    var d=parseInt(document.getElementById('stopHere').offsetHeight);
    if(tempX<=c&&tempY<=300+d&&0<=tempX&&300<=tempY)window.scrollTo(0,200);
}

function update(e)
{
    var doc=document.documentElement;
    var body=document.body;
    tempX=(e.pageX)?e.pageX:e.clientX+(doc&&doc.scrollLeft||body&&body.scrollLeft||0)-(doc&&doc.clientLeft||body&&body.clientLeft||0);
    tempY=(e.pageY)?e.pageY:e.clientY+(doc&&doc.scrollTop||body&&body.scrollTop||0)-(doc&&doc.clientTop||body&&body.clientTop||0);
}

window.onscroll=stopScroll;
window.onmousemove=update;

CSS :

#stopHere
{
    float:left;
    width:100%;
    border:1px solid black;
    text-align:center;
}

What does the program is that if the cursor is on the <a>, then you scroll, the <a> will be on the top. What I want is that you scroll over it and it goes on the top. How can it works?

user1365010
  • 3,185
  • 8
  • 24
  • 43

3 Answers3

2

Do you mean something like this: http://jsfiddle.net/GTSaz/1/

So that if, when you are scrolling, your mouse is over the element the scrolling will jump down to have the element at the top of the viewport (0,300 in this case).

Also, if you don't want to hardcode the position (0,300) then you can detect in in the script, see http://www.quirksmode.org/js/findpos.html

Adam Heath
  • 4,703
  • 2
  • 35
  • 50
2

If I'm understanding correctly, you could set it up at top of the page simpler using something like:

document.getElementById('stopHere').style.position="fixed"
document.getElementById('stopHere').style.top="0"
NoBugs
  • 9,310
  • 13
  • 80
  • 146
0

I am not really sure what do you want . Do you want a fixed div or window.scroll(0,0)

But, I have combined your's and Adam's code, to workaround the situation.

http://jsfiddle.net/c3CJF/1/

Markup:

<a id="stopHere" >Hi !</a>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />

CSS:

#stopHere
{
    position:absolute;
    top:300px;
    left:0px;
    width:100%;
    border:1px solid black;
    text-align:center;
}

Javascript:

var theAnchor=document.getElementById("stopHere");
var tempX,tempY;
var docBody = document.body,
    docEl = document.documentElement,
    scrollElement = /WebKit/.test(navigator.userAgent) || document.compatMode == 'BackCompat' ? docBody : docEl;
var lastScrollTop = scrollElement.scrollTop;
function stopScroll(e)
{
    // Extra check to make if your scroll passes the element on its movement
    if((scrollElement.scrollTop - lastScrollTop + tempY) >= 300){
       window.scrollTo(0,300);
    }
    lastScrollTop = scrollElement.scrollTop;
    var c=parseInt(document.getElementById('stopHere').offsetWidth);
    var d=parseInt(document.getElementById('stopHere').offsetHeight);
    /* the if is to test if the mouse is over the anchor */
    if(tempX<=c&&tempY<=300+d&&0<=tempX&&300<=tempY)window.scrollTo(0,300);
}

function update(e)//to have the x and y of the mouse
{
    var doc=document.documentElement;
    var body=document.body;
    tempX=(e.pageX)?e.pageX:e.clientX+(doc&&doc.scrollLeft||body&&body.scrollLeft||0)-(doc&&doc.clientLeft||body&&body.clientLeft||0);
    tempY=(e.pageY)?e.pageY:e.clientY+(doc&&doc.scrollTop||body&&body.scrollTop||0)-(doc&&doc.clientTop||body&&body.clientTop||0);

}

window.onscroll=stopScroll;
window.onmousemove=update;
Jashwant
  • 28,410
  • 16
  • 70
  • 105