cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
                <div class=giar>
    <h1>Loading...</h1>
    <div class=votes>
      <span class=down>0</span><span class=up>0</span>
    </div>
    <div class="reading-list">
      <h2>Reading List</h2>
      <ul>
        <li>Items will be added here</li>
      </ul>
    </div>
    <a href="#" class="clear-list">clear</a>
  </div>

            
          
!
            
               .giar {
	position: relative;
	width: 40%;
	margin: 0 auto;
	text-align: center;
	font-family: Helvetica Neue, Helvetica, sans-serif;
	color: #466;
 }
 .giar h1 {
	min-height: 1em;
 }
 .giar .votes {
	position: relative;

	margin: 2em auto;
	font-size: 2em;
 }
 .giar .votes span {
	border: 1px solid #eee;
	cursor: pointer;
	display: inline-block;
	padding: .5em;
 }
 .giar .votes span:hover {
	background: #eee;
 }
 .giar .votes .down {
	margin-right: .3em;
 }
 .giar .votes .down:before {
	content: "๐Ÿ‘Ž ";
 }
 .giar .votes .up {
	margin-left: .3em;
 }
 .giar .votes .up:before {
	content: "๐Ÿ‘ ";
 }
 .reading-list ul{
	list-style: none;
	padding: 0;
 }
 .reading-list ul li{
	padding-bottom: .5em;
	margin-bottom: .5em;
	border-bottom: 1px solid #eee;
 }
 a{
	text-decoration: none;
	color: #69c;
 }
 a:hover{
	color: #699;
 }
 .clear-list{
	color: #930;
 }
 .clear-list:hover{
	color: #600;
 }
            
          
!
            
              var posts_mockup = [
	{ ID: 1, title: "First Post Title", permalink: 'http://example.com/1/', upvotes: 12, downvotes: 2 },
	{ ID: 22, title: "Second Post Title", permalink: 'http://example.com/2/', upvotes: 1, downvotes: 22 },
	{ ID: 33, title: "Third Post Title", permalink: 'http://example.com/3/', upvotes: 5, downvotes: 4 },
	{ ID: 44, title: "Fourth Post Title", permalink: 'http://example.com/4/', upvotes: 2, downvotes: 2 },
	{ ID: 55, title: "Fifth Post Title", permalink: 'http://example.com/5/', upvotes: 8, downvotes: 13 },
];

var $el;
var posts = {};
var votedOn = JSON.parse( localStorage.getItem( 'giar_votedOn' ) );

function getPostsFromServer( callback ) {
	// TODO: AJAX get posts from server
	
	posts = posts_mockup;
	if ( 'function' === typeof callback ) {
		callback.call();
	}
}
function getRandomPost() {
	var post = posts[ Math.floor( Math.random() * posts.length ) ];
	var current = $el.data( 'post' );
	if ( 'object' == typeof current && current.ID === post.ID ) {
		post = getRandomPost();
	}
	return post;
}
function showPost( post ) {
	$el.data( 'currentPost', post );
	$el.find( 'h1' ).text( post.title );
	$el.find( '.votes .up' ).text( post.upvotes );
	$el.find( '.votes .down' ).text( post.downvotes );
}

function getLS( key ) {
	var data = window.localStorage.getItem( key );
	try {
		return JSON.parse( data );
	} catch ( e ) {
		return data;
	}
}

function setLS( key, data ) {
	return window.localStorage.setItem( key, JSON.stringify( data ) );
}

function voteOnPost( post, updown ) {
	var readingList = getLS( 'readingList' ) || [];
	if ( 'up' === updown ) {
		if ( -1 === readingList.indexOf( post.ID ) ) {
			readingList.push ( post.ID );
			setLS( 'readingList', readingList );
			addReadingListElem( post );
		}
	}
	showPost( getRandomPost() );

	// TODO: AJAX send vote to server

}

function addReadingListElem( post ) {
	$el.find( '.reading-list ul' ).append( '<li data-id="' + post.ID + '"><a target=_blank href="' + post.permalink + '">' + post.title + '</a></li>' );
}

$( document ).ready( function() {
	$el = $( '.giar' );

	getPostsFromServer( initAfterAjax );

	function initAfterAjax() {
		showPost( getRandomPost() );

		// display saved reading list
		$el.find( '.reading-list ul' ).empty();
		var readingList = getLS( 'readingList' ) || [];
		$.each( readingList, function( i, ID ) {
			$.each( posts, function( i, post ) {
				if ( post.ID === ID ) {
					addReadingListElem( post );
				}
			} );
		} );
	}

	$el.on( 'click', '.votes span', function( e ) {
		voteOnPost( $el.data( 'currentPost' ), $( this ).attr( 'class' ) );
	} );

	$el.on( 'click', '.clear-list', function( e ) {
		e.preventDefault();
		setLS( 'readingList', [] );
		$el.find( '.reading-list ul' ).empty();
	} );

} );


            
          
!
999px
Loading ..................

Console