Related Posts Slide Out Boxes with jQuery and CSS3

The other day we were wondering how we could show our visitors more of our works. It’s normal that in a blog older posts get forgotten and even if something […]

The other day we were wondering how we could show our visitors more of our works. It’s normal that in a blog older posts get forgotten and even if something might still be very interesting and relevant, it get’s lost under the pile of new stuff. So we decided to create something like a little widget that can be used to show related posts in any page. The main idea is to show a fixed list at the right side of the screen with some thumbnails sticking out. When the user hovers over the items, they slide out, revealing the title and two links, one for the related article itself and one for the demo. Additionally, we will have a shuffle button under the list. When pressed, the list gets randomly refreshed with related posts.

Before we use this, we of course, want to share it with you 🙂

So, let’s get started!

The Markup

The HTML structure is going to be a list with some nested elements inside: the main div with the thumbnail, the span for the title and the span with the two links:

<div id="rp_list" class="rp_list">
	<ul>
		<li>
			<div>
				<img src="images/1.jpg" alt=""/>
				<span class="rp_title">Post Title</span>
				<span class="rp_links">
					<a target="_blank" href="#">Article</a>
					<a target="_blank" href="#">Demo</a>
				</span>
			</div>
		</li>
		...
	</ul>
	<span id="rp_shuffle" class="rp_shuffle"></span>
</div>

All related posts are going to be listed in our structure. In the JavaScript we will define that we only show 5 posts at a time. Of course, you need to replace the # with your links.
The shuffle span will be positioned after the list.

Let’s take a look at the style.

The CSS

We will start by the general style attributes:

.rp_list {
	font-family:Verdana, Helvetica, sans-serif;
	position:fixed;
	right:-220px;
	top:40px;
	margin:0;
	padding:0;
}

As you can see, the list will have a fixed position, always staying in the same place when the user scrolls the page. We are going to set the right to a negative value, hiding the whole list.

Next, let’s define the shuffle span style:

span.rp_shuffle{
	background:#222 url(../images/shuffle.png) no-repeat 10px 50%;
	width:28px;
	height:14px;
	display:block;
	margin:10px 0px 0px 20px;
	cursor:pointer;
	padding:4px;
	border:1px solid #000;
	-moz-border-radius:5px 0px 0px 5px;
	-webkit-border-bottom-left-radius: 5px;
	-webkit-border-top-left-radius: 5px;
	border-bottom-left-radius: 5px;
	border-top-left-radius: 5px;
}

The list is going to have the following style:

.rp_list ul{
	margin:0;
	padding:0;
	list-style:none;
}

The list items will not be shown initially:

.rp_list ul li{
	width: 240px;
	margin-bottom:5px;
	display:none;
}

Our main elements will have the following style:

.rp_list ul li div{
	display: block;
	line-height:15px;
	width: 240px;
	height: 80px;
	background:#333;
	border:1px solid #000;
	-moz-border-radius:5px 0px 0px 5px;
	-webkit-border-bottom-left-radius: 5px;
	-webkit-border-top-left-radius: 5px;
	border-bottom-left-radius: 5px;
	border-top-left-radius: 5px;
}

The thumbnail will be of the size 70×70 pixel and we will add a box shadow to it:

.rp_list ul li div img{
	width:70px;
	border:none;
	float:left;
	margin:4px 10px 0px 4px;
	border:1px solid #111;
	-moz-box-shadow:1px 1px 3px #000;
	-webkit-box-shadow:1px 1px 3px #000;
	box-shadow:1px 1px 3px #000;
}

The title span is going to have a inset shadow:

span.rp_title{
	font-size:11px;
	color:#ddd;
	height:46px;
	margin:4px 0px 0px 20px;
	display:block;
	text-shadow:1px 1px 1px #000;
	padding-top:3px;
	background:#222;
	-moz-box-shadow:0px 0px 5px #000 inset;
	-webkit-box-shadow:0px 0px 5px #000 inset;
	box-shadow:0px 0px 5px #000 inset;
}

The span for the links and the buttons will have the following style:

span.rp_links{
	width:195px;
	height:8px;
	padding-top:2px;
	display:block;
	margin-left:42px;
}
span.rp_links a{
	background: #222 url(../images/bgbutton.png) repeat-x;
	padding: 2px 18px;
	font-size:10px;
	color: #fff;
	text-decoration: none;
	line-height: 1;
	-moz-box-shadow: 0 1px 3px #000;
	-webkit-box-shadow: 0 1px 3px #000;
	box-shadow:0 1px 3px #000;
	text-shadow: 0 -1px 1px #222;
	cursor: pointer;
	outline:none;
}
span.rp_links a:hover{
	background-color:#000;
	color:#fff;
}

And that’s all the style. Let’s add some magic!

The JavaScript

The main idea is to first show 5 items with their whole width and to quickly slide them in until one can only see the thumbnail. That should happen when we load the page. It is a neat effect to show the user that there is something going on and that he can interact with this element.

When we hover over an item, we will make it slide out until its full width, revealing the title and the links.

The shuffle function will make 5 posts appear randomly. As you noticed, we added all our related posts to the HTML structure and from those items, we will randomly choose 5. This method does not guarantee us that the next set of items shown are not repeated, but it’s a simple solution.

We will add the following jQuery function:

$(function() {
	/**
	* the list of posts
	*/
	var $list 		= $('#rp_list ul');
	/**
	* number of related posts
	*/
	var elems_cnt 		= $list.children().length;

	/**
	* show the first set of posts.
	* 200 is the initial left margin for the list elements
	*/
	load(200);

	function load(initial){
		$list.find('li').hide().andSelf().find('div').css('margin-left',-initial+'px');
		var loaded	= 0;
		//show 5 random posts from all the ones in the list.
		//Make sure not to repeat
		while(loaded < 5){
			var r 		= Math.floor(Math.random()*elems_cnt);
			var $elem	= $list.find('li:nth-child('+ (r+1) +')');
			if($elem.is(':visible'))
				continue;
			else
				$elem.show();
			++loaded;
		}
		//animate them
		var d = 200;
		$list.find('li:visible div').each(function(){
			$(this).stop().animate({
				'marginLeft':'-50px'
			},d += 100);
		});
	}

	/**
	* hovering over the list elements makes them slide out
	*/
	$list.find('li:visible').live('mouseenter',function () {
		$(this).find('div').stop().animate({
			'marginLeft':'-220px'
		},200);
	}).live('mouseleave',function () {
		$(this).find('div').stop().animate({
			'marginLeft':'-50px'
		},200);
	});

	/**
	* when clicking the shuffle button,
	* show 5 random posts
	*/
	$('#rp_shuffle').unbind('click')
					.bind('click',shuffle)
					.stop()
					.animate({'margin-left':'-18px'},700);

	function shuffle(){
		$list.find('li:visible div').stop().animate({
			'marginLeft':'60px'
		},200,function(){
			load(-60);
		});
	}
});

And that’s all!

We hope you enjoyed this tutorial and found it useful!

Tagged with:

Manoela Ilic

Manoela is the main tinkerer at Codrops. With a background in coding and passion for all things design, she creates web experiments and keeps frontend professionals informed about the latest trends.

Stay up to date with the latest web design and development news and relevant updates from Codrops.