0

I have in my site some divs
And I want that when the user scorll up a litlle the page will scorll and the next div will be shown
For example if I have 2 divs:

 <div style="height:800px;">some content </div>
 <div style="height:800px;">some content </div>

So when the page loaded the first div is shown, Now I want that if the user scorll a litlle bit to the next div the all page will scroll up and the next div will be display

Harman met
  • 241
  • 2
  • 3
  • 10

3 Answers3

0

Capture the scroll event and when the user scroll jump to your id.

window.onscroll= function(){
    window.location.href=widow.location + '#<desired_id>';
}

or:

If you have fixed divs:

window.onscroll= function(){
    window.scrollTo(x,y);
}

How to tell the current scroll posotion:

window.onscroll = function() {
    var top = pageYOffset || 0;
    console.log('top:' + top)
}
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
0

Using jQuery

Taken from Check if element is visible after scrolling

You can use this function to detect if the user has "scorll a litlle bit to the next div" and then show the div you are aiming to show:

<div id="div1" style="height:800px;">some content</div>
<div id="div2" style="height:800px;">some content</div>

// checks if the element passed in is in view
function isScrolledIntoView(elem)
{
    var $elem = $(elem);
    var $window = $(window);

    var docViewTop = $window.scrollTop();
    var docViewBottom = docViewTop + $window.height();

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

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

// get the element to check e.g. the "next div" in your case
var el = document.getElementById('div2');

// when the user scrolls, check if the element has come into view
window.onscroll = function() {
    if (isScrolledIntoView(el)) {
        // take the user to the top of the div by it's id
        $('html, body').animate({ 
            scrollTop: $('#div2').offset().top 
        }, 'slow');
        return false;
    }
}
Community
  • 1
  • 1
cnorthfield
  • 3,384
  • 15
  • 22
0
<div  id="div1" style="height:800px;">some content </div>
<div id="div2" style="height:800px;">some content </div>

**Considering the Above two divs**



var mousewheelevt = (/Firefox/i.test(navigator.userAgent)) ?"DOMMouseScroll" : "mousewheel" 
    //FF doesn't recognize mousewheel as of FF3.x
        $('#div1').bind(mousewheelevt, function(e){

            var evt = window.event || e //equalize event object     
            evt = evt.originalEvent ? evt.originalEvent : evt; //convert to originalEvent if possible               
            var delta = evt.detail ? evt.detail*(-40) : evt.wheelDelta //check for detail first, because it is used by Opera and FF

            if(delta > 0) {
                //scroll up 

                $('#div1').hide();
                $('#div2').show();
            }
            else{
                //scroll down

                $('#div2').hide();
                $('#div1').show();
            }   
        });
Shantanu Madane
  • 617
  • 5
  • 14