利用jQuery UI制作包含文本内容的slider

转自:
Create Featured Content Slider Using jQuery UI
http://webdeveloperplus.com/jquery/featured-content-slider-using-jquery-ui/

demo http://demo.webdeveloperplus.com/featured-content-slider/

slider

Showing off the best content of your website or blog in a nice intuitive way will surely catch more eyeballs. Using an auto-playing content slider is the one of techniques to show your featured content. It saves you space and makes for a better user experience, and if you add a pinch of eye candy to it, then there’s no looking back.

There are a few tutorials on creating featured content sliders like the one from CSS-Tricks, but it uses jQuery Coda Slider plugin. Today I’m going to show you how to create a featured content slider for your website using the jQuery UI library.

Let’s start with it..

Add JavaScript Files

First of all, grab the jQuery and jQuery UI libraries, if you haven’t already and include them in your page header. For this tutorial, you’ll need jQuery UI version 1.5.3. I usually use Google AJAX libraries to load jQuery and jQuery UI files as it acts as a CDN for your JavaScript files.

  1. <code><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" ></script> 
  2. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.5.3/jquery-ui.min.js" ></script> 
  3. </code> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" ></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.5.3/jquery-ui.min.js" ></script>

The Featured Content Structure

Now create a div with its contents as the tabs structured as a list and content corresponding to each tab within a separate div.

  1. <div id="featured" > 
  2.     <ul class="ui-tabs-nav"> 
  3.         <li class="ui-tabs-nav-item ui-tabs-selected" id="nav-fragment-1"><a href="#fragment-1"><img src="images/image1-small.jpg" alt="" /><span>15+ Excellent High Speed Photographs</span></a></li> 
  4.         <li class="ui-tabs-nav-item" id="nav-fragment-2"><a href="#fragment-2"><img src="images/image2-small.jpg" alt="" /><span>20 Beautiful Long Exposure Photographs</span></a></li> 
  5.         <li class="ui-tabs-nav-item" id="nav-fragment-3"><a href="#fragment-3"><img src="images/image3-small.jpg" alt="" /><span>35 Amazing Logo Designs</span></a></li> 
  6.         <li class="ui-tabs-nav-item" id="nav-fragment-4"><a href="#fragment-4"><img src="images/image4-small.jpg" alt="" /><span>Create a Vintage Photograph in Photoshop</span></a></li> 
  7.     </ul> 
  8.     <!-- First Content --> 
  9.     <div id="fragment-1" class="ui-tabs-panel" style=""> 
  10.         <img src="images/image1.jpg" alt="" /> 
  11.         <div class="info" > 
  12.         <h2><a href="#" >15+ Excellent High Speed Photographs</a></h2> 
  13.         <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla tincidunt condimentum lacus. Pellentesque ut diam....<a href="#" >read more</a></p> 
  14.         </div> 
  15.     </div> 
  16.     <!-- Second Content --> 
  17.     <div id="fragment-2" class="ui-tabs-panel ui-tabs-hide" style=""> 
  18.         <img src="images/image2.jpg" alt="" /> 
  19.         <div class="info" > 
  20.         <h2><a href="#" >20 Beautiful Long Exposure Photographs</a></h2> 
  21.         <p>Vestibulum leo quam, accumsan nec porttitor a, euismod ac tortor. Sed ipsum lorem, sagittis non egestas id, suscipit....<a href="#" >read more</a></p> 
  22.         </div> 
  23.     </div> 
  24.     <!-- Third Content --> 
  25.     <div id="fragment-3" class="ui-tabs-panel ui-tabs-hide" style=""> 
  26.         <img src="images/image3.jpg" alt="" /> 
  27.         <div class="info" > 
  28.         <h2><a href="#" >35 Amazing Logo Designs</a></h2> 
  29.         <p>liquam erat volutpat. Proin id volutpat nisi. Nulla facilisi. Curabitur facilisis sollicitudin ornare....<a href="#" >read more</a></p> 
  30.         </div> 
  31.     </div> 
  32.     <!-- Fourth Content --> 
  33.     <div id="fragment-4" class="ui-tabs-panel ui-tabs-hide" style=""> 
  34.         <img src="images/image4.jpg" alt="" /> 
  35.         <div class="info" > 
  36.         <h2><a href="#" >Create a Vintage Photograph in Photoshop</a></h2> 
  37.         <p>Quisque sed orci ut lacus viverra interdum ornare sed est. Donec porta, erat eu pretium luctus, leo augue sodales....<a href="#" >read more</a></p> 
  38.         </div> 
  39.     </div> 
  40. </div> 
