Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether an element is fully visible and then stop scrolling

graphic illustrating issue

I´m using FullSlider.js to create a full-slide-webpage. In case the red element is fully visible, I need the browser to block the scroll event (means: window is not moving, but I´m able to receive the action) and than after I did some stuff I want to enable scrolling again.

That is what I did so far:

I read a lot of stuff about this and tried even more solutions like: stop scrolling: 1. stop scrolling of webpage with jquery does not work at all

  1. How to programmatically disable page scrolling with jQuery does stop the scrolling, but it is not possible to enable scrolling again

  2. event prevent default, works fine in chrome, but less fine in firefox

check if element is visible:

Check if element is visible after scrolling

I used the solution above and tried:

  1. to check if the red element is visible (did not work)
  2. checked if a tiny span above the red element is visible (did not work well)
  3. checked if a tiny span below the red element is visible (did not work well)
  4. checked if a tiny span above and below the element is visible (did not work at all)

  5. tried some idea about getting the scrollTop of the red element and check if the bodys scrollTop is equal or near

In fact the 2. solution did work quiet well, but I just was not able to figure out the offset I needed to at to compensate the "fixed-header navigation".

Currently I´m using the "isScrolledIntoView" to detect whether the position fits (works well on large screens, does not work at all on small screens). For the stop scrolling, I´m using the following hack:

CSS: .scrollHack { position: static; overflow: hidden; }

JS:

  $(document).on('mousewheel', function(event, delta) {
      // stopScroll and isStopped are booleans delivered by another script
      if(isScrolledIntoView($("#s3")))
      {
        isStopped = false; 
        if(delta >= 0) {
         $('#s3').get(0).contentWindow.car.next(); //  car.next();
        }
        else{
         $('#s3').get(0).contentWindow.car.previous(); 
        }    
        stopScroll = $('#s3').get(0).contentWindow.isStopped; 
      }
     if(!stopScroll && isScrolledIntoView($("#s3")))
     {
      event.preventDefault(); 
      $("body").addClass("scrollHack");
     }
     else
     {
          $("body").removeClass("scrollHack"); 
     }

});

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

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

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

Was someone faced with an familiar situation and has some scripts or hacks that would help me out?

like image 300
Qullbrune Avatar asked Jan 13 '14 16:01

Qullbrune


People also ask

How do you check if an element is visible after scrolling?

Use the getBoundingClientRect() method to get the size of the element and its relative position to the viewport. Compare the position of the element with the viewport height and width to check if the element is visible in the viewport or not.

How do you know if an element is completely visible?

To know whether the element is fully visible in viewport, you will need to check whether top >= 0, and bottom is less than the screen height. In a similar way you can also check for partial visibility, top is less than screen height and bottom >= 0.

Is DOM element visible?

Method 1: Checking the height, width and bounding rectangle of the element: The element can be checked whether it is visible by checking the height, width, and dimensions of the bounding rectangle of the element.

How do you check if a user has scrolled to the bottom?

If you want to check whether the user has scrolled to the bottom of the page, you can use the scroll() jQuery event. The given code piece takes the top scroll of the window, so how much the page is scrolled down, it adds the height of the visible window and checks if it is equivalent to the height of the document.


1 Answers

Sorry, I can't answer in a comment yet. So this is only a partial answer, regarding the second part of your question.

For checking whether your element is fully in view:

  • Try using jQuery innerHeight() instead of height(). This gives you the element height without margins and borders.

  • Checking for a tiny span above the red element should not be necessary. Wouldn't that be equal to just checking whether the top of the red element is on the screen? You could do that like this:

    function isScrolledIntoView(elem) {        
        var docViewTop = $(window).scrollTop();
        var elemTop = $(elem).offset().top;
        return (elemTop >= docViewTop));
    }
    

    But this is only checking whether the top of your element is visible!

  • You say that your current solution does not work well on small screens. Could it be that the element you are checking is taller than the viewport on small screens?

Maybe this helps a little. Else it would be really good to see an example page or a jsfiddle example.

like image 109
pjupju Avatar answered Sep 27 '22 15:09

pjupju