How to scroll infinity with pure javascript?

Asked

Viewed 3,827 times

4

I’m trying to implement an infinite scroll on my site, like this one, but I’m having trouble making the script that picks up the mouse scrolling event. I tried using this script (below) that I picked up on a website, but it’s not exactly what I need. Does anyone have any suggestions?

cont=0
function handle(delta) {
    var nav = document.getElementById('navegacao');
    var ln = nav.getElementsByTagName('a');
    if (delta < 0){
        cont=cont+1;
        ln[cont].click();
    }
    else{
        cont=cont-1;
        ln[cont].click();
    }
}

function wheel(event){
    var delta = 0;
    if (!event) event = window.event;
    if (event.wheelDelta) {
        delta = event.wheelDelta/120; 
    } else if (event.detail) {
        delta = -event.detail/3;
    }
    if (delta)
        handle(delta);
        if (event.preventDefault)
                event.preventDefault();
        event.returnValue = false;
}

/* Initialization code. */
if (window.addEventListener)
    window.addEventListener('DOMMouseScroll', wheel, false);
    window.onmousewheel = document.onmousewheel = wheel;
  • What does this code do that you don’t like? What exactly do you need to do?

  • So @bfavaretto I need that when I scroll the mouse move to the next element of my page like this: http://wandaprint.com/home. but this code (which I posted and was the only one I found), jumps two in two Oo elements

  • That site there does not pass in Pagespeed: his preloader is close to 20% here already almost a minute ago.

  • hahahaha @Gustavorodrigues It really is a very heavy site, it has many images but it is a well done site (the design part at least rs)

  • I still hope that the Pagespeed popularize, even more here in Brazil where the speeds are not so high. I hope he finishes charging so I can comment on him.

  • Page uploaded! This page doesn’t need a preloader for the images so much. Organize the elements first then load the images as the user navigates through the images.

Show 1 more comment

3 answers

2


Dommousescroll only works on Firefox, you also have to use onmousescroll to work on other browsers. I made an example of paging with scroll like the link you posted in jsFiddle: http://jsfiddle.net/c6PPv/3/

  • did not work in Chrome.

  • Exactly, I tested here and nothing happened @Pauloroberto

  • @Pauloroberto got something http://jsbin.com/kozukizi/2/edit?html,css,js,output

  • still not what you wanted @Odair?

  • Yes, that’s exactly it, but I wanted to make the call in my javascript and not in the div as I did, you know if you have this possibility @Pauloroberto?

  • what do you mean by this @odair ? I could not understand, can explain better?

  • Sorry @Pauloroberto, I’m starting programming now hehehe so in case I made the call of my function in div <divclass="box" id="window-home" onMouseWheel="Picture()"></div>, can I call her in the script? something like window.onMouseWheel...

  • see my reply @Odair

  • 1

    I updated the link in the reply, working on Chrome.

  • Thank you very much @Wynn :)

Show 5 more comments

1

In a generic way to make an infinite scroll page is sort of like this, but in case what you want is to do like a parallax.

Just like in the example of infinite scroll what you can do is prevent successive events from being processed, so use a timeout:

var scrolling;
function wheel () {
  if (scrolling) {return;}

  scrolling = true;
  setTimeout(function () {
    scrolling = false;
  }, 200 /* o tempo para animar a página */);

  /* o resto da função */
}

1

With the code you developed here that would be:

var count=0;
function Picture(){
    if (event.wheelDelta >= 4){
        if(count == 3){
            count=-1;
        }else{
            Resize(++count);
        }
    }
    else if (event.wheelDelta <= 4)
        if(count == 0){
            count=4;
        }else{
            Resize(--count);
        }
    return false;
}
function Resize(c){    
    var nav = document.getElementById('navegacao');
    var btn = nav.getElementsByTagName('li');
    var ln = nav.getElementsByTagName('a');
    ln[c].click();
}

Using the direct call on <div> of your html:

<divclass="box" id="janela-home" onMouseWheel="Picture()"></div>

You would like to call in js for it to run in window, so you should remove this event onMouseWheel html and use the following code:

var mousewheelevt=(/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel" //Firefox não reconhece o mouseWheel na versão FF3.x

if (document.attachEvent) //para IE (e Opera dependendo das configurações do usuário)
    document.attachEvent("on"+mousewheelevt, function(e){Picture()})
else if (document.addEventListener) //WC3 browsers
    document.addEventListener(mousewheelevt, function(e){Picture()}, false);

Reference

  • 1

    Thank you @Pauloroberto! It worked right!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.