<div id="featured" >
	<ul class="ui-tabs-nav">
	    <li class="ui-tabs-nav-item ui-tabs-selected" id="nav-fragment-1"><a href="#fragment-1"><img src="images/image1-small.jpg" alt="" /><span>15+ Excellent High Speed Photographs</span></a></li>
	    <li class="ui-tabs-nav-item" id="nav-fragment-2"><a href="#fragment-2"><img src="images/image2-small.jpg" alt="" /><span>20 Beautiful Long Exposure Photographs</span></a></li>
	    <li class="ui-tabs-nav-item" id="nav-fragment-3"><a href="#fragment-3"><img src="images/image3-small.jpg" alt="" /><span>35 Amazing Logo Designs</span></a></li>
	    <li class="ui-tabs-nav-item" id="nav-fragment-4"><a href="#fragment-4"><img src="images/image4-small.jpg" alt="" /><span>Create a Vintage Photograph in Photoshop</span></a></li>
	</ul>
	<!-- First Content -->
	<div id="fragment-1" class="ui-tabs-panel" style="">
		<img src="images/image1.jpg" alt="" />
		<div class="info" >
		<h2><a href="#" >15+ Excellent High Speed Photographs</a></h2>
		<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla tincidunt condimentum lacus. Pellentesque ut diam....<a href="#" >read more</a></p>
		</div>
	</div>
	<!-- Second Content -->
	<div id="fragment-2" class="ui-tabs-panel ui-tabs-hide" style="">
		<img src="images/image2.jpg" alt="" />
		<div class="info" >
		<h2><a href="#" >20 Beautiful Long Exposure Photographs</a></h2>
		<p>Vestibulum leo quam, accumsan nec porttitor a, euismod ac tortor. Sed ipsum lorem, sagittis non egestas id, suscipit....<a href="#" >read more</a></p>
		</div>
	</div>
    <!-- Third Content -->
    <div id="fragment-3" class="ui-tabs-panel ui-tabs-hide" style="">
		<img src="images/image3.jpg" alt="" />
		<div class="info" >
		<h2><a href="#" >35 Amazing Logo Designs</a></h2>
		<p>liquam erat volutpat. Proin id volutpat nisi. Nulla facilisi. Curabitur facilisis sollicitudin ornare....<a href="#" >read more</a></p>
	    </div>
	</div>
    <!-- Fourth Content -->
    <div id="fragment-4" class="ui-tabs-panel ui-tabs-hide" style="">
		<img src="images/image4.jpg" alt="" />
		<div class="info" >
		<h2><a href="#" >Create a Vintage Photograph in Photoshop</a></h2>
		<p>Quisque sed orci ut lacus viverra interdum ornare sed est. Donec porta, erat eu pretium luctus, leo augue sodales....<a href="#" >read more</a></p>
	    </div>
	</div>
</div>

The class names ui-tabs-selected and ui-tabs-hide should not be changed, you can change other class names to your own.

The CSS Styles

Now for the CSS part, I fixed the width and height of the outer container div with id #featured and added a right padding of 250px to make space for the absolutely positioned navigation tabs for our featured content slider. Also, i set its position attribute to relative so that i can absolutely position elements inside #featured div relative to it.

  1. #featured{ 
  2.     width:400px
  3.     padding-right:250px
  4.     position:relative
  5.     height:250px
  6.     background:#fff
  7.     border:5px solid #ccc
#featured{
	width:400px;
	padding-right:250px;
	position:relative;
	height:250px;
	background:#fff;
	border:5px solid #ccc;
}

The navigation tabs are absolutely positioned to the right with following styles:

  1. #featured ul.ui-tabs-nav{ 
  2.     position:absolute
  3.     top:0; left:400px
  4.     list-style:none
  5.     padding:0; margin:0; 
  6.     width:250px
  7. #featured ul.ui-tabs-nav li{ 
  8.     padding:1px 0; padding-left:13px
  9.     font-size:12px
  10.     color:#666
  11. #featured ul.ui-tabs-nav li span{ 
  12.     font-size:11px; font-family:Verdana
  13.     line-height:18px
#featured ul.ui-tabs-nav{
	position:absolute;
	top:0; left:400px;
	list-style:none;
	padding:0; margin:0;
	width:250px;
}
#featured ul.ui-tabs-nav li{
	padding:1px 0; padding-left:13px;
	font-size:12px;
	color:#666;
}
#featured ul.ui-tabs-nav li span{
	font-size:11px; font-family:Verdana;
	line-height:18px;
}

The content panels are given following styles so as to fit them inside featured div container. The ui-tabs-hide class is essential to working of this script as it decides which content panels are hidden and which is displayed.

  1. #featured .ui-tabs-panel{ 
  2.     width:400px; height:250px
  3.     background:#999; position:relative
  4.         overflow:hidden
  5. #featured .ui-tabs-hide
  6.     display:none
#featured .ui-tabs-panel{
	width:400px; height:250px;
	background:#999; position:relative;
        overflow:hidden;
}
#featured .ui-tabs-hide{
	display:none;
}

