zoukankan      html  css  js  c++  java
  • 利用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> 

    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> 

    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

    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

    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

    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

    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

    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

    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. }); 

    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); 

    to:

    1. $("#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. }); 
  • 相关阅读:
    图片延迟加载
    Python开发【第一篇】:初识Python
    JavsScript+dom
    Django学习系列18:使用迁移创建生产数据库
    Django学习系列17:在模板中渲染待办事项
    Django学习系列16:处理完POST请求后重定向
    Django学习系列15:把POST请求中的数据存入数据库
    Django学习系列14:第一个数据库迁移
    Django学习系列13:Django ORM和第一个模型
    Django学习系列12:把Python变量传入模板中渲染
  • 原文地址:https://www.cnblogs.com/afarmer/p/2276013.html
Copyright © 2011-2022 走看看