Loading image when scrollbar moves

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deegeorge
    New Member
    • Nov 2008
    • 58

    Loading image when scrollbar moves

    Hi,
    I have 500 images.when clicking on one link it need to load this much images.Its loading
    fine.My requirement is load the image only in the viewable area.when scrolling the scrollbar next
    viewable area need to be loaded what i need to do


    Pls help me
    Its urgent


    Thanks in advance
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    so you have 500 images in one page? i would suggest a redesign to have some paging mechanism that is a bit more better to handle then. otherwise you would need to connect the scrolling-event to a load mechanism in a way that you react to the scrolloffset and calculate which images has to be loaded then. this is quite easy when all images have the same size and would be a bit more complicated when the (probably height) differs.

    kind regards

    Comment

    • Canabeez
      New Member
      • Jul 2009
      • 126

      #3
      Here, try this, it should lead you to the right way.

      Not sure how you can manage the src's, because you can't store them in the image and keep the image unloaded at the same time (Well, maybe you could store them as ALT text or a TITLE), but you decide.

      But as gits said, if I were you, I'd consider redesign ;)

      Good luck

      [code=javascript]
      function getPosition(Obj ect)
      {
      if('undefined' != typeof(Object.o ffsetParent))
      {
      for(var posX=0,posY=0;O bject;Object=Ob ject.offsetPare nt)
      {
      posX += Object.offsetLe ft;
      posY += Object.offsetTo p;
      }
      return [posX,posY];
      }
      else
      {
      return [Object.x,Object .y];
      }
      }

      window.onscroll = function()
      {
      var scrollTop = document.body.s crollTop;
      var innerHeight = window.innerHei ght;

      var images = document.getEle mentsByName("my Image");

      for(var i=0;i<images.le ngth;i++)
      {
      var imageHeight = getPosition(ima ges[i])[1];
      if(imageHeight > scrollTop && imageHeight < (scrollTop + innerHeight))
      {
      images[i].src = images_src[i];
      }
      }
      }

      var images_src = [];

      for(var i=0;i<200;i++)
      {
      images_src[i] = 'http://www.google.com/intl/en_ALL/images/logo.gif';
      var img = document.create Element("img");
      img.width = 276;
      img.height = 110;
      img.name = "myImage";
      document.body.a ppendChild(img) ;
      document.body.a ppendChild(docu ment.createElem ent("br"));
      }
      [/code]

      Comment

      Working...