Skip to content

Instantly share code, notes, and snippets.

@cgspicer
Created February 11, 2013 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgspicer/4756550 to your computer and use it in GitHub Desktop.
Save cgspicer/4756550 to your computer and use it in GitHub Desktop.
A CodePen by Coran Spicer. focalPoint.js for jQuery - A jQuery plugin that stores an array of elements and adds a class to the element that is center-most in the viewport. Useful for drawing user attention to the center-most article in a loop, for instance.
<div class="container">
<article class="post first">
<header>
<h3>Some Post</h3>
</header>
<section>
<p>Some sort of content and ipsum. Bacon ipsum dolor sit amet doner short loin andouille ball tip tongue pastrami. Corned beef spare ribs shoulder pastrami doner bresaola sausage ribeye beef frankfurter meatball sirloin cow flank pig. Salami strip steak sirloin drumstick ribeye meatloaf. Cow turducken flank shoulder t-bone hamburger. T-bone short loin pastrami brisket beef ribs ball tip. Pork loin turducken jowl, short ribs tail corned beef andouille pork chop jerky.</p>
</section>
</article>
<article class="post second">
<header>
<h3>Some Post</h3>
</header>
<section>
<p>Some sort of content and ipsum. Bacon ipsum dolor sit amet doner short loin andouille ball tip tongue pastrami. Corned beef spare ribs shoulder pastrami doner bresaola sausage ribeye beef frankfurter meatball sirloin cow flank pig. Salami strip steak sirloin drumstick ribeye meatloaf. Cow turducken flank shoulder t-bone hamburger. T-bone short loin pastrami brisket beef ribs ball tip. Pork loin turducken jowl, short ribs tail corned beef andouille pork chop jerky.</p>
</section>
</article>
<article class="post third">
<header>
<h3>Some Post</h3>
</header>
<section>
<p>Some sort of content and ipsum. Bacon ipsum dolor sit amet doner short loin andouille ball tip tongue pastrami. Corned beef spare ribs shoulder pastrami doner bresaola sausage ribeye beef frankfurter meatball sirloin cow flank pig. Salami strip steak sirloin drumstick ribeye meatloaf. Cow turducken flank shoulder t-bone hamburger. T-bone short loin pastrami brisket beef ribs ball tip. Pork loin turducken jowl, short ribs tail corned beef andouille pork chop jerky.</p>
</section>
</article>
<article class="post fourth">
<header>
<h3>Some Post</h3>
</header>
<section>
<p>Some sort of content and ipsum. Bacon ipsum dolor sit amet doner short loin andouille ball tip tongue pastrami. Corned beef spare ribs shoulder pastrami doner bresaola sausage ribeye beef frankfurter meatball sirloin cow flank pig. Salami strip steak sirloin drumstick ribeye meatloaf. Cow turducken flank shoulder t-bone hamburger. T-bone short loin pastrami brisket beef ribs ball tip. Pork loin turducken jowl, short ribs tail corned beef andouille pork chop jerky.</p>
</section>
</article>
<article class="post fourth">
<header>
<h3>Some Post</h3>
</header>
<section>
<p>Some sort of content and ipsum. Bacon ipsum dolor sit amet doner short loin andouille ball tip tongue pastrami. Corned beef spare ribs shoulder pastrami doner bresaola sausage ribeye beef frankfurter meatball sirloin cow flank pig. Salami strip steak sirloin drumstick ribeye meatloaf. Cow turducken flank shoulder t-bone hamburger. T-bone short loin pastrami brisket beef ribs ball tip. Pork loin turducken jowl, short ribs tail corned beef andouille pork chop jerky.</p>
</section>
</article>
<article class="post fourth">
<header>
<h3>Some Post</h3>
</header>
<section>
<p>Some sort of content and ipsum. Bacon ipsum dolor sit amet doner short loin andouille ball tip tongue pastrami. Corned beef spare ribs shoulder pastrami doner bresaola sausage ribeye beef frankfurter meatball sirloin cow flank pig. Salami strip steak sirloin drumstick ribeye meatloaf. Cow turducken flank shoulder t-bone hamburger. T-bone short loin pastrami brisket beef ribs ball tip. Pork loin turducken jowl, short ribs tail corned beef andouille pork chop jerky.</p>
</section>
</article>
<article class="post fourth">
<header>
<h3>Some Post</h3>
</header>
<section>
<p>Some sort of content and ipsum. Bacon ipsum dolor sit amet doner short loin andouille ball tip tongue pastrami. Corned beef spare ribs shoulder pastrami doner bresaola sausage ribeye beef frankfurter meatball sirloin cow flank pig. Salami strip steak sirloin drumstick ribeye meatloaf. Cow turducken flank shoulder t-bone hamburger. T-bone short loin pastrami brisket beef ribs ball tip. Pork loin turducken jowl, short ribs tail corned beef andouille pork chop jerky.</p>
</section>
</article>
<article class="post fourth">
<header>
<h3>Some Post</h3>
</header>
<section>
<p>Some sort of content and ipsum. Bacon ipsum dolor sit amet doner short loin andouille ball tip tongue pastrami. Corned beef spare ribs shoulder pastrami doner bresaola sausage ribeye beef frankfurter meatball sirloin cow flank pig. Salami strip steak sirloin drumstick ribeye meatloaf. Cow turducken flank shoulder t-bone hamburger. T-bone short loin pastrami brisket beef ribs ball tip. Pork loin turducken jowl, short ribs tail corned beef andouille pork chop jerky.</p>
</section>
</article>
</div>
(function( $ ){
/**
* focalCenter.js
* A jQuery plugin that takes an array of elements and assigns a class to the element nearest the viewport's center
* http://www.codekaiju.com
*
* Copyright 2013, Coran Spicer
* Free to use under the MIT license.
*
* Date: Mon Feb 11 2013
*/
var methods = {
init : function( options ) {
var settings = $.extend( {
'direction' : 'vertical',
'classToAssign' : 'focal-center',
'relativeContainer' : 'window' //unsupported stub
}, options);
$this = $(this);
var $container = settings.relativeContainer == 'window' ? $(window) : $this.closest( $( settings.relativeContainer ) );
var minWidth = $container.scrollLeft();
var maxWidth = $container.width();
var centerX = (maxWidth + minWidth) / 2;
var minHeight = $container.scrollTop();
var maxHeight = $container.height();
var centerY = (maxHeight + minHeight)/2;
var focalPoints = [];
$this.each( function(target) {
self = this;
self.fromCenterX = Math.abs( centerX - ( $(self).offset().left + $(self).width() / 2 ) + minWidth / 2 );
self.fromCenterY = Math.abs( centerY - ( $(self).offset().top + $(self).height() / 2 ) + minHeight / 2 );
focalPoints.push(self);
});
function assignClasses() {
for ( var f = 0; f < focalPoints.length; f++ ) {
$( focalPoints[f] ).removeClass( settings.classToAssign );
}
var lastFocalPoint = focalPoints.length - 1;
$( focalPoints[ lastFocalPoint ] ).addClass( settings.classToAssign );
}
function getDistances() {
minWidth = $container.scrollLeft();
maxWidth = $container.width();
centerX = (maxWidth + minWidth) / 2;
minHeight = $container.scrollTop();
maxHeight = $container.height();
centerY = (maxHeight + minHeight) / 2;
for ( var i = 0; i < focalPoints.length; i++ ) {
self = focalPoints[i];
self.fromCenterX = Math.abs( centerX - ( $(self).offset().left + $(self).width() / 2 ) + minWidth / 2 );
self.fromCenterY = Math.abs( centerY - ( $(self).offset().top + $(self).height() / 2 ) + minHeight / 2 );
}
}
function sortFocalPoints(){
if ( settings.direction == 'horizontal' ) {
focalPoints = focalPoints.sort(function(a, b) {
return b.fromCenterY - a.fromCenterY;
});
} else if ( settings.direction == 'vertical' ) {
focalPoints = focalPoints.sort(function(a, b) {
return b.fromCenterY - a.fromCenterY;
});
} else {
$.error( 'unable to sort against: ' + settings.direction );
}
}
/* do the work */
$(window).scroll( function() {
getDistances();
sortFocalPoints();
assignClasses();
});
getDistances();
sortFocalPoints();
assignClasses();
}
};
$.fn.focalCenter = function( method ) {
// Method calling logic
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.focalCenter' );
}
};
})( jQuery );
$(document).ready( function(){
$('.post').focalCenter();
});
body { padding-top: 100px; }
article.post {
margin: 60px 10px;
-webkit-transition: background 1s;
-moz-transition: background 1s;
transition: background 1s;
}
article.post header h3 { font-weight:bold; text-indent: 10px; }
article.post section p { padding: 20px; }
article.post.focal-center { background: lightgray; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment