1

In a horizontal listView - is it possible to know whether a certain fragment is "outside" the screen?

silicakes
  • 4,490
  • 2
  • 23
  • 33

1 Answers1

3

Because WinJS is essentially running in IE 10, you can approach this as you would a regular web issue. Some of this is borrowed from Check if element is visible after scrolling

Using JQuery, you can do:

function isScrolledIntoView(elem)
{
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom)
      && (elemBottom <= docViewBottom) &&  (elemTop >= docViewTop) );
}

You can also do it in pure "WinJS" (Internet Explorer 10 + JavaScript), it would look something like:

function isScrolledIntoView(elem)
{
    var docViewTop = window.pageXOffset;
    var docViewBottom = docViewTop + window.innerHeight;

    var elemTop = =  document.getElementById(elem).offsetTop
    var elemBottom = elemTop + =  document.getElementById(elem).offsetHeight

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom)
      && (elemBottom <= docViewBottom) &&  (elemTop >= docViewTop) );
}
Community
  • 1
  • 1
techsaint
  • 722
  • 9
  • 21
  • 1
    Thanks man, I've changed it to fit a horizontal view rather than a vertical one: function isScrolledIntoView(elem) { var docViewLeft = $(window).scrollLeft(); var docViewRight = docViewLeft + $(window).width(); var elemLeft = $(elem).offset().left; var elemRight = elemLeft + $(elem).width(); return ((elemRight >= docViewLeft) && (elemLeft <= docViewRight) && (elemRight <= docViewRight) && (elemLeft >= docViewLeft)); } Cheers! – silicakes Jun 20 '12 at 15:38
  • @Mike86 Thanks for the code update! Glad to see things working for you! – techsaint Jun 20 '12 at 17:33