And the selected tab is given a background image with a left arrow. Here are the styles for selected tab.

  1. #featured li.ui-tabs-nav-item a{/*On Hover Style*/ 
  2.     display:block
  3.     height:60px
  4.     color:#333background:#fff
  5.     line-height:20px
  6.     outline:none
  7. #featured li.ui-tabs-nav-item a:hover{ 
  8.     background:#f2f2f2
  9. #featured li.ui-tabs-selected{ /*Selected tab style*/ 
  10.     background:url('images/selected-item.gif') top left no-repeat
  11. #featured ul.ui-tabs-nav li.ui-tabs-selected a{ 
  12.     background:#ccc
#featured li.ui-tabs-nav-item a{/*On Hover Style*/
	display:block;
	height:60px;
	color:#333;  background:#fff;
	line-height:20px;
	outline:none;
}
#featured li.ui-tabs-nav-item a:hover{
	background:#f2f2f2;
}
#featured li.ui-tabs-selected{ /*Selected tab style*/
	background:url('images/selected-item.gif') top left no-repeat;
}
#featured ul.ui-tabs-nav li.ui-tabs-selected a{
	background:#ccc;
}

Since i used small thumbnail images in the navigation tabs, i applied following styles to them.

  1. #featured ul.ui-tabs-nav li img{ 
  2.     float:left; margin:2px 5px
  3.     background:#fff
  4.     padding:2px
  5.     border:1px solid #eee
#featured ul.ui-tabs-nav li img{
	float:left; margin:2px 5px;
	background:#fff;
	padding:2px;
	border:1px solid #eee;
}

Also, in the content panel which is displayed, it has one image of size 400px x 250px and some relavent title and description inside the div with class info. To display .info div over the image i absolutely positioned it over the image with a transparent background to add some eye-candy.

  1. #featured .ui-tabs-panel .info{ 
  2.     position:absolute
  3.     top:180px; left:0; 
  4.     height:70px; width: 400px
  5.     background: url('images/transparent-bg.png'); 
  6. #featured .info h2{ 
  7.     font-size:18px; font-family:Georgia, serif
  8.     color:#fff; padding:5px; margin:0; 
  9.     overflow:hidden
  10. #featured .info p{ 
  11.     margin:0 5px
  12.     font-family:Verdana; font-size:11px
  13.     line-height:15px; color:#f0f0f0
  14. #featured .info a{ 
  15.     text-decoration:none
  16.     color:#fff
  17. #featured .info a:hover{ 
  18.     text-decoration:underline
#featured .ui-tabs-panel .info{
	position:absolute;
	top:180px; left:0;
	height:70px; width: 400px;
	background: url('images/transparent-bg.png');
}
#featured .info h2{
	font-size:18px; font-family:Georgia, serif;
	color:#fff; padding:5px; margin:0;
	overflow:hidden;
}
#featured .info p{
	margin:0 5px;
	font-family:Verdana; font-size:11px;
	line-height:15px; color:#f0f0f0;
}
#featured .info a{
	text-decoration:none;
	color:#fff;
}
#featured .info a:hover{
	text-decoration:underline;
}

Note: All the required styles are within style.css file.

The JavaScript Code

Finally the JavaScript code, that’ll make our featured content slider work. I’m using rotating tabs feature of jQuery UI library that makes the content panels rotate automatically after given time interval.

  1. $(document).ready(function(){ 
  2.     $("#featured > ul").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true); 
  3. }); 
	$(document).ready(function(){
		$("#featured > ul").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true);
	});

And there you are with a nice looking featured content slider.

View Working Demo or Download the source code and try it.

Update(Aug 20, 2008)

How To Use With Latest version of jQuery UI

If you are using the latest jQuery UI version aleady on your website, then do not use the earlier version I am using above, simply change the code as below(Thanks Deni Sri Supriyono for commenting about this):

Just change this line from the JavaScript code:

  1. $("#featured > ul").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true); 
$("#featured > ul").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true);

to:

  1. $("#featured").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true); 
$("#featured").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true);

Pausing Slideshow on Hover

Here is a nice solution from Vernon(in comments below) for those who were looking to enable pause on hover for the current slide. Use this piece of JavaScript code instead of above.

  1. $(document).ready(function(){ 
  2. $("#featured").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true); 
  3. $("#featured").hover( 
  4. function() { 
  5. $("#featured").tabs("rotate",0,true); 
  6. }, 
  7. function() { 
  8. $("#featured").tabs("rotate",5000,true); 
  9. ); 
  10. }); 
$(document).ready(function(){
$("#featured").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true);
$("#featured").hover(
function() {
$("#featured").tabs("rotate",0,true);
},
function() {
$("#featured").tabs("rotate",5000,true);
}
);
});

转载于:https://www.cnblogs.com/afarmer/archive/2011/12/04/2276013.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值