Skip to content

Instantly share code, notes, and snippets.

@Kuzcoo
Forked from 140bytes/LICENSE.txt
Last active February 13, 2017 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Kuzcoo/679180c82493e7fad148 to your computer and use it in GitHub Desktop.
Save Kuzcoo/679180c82493e7fad148 to your computer and use it in GitHub Desktop.
140byt.es -- Click ↑↑ fork ↑↑ to play!

140infinite

Tweet-sized helper for infinite scroll.

How to

infinite(container)(function () {
		// your handler which triggers when
		// you reach the end of the container
	});

140byt.es

A tweet-sized, fork-to-play, community-curated collection of JavaScript.

How to play

  1. Click the Fork button above to fork this gist.
  2. Modify all the files to according to the rules below.
  3. Save your entry and tweet it up!

Keep in mind that thanks to the awesome sensibilities of the GitHub team, gists are just repos. So feel free to clone yours and work locally for a more comfortable environment, and to allow commit messages.

Rules

All entries must exist in an index.js file, whose contents are

  1. an assignable, valid Javascript expression that
  2. contains no more than 140 bytes, and
  3. does not leak to the global scope.

All entries must also be licensed under the WTFPL or equally permissive license.

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

function (c) { // take an html element
return function (f) { // f is the handler to attach
return function () {
// if bottom < window height trigger handler
c.getBoundingClientRect().bottom<window.innerHeight?f():'';
};
}
};
}
// 98 bytes
function(c){return function(f){return function(){c.getBoundingClientRect().bottom<window.innerHeight?f():""}}}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2015 Thomas Gérard <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "140infinite",
"description": "Some kind of infinite scrolling oriented lib",
"keywords": [
"five",
"descriptive",
"keywords",
"or",
"fewer"
]
}
<!DOCTYPE html>
<title>140infinite</title>
<style>
.container {
display: flex;
flex-wrap: wrap;
}
.img-container {
flex: 1 0 300px;
max-height: 300px;
padding: 2px 4px;
opacity: 0;
overflow: hidden;
transition: opacity 0.5s ease-in-out;
}
</style>
<body>
<div class='container'>
</div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
// my lib
var infinite = function(c){return function(f){return function(){c.getBoundingClientRect().bottom<window.innerHeight?f():""}}};
// for appending images to the end of the block
var loadImages = function (container) {
var div = document.createElement('div');
div.classList.add('img-container');
div.innerHTML =
'<img alt="kikoo" src="http://lorempixel.com/400/300/city?v='+Date.now()+'">';
div.querySelector('img')
.addEventListener('load', function show() {
setTimeout(function() {div.style.opacity = 1;}
, Math.random() * 100);
this.removeEventListener('load', show);
});
container.appendChild(div);
};
var container = document
.querySelector('.container');
// preload some images
for (var i = 0; i < 20; i++) { loadImages(container); }
window.onscroll = infinite(container)(function () {
for (var i = 0; i < 5; i++) { loadImages(container); }
});
</script>
</body>
@atk
Copy link

atk commented Apr 22, 2016

why not infinite(container, function)? Then you could use

function(c,f){return function(){c.getBoundingClientRect().bottom<window.innerHeight&&f()}}

and save a few bytes (by the way, x&&y() is smaller than x?y():'' or even x?y():0).

You'd even have enough characters to add the event inside your function

function(c,f){addEventListener('scroll',function(){c.getBoundingClientRect().bottom<window.innerHeight&&f()})}

@Kuzcoo
Copy link
Author

Kuzcoo commented Jun 21, 2016

Yes that's far better! Add the event inside is great. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment