Coder Social home page Coder Social logo

customd / jquery-visible Goto Github PK

View Code? Open in Web Editor NEW
995.0 42.0 370.0 58 KB

A jquery plugin which allows us to quickly check if an element is within the browsers visual viewport regardless of the window scroll position

Home Page: https://www.customd.com/articles/13/checking-if-an-element-is-visible-on-screen-using-jquery

License: MIT License

JavaScript 46.92% HTML 53.08%

jquery-visible's Introduction

Element Onscreen Visibility

This is a jQuery plugin which allows us to quickly check if an element is within the browsers visual viewport, regardless of the scroll position. If a user can see this element, the function will return true.

Documentation

Basic visibility check

This basic check will return true if the entire element is visible to the user (within the visual viewport).

$('#element').visible();

If you'd like to check for ANY PART of the element, you can use the following:

$('#element').visible( true );

The plugin ignores the elements visibility by default. E.g., display:none, visibility: hidden, offsetWidth or offsetHeight is 0). To filter on css visibility, you can use the jQuery :visible selector:

$('#element:visible').visible();

Optionally, you can specify a second parameter to the .visible plugin, which will check whether the element is visible, as well as whether it's within the viewport too.

$('#element:visible').visible( false, true );

Optionally, you can add a third parameter to specify the direction to check for visibility. This can either be 'horizontal', 'vertical' or 'both'. Default is to 'both'.

$('#element').visible( false, false, 'horizontal' );

Demos

The Demos for this plugin live under the examples/ directory. Open them directly in your web browser, or view the following online examples:

See the blog article:

Limitations

Currently, this plugin will not check for visibility in nested scrollable areas, only on the main viewport (window object).

jquery-visible's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jquery-visible's Issues

demo doesn't work

Hi.

I downloaded the plugin and tried to run the demo.

When I click "detect visibility" it reports everything is offscreen, even the elements that are clearly onscreen.

Tested in chrome 26 and firefox 20 on linux.

jquery-visible/blob/master/jquery.visible.js seems broke

Hey!

Looking at https://github.com/teamdf/jquery-visible/blob/master/jquery.visible.js and comparing it to http://opensource.teamdf.com/visible/jquery.visible.js and https://github.com/teamdf/jquery-visible/blob/master/jquery.visible.min.js it seems the first file contains some errors.

Line https://github.com/teamdf/jquery-visible/blob/master/jquery.visible.js#L23 doesn't seem to work as expected. this.offsetWidth returns undefined resulting in this.offsetWidth * this. offsetHeight to result in NaN

!!NaN will always return false.

Bump npm version?

I had a problem with v1.2.0 which was resolved when I checked out master.

Non-JQuery version?

Hey there,

It would be fantastic if this plugin could be made to work without the jquery dependency. The plugin itself is only 3kb or so, it doesn't make sense to have to include the 40-50kb of jquery code just to use it.

Any thoughts?

Logic broken for tall elements and partial = true

Hi,

Where getBoundingClientRect is available, the current algorithm checks to see whether the top OR the bottom are visible to consider the element partially visible. However, for tall elements the top of the element can be above the viewport and the bottom of the element below. This causes the function to return "false" in spite of the element being visible.

The partial case can't be driven from top OR bottom being visible. Instead you need to check whether the top is above the bottom of the viewport AND the bottom is below the top of the viewport.

The same logic also needs to apply to left & right.

The legacy browser code is OK.

        if (typeof t.getBoundingClientRect === 'function'){

            // Use this native browser method, if available.
            var rec = t.getBoundingClientRect(),
                tViz = rec.top    >= 0 && rec.top    <  vpHeight,
                bViz = rec.bottom >  0 && rec.bottom <= vpHeight,
                lViz = rec.left   >= 0 && rec.left   <  vpWidth,
                rViz = rec.right  >  0 && rec.right  <= vpWidth,
                tBounds = rec.top    <  vpHeight,
                bBounds= rec.bottom >  0,
                lBounds = rec.left   <  vpWidth,
                rBounds = rec.right  >  0,
                vVisible   = partial ? tBounds && bBounds : tViz && bViz,
                hVisible   = partial ? lBounds && rBounds : lViz && rViz;

            if(direction === 'both')
                return clientSize && vVisible && hVisible;
            else if(direction === 'vertical')
                return clientSize && vVisible;
            else if(direction === 'horizontal')
                return clientSize && hVisible;
        }

List on npm?

Could you list this on npm (or just link me to it, if it already is) for those of us using browserify?

Troubles when using .hide() / .slideUp()

If I hide the element I want to track is visisble or not, .visible() will return true all the time.

I haven't found anything on the web, which could do this.

I am doing a infinite scroll page, and when I scroll to bottom, I wish to show a loading text, and when the content is then loaded, then hide the loading text.

Minified version problem

Hi.

I'm using this script on our site with our scripts compressor. I had to make small changes to make this script work. Problem is with self executing function. Right now it's:

(function(e){/*code here*/})(jQuery)

I suggest to change it to:

;(function(e){/*code here*/}(jQuery));

Our minifier put all script into single line and that's the problem.

King regards.

[enhancement] Add missing bower.json.

Hey, maintainer(s) of teamdf/jquery-visible!

We at VersionEye are working hard to keep up the quality of the bower's registry.

We just finished our initial analysis of the quality of the Bower.io registry:

7530 - registered packages, 224 of them doesnt exists anymore;

We analysed 7306 existing packages and 1070 of them don't have bower.json on the master branch ( that's where a Bower client pulls a data ).

Sadly, your library teamdf/jquery-visible is one of them.

Can you spare 15 minutes to help us to make Bower better?

Just add a new file bower.json and change attributes.

{
  "name": "teamdf/jquery-visible",
  "version": "1.0.0",
  "main": "path/to/main.css",
  "description": "please add it",
  "license": "Eclipse",
  "ignore": [
    ".jshintrc",
    "**/*.txt"
  ],
  "dependencies": {
    "<dependency_name>": "<semantic_version>",
    "<dependency_name>": "<Local_folder>",
    "<dependency_name>": "<package>"
  },
  "devDependencies": {
    "<test-framework-name>": "<version>"
  }
}

Read more about bower.json on the official spefication and nodejs semver library has great examples of proper versioning.

NB! Please validate your bower.json with jsonlint before commiting your updates.

Thank you!

Timo,
twitter: @versioneye
email: [email protected]
VersionEye - no more legacy software!

Demo not working

I just tried the demo. It says that it prints offscreen or onscreen when the check is run, but nothing is printed.

Is there an example for an request and load, scroll and resize-event

Is there an example for an request and load, scroll and resize-event?

I have no idea to start? I want say:
If div.class in the viewport and if this div.class not more in the viewport .... by the scroll and resize and load-Event.

But I want it vor each div with the class xyz.

Thanks very much!

request: Offset?

an offset would be nice! so that it triggers earlier!

great plugin tho!

Can you help me to detect visibility of whole element?

First, thank you for this plugin. But I'm beginner and I'm not so sure how to use it. I have page where user should show his skill and bars grow when the user loads that slide (impress.js). So this function works great when I switch between two distant slides, but when they are near each other, one part of the skill page is "visible" and bars load their full size early.
Here is the code that I used for my bars:

var win = jQuery(window);
var allMods = jQuery(".skill");

allMods.each(function(i, el) {
  if (jQuery(el).visible(true)) {                                           
    jQuery("span.bar.line1").animate({ width: w1 }, 3000, function() { });
   } 
});

I hardly make this work:) But can you help to make it TRUE only if it's whole div visible?

Visible on the specific part on the window

Is it possible to set only the specific part on the window to be visible true?
I want my element to be on the bottom of the window but still not be visible, until you scroll to the center of the widow...

Error with direction being already defined

Just checked the code with JSHint and it displayed the following warning..

23  'direction' is already defined.

You might also want to consider passing window to the closure for efficiency improvements.

Add onVisible callback

This would be pretty convenient if you had some callbacks for when an element is visible or not visible. Would make it easier to implement all sorts of cool things.

Meant to work with multiple matching elements?

From what I can tell, this only works with the first matched element in the DOM. Am I just using it incorrectly, or are multiple matching elements not supported? If the latter, is this a planned feature?

Minor inaccuracy

Looking at the code I noticed that in the case where we are checking for partial visibility it uses <= and >= which is technically incorrect because if the top edge of the element lines up exactly with the bottom edge of the container then it would be inaccurate to consider it visible. It's the sort of thing that rarely comes up but it's also the sort of thing that makes things like this possible:

http://www.youtube.com/watch?v=W1U5RUVENNE

Documentation issue with partial check of any element

In the documentation it says:

If you'd like to check for ANY PART of the element, you can use the following:

$('#element').list( true );

that however gives me the following error:

TypeError: $(...).list is not a function

I'm assuming that to check partially for the element it should just be:

$('#element').visible( true );

which works fine?

jquery.visible vs jquery-visible

We had a bower entry for "jquery.visible": "1.1.0" which used to point to this repository. Now it seems to be pointing to this other repository which clearly doesn't do the same behavior.

The fix was pretty easy, change "jquery.visible": "1.1.0" to "jquery-visible": "1.1.0", however we want to see if this is a result of a hijacked package, bower inferring dots (.) as dashes (-), or something else.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.