2

I just saw something very interesting in vk.com

Basically in profile page the right column is changing its width when the left column is gone of the view.

Example : http://vk.com/durov

Just scroll down a little bit to see it.

Ben
  • 1,728
  • 10
  • 28
  • 43

1 Answers1

3

Every element in the DOM defines certain spatial offset properties that can be accessed from JavaScript, them being offsetTop, offsetLeft etc. which give you the value how much offset it has from its parent. You can also access the parent in question with a while loop which simply assigns the next parent until none are left ( you can add their offsets to the variables of the original offset to gain the "true" (x,y) offset) and the while loop exists when there are no more parent elements.

Then you do some simple logic to check whether or not it is located within the viewport (hint - use the page offset and width/height properties of the window object). Is this enough to go on or do you need a code sample?

You could do some hacks if you know where exactly your element will appear and its height (width is really of no consequence here), but that's a brute force approach. Simply checking the page Y offset and height against the predetermined "maxY" of the element you're trying to get offscreen to display a certain div to the right (like in the example given).

ADDENDUM: (Even more to the point)

This checks whether ANY part of the element is visible in the viewport:

function isVisibleInViewport(elem)
{
    var y = elem.offsetTop;
    var height = elem.offsetHeight;

    while ( elem = elem.offsetParent )
        y += elem.offsetTop;

    var maxHeight = y + height;
    var isVisible = ( y < ( window.pageYOffset + window.innerHeight ) ) && ( maxHeight >= window.pageYOffset );
    return isVisible; 

}

I tried writing it with clarity in mind. You can shorten it further if you wish. Even expand it to include logic if you wish to move to the side (x-axis, offsetLeft variable). And changes to your logic ought to be minimal if you want to check whether it's completely within view ( can't see a reason though).

My example usage

The logic to govern when to call this function goes beyond the scope of this, I hope you have some method in mind. My example:

function check()
{
    var canvas = document.getElementById("webGlExperiments");
    if(isVisibleInViewport(canvas))
        document.getElementById("test").innerHTML = "It is!";
    else
        document.getElementById("test").innerHTML = "It is not!";
}

And then add an event listener to check for scrolls:

window.addEventListener("scroll", check, false);

For my example, you'll also need another element like a canvas with the id webGlExperiments, change it to something that suits you and your problem. This is the div in question I used in my test which you can adapt for yourself:

<div id="test" style="margin-top:100px;">Testing, testing!</div>
  • @EvenJohnson Give me a moment to conjure it up. –  Apr 06 '12 at 09:35
  • There you go, I've applied it to some experiments of mine. Hope it helps! It simply turns the inside HTML to... Well, you get the picture. For testing purposes you can force body `min-height` to 2000px; and give it a go with an element of your choosing. –  Apr 06 '12 at 10:00
  • I did't get it i mean i make a simle html page but nothing happend. And why using onClick() so lets say this is left column its never clicked right ? – Ben Apr 06 '12 at 10:13
  • And how can i click a div that is not in the viewport – Ben Apr 06 '12 at 10:18
  • It's just a simple example, what you want to use is an event listener which will modify your div which has some innerHTML or text the moment that you start scrolling. You might have a function that adds a div to the left which offers you to go back up (like the example). `window.addEventListener("scroll", check, false);` It takes three arguments, first two are important, the event type - scroll and the function you wish to use with it. Modify it to suit your problem. Simply add it in your script and it will invoke check on every scroll. –  Apr 06 '12 at 10:26
  • Yes all is working, just not in IE but thats not a problem. Thanks very much Domagoj – Ben Apr 06 '12 at 11:14