Skip to content

Beacon Listener - Create one or more listeners to watch for nodes with specified selectors entering either the viewport or a specified region

License

Notifications You must be signed in to change notification settings

Perturbatio/beacon-listener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

beacon-listener

Beacon Listener - Create listeners to detect when nodes with the specified selector(s) enter either the viewport or a specified region

Basic usage:

(function(){
	
	//listen for all elements with a class of 'beacon' in the viewport
	var myListener = new BeaconListener({
		beacons: '.beacon'
	});
	
	myListener.addEventListener('beacon-listener:found', function(e){
		e.detail.beacon.show(true);
	});
	
	myListener.addEventListener('beacon-listener:lost', function(e){
		e.detail.beacon.hide(true);
	});
	
})();

Advanced usage:

(function(){
	
		// listen for all elements with a class of 'beacon'
		// but only if they are fully inside the region defined by #my-region
		var myListener = new BeaconListener({
			beacons: '.beacon',
			region: '#my-region',
			fullyInside: true,
			pollInterval: 200
		});
	
		myListener.addEventListener('beacon-listener:found', function(e){
			e.detail.beacon.show(true);
		});
	
		myListener.addEventListener('beacon-listener:lost', function(e){
			e.detail.beacon.hide(true);
		});
})();

Lazy loading images:

(function(){
	var imageListener = new BeaconListener({
		beacons     : 'img[data-src]',
		fullyInside : true,
		pollInterval: 1000 / 60,
		autoStart: true
	});
	
	imageListener.addEventListener('beacon-listener:found', function(e){
		var img  = e.detail.beacon,
			newSrc = img.getAttribute('data-src');
		
		if (newSrc && newSrc.length > 0){
			img.src = newSrc;
			img.removeAttribute('data-src');
		}
	});
})();

About

Beacon Listener - Create one or more listeners to watch for nodes with specified selectors entering either the viewport or a specified region

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published