Most Visible v1.4.0
Returns the most visible element from a set of elements.
A JavaScript (and jQuery) plugin which returns the most visible element from a given set.
Installation
Download
- most-visible.min.js (1.35kB, 687B gzipped)
- most-visible.js (4.91kB, 1.48kB gzipped)
CDN
<script src="https://unpkg.com/[email protected]/dist/most-visible.min.js"></script> <!-- OR --> <script src="https://unpkg.com/[email protected]/dist/most-visible.js"></script>
Package Managers
Install via yarn:
$ yarn add most-visible
Install via NPM:
$ npm install most-visible --save
Usage
jQuery
The plugin will automatically be added to any global (window) version of jQuery:
$('.my-elements').mostVisible().addClass('most-visible'); // with options $('.my-elements').mostVisible({ percentage: true, offset: 160 }).addClass('most-visible');
Vanilla JavaScript
You can pass in either a selector string:
var element = mostVisible('.elements');
Or a NodeList:
var element = mostVisible(document.querySelectorAll('.elements'), { percentage: true });
Or you can create a new instance:
var instance = new mostVisible('.elements'); instance.getMostVisible();
Webpack and Browserify
var mostVisible = require('most-visible'); mostVisible('.elements');
To attach the jQuery plugin to a non-global version of jQuery you must call .makeJQueryPlugin
:
var $ = require('jquery'); var mostVisible = require('most-visible'); mostVisible.makeJQueryPlugin($); $('.my-elements').removeClass('active').mostVisible().addClass('active');
Options
Option | Type | Description | Default |
---|---|---|---|
percentage | boolean | Whether to calculate the visibility of an element as a percentage of its height | false |
offset | number | A pixel offset to use when calculating visibility. Useful for e.g fixed headers | 0 |
Modify the mostVisible.defaults
object to change default option values:
mostVisible.defaults.percentage = true; mostVisible.defaults.offset = 160;
License
The MIT License (MIT) Copyright (c) 2019 The Most Visible authors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.