jQuery滚动插件2 jCarousel

  jCarousel

 

jCarousel是为了控制一个垂直水平列表控件的一个jQuery插件。

使用也挺方便的。

 Simple  carousel

Getting started

To use the jCarousel component, include the jQuery library, the jCarousel source file, the jCarousel core stylesheet file and a jCarousel skin stylesheet file inside the <head> tag of your HTML document:

<script type="text/javascript" src="/path/to/jquery-1.2.1.pack.js"></script>
<script type="text/javascript" src="/path/to/lib/jquery.jcarousel.pack.js"></script>
<link rel="stylesheet" type="text/css" href="/path/to/lib/jquery.jcarousel.css" />
<link rel="stylesheet" type="text/css" href="/path/to/skin/skin.css" />

The download package contains some example skin packages. Feel free to build your own skins based on it.

jCarousel expects a very basic HTML markup structure inside your HTML document:

<ul id="mycarousel" class="jcarousel-skin-name">
   <!-- The content goes in here -->
</ul>

jCarousel automatically wraps the required HTML markup around the list. The class attribute applies the jCarousel skin "name" to the carousel.

To setup jCarousel, add the following code inside the <head> tag of your HTML document:

<script type="text/javascript">
jQuery(document).ready(function() {
    jQuery('#mycarousel').jcarousel({
        // Configuration goes here
    });
});
</script>

jCarousel accepts a lot of configuration options, see chapter "Configuration" for further informations.

After jCarousel has been initialised, the fully created markup in the DOM is:

<div class="jcarousel-skin-name">
  <div class="jcarousel-container">
    <div disabled="disabled" class="jcarousel-prev jcarousel-prev-disabled"></div>
    <div class="jcarousel-next"></div>
    <div class="jcarousel-clip">
      <ul class="jcarousel-list">
        <li class="jcarousel-item-1">First item</li>
        <li class="jcarousel-item-2">Second item</li>
      </ul>
    </div>
  </div>
</div>

As you can see, there are some elements added which have assigned classes (in addition to the classes you may have already assigned manually). Feel free to design your carousel with the classes you can see above.

Note:
  • The skin class "jcarousel-skin-name" has been moved from the <ul> to the top <div> element.
  • The first nested <div> under <div class="jcarousel-container"> illustrates a disabled button, the second an enabled one. The disabled button has the attribute disabled (which actually makes no sense for <div> elements, but you can also use <button> elements or whatever you want) as well as the additional class jcarousel-prev-disabled (or jcarousel-next-disabled).
  • All <li> elements of the list have the class jcarousel-item-n assigned where n represents the position in the list.
  • Not shown here is, that all classes are followed by additional classes with a suffix dependent on the orientation of the carousel, ie. <ul class="jcarousel-list jcarousel-list-horizontal"> for a horizontal carousel.

Dynamic content loading

By passing the callback function itemLoadCallback as configuration option, you are able to dynamically create <li> items for the content.

<script type="text/javascript">
jQuery(document).ready(function() {
    jQuery('#mycarousel').jcarousel({
        itemLoadCallback: itemLoadCallbackFunction
    });
});
</script>

itemLoadCallbackFunction is a JavaScript function that is called when the carousel requests a set of items to be loaded. Two parameters are passed: The instance of the requesting carousel and a flag which indicates the current state of the carousel ('init', 'prev' or 'next').

<script type="text/javascript">
function itemLoadCallbackFunction(carousel, state)
{
    for (var i = carousel.first; i <= carousel.last; i++) {
        // Check if the item already exists
        if (!carousel.has(i)) {
            // Add the item
            carousel.add(i, "I'm item #" + i);
        }
    }
};
</script>

jCarousel contains a convenience method add() that can be passed the index of the item to create and the innerHTML string of the item to be created. If the item already exists, it just updates the innerHTML. You can access the index of the first and last visible element by the public variables carousel.first and carousel.last.

Configuration

jCarousel accepts a list of options to control the appearance and behaviour of the carousel. Here is the list of options you may set:

PropertyTypeDefaultDescription
verticalboolfalseSpecifies wether the carousel appears in horizontal or vertical orientation. Changes the carousel from a left/right style to a up/down style carousel.
startinteger1The index of the item to start with.
offsetinteger1The index of the first available item at initialisation.
sizeintegerNumber of existing <li> elements if size is not passed explicitlyThe number of total items.
scrollinteger3The number of items to scroll by.
visibleintegernullIf passed, the width/height of the items will be calculated and set depending on the width/height of the clipping, so that exactly that number of items will be visible.
animationmixed"fast"The speed of the scroll animation as string in jQuery terms ("slow" or "fast") or milliseconds as integer (See jQuery Documentation). If set to 0, animation is turned off.
easingstringnullThe name of the easing effect that you want to use (See jQuery Documentation).
autointeger0Specifies how many seconds to periodically autoscroll the content. If set to 0 (default) then autoscrolling is turned off.
wrapstringnullSpecifies whether to wrap at the first/last item (or both) and jump back to the start/end. Options are "first", "last" or "both" as string. If set to null, wrapping is turned off (default). You can also pass "circular" as option to enable support for circular carousels. See the example Circular carousel on how to implement it.
initCallbackfunctionnullJavaScript function that is called right after initialisation of the carousel. Two parameters are passed: The instance of the requesting carousel and the state of the carousel initialisation (init, reset or reload)
itemLoadCallbackfunctionnullJavaScript function that is called when the carousel requests a set of items to be loaded. Two parameters are passed: The instance of the requesting carousel and the state of the carousel action (prev, next or init). Alternatively, you can pass a hash of one or two functions which are triggered before and/or after animation:
itemLoadCallback: {
  onBeforeAnimation: callback1,
  onAfterAnimation: callback2
}
itemFirstInCallback functionnullJavaScript function that is called (after the scroll animation) when an item becomes the first one in the visible range of the carousel. Four parameters are passed: The instance of the requesting carousel and the <li> object itself, the index which indicates the position of the item in the list and the state of the carousel action (prev, next or init). Alternatively, you can pass a hash of one or two functions which are triggered before and/or after animation:
itemFirstInCallback: {
  onBeforeAnimation: callback1,
  onAfterAnimation: callback2
}
itemFirstOutCallbackfunctionnullJavaScript function that is called (after the scroll animation) when an item isn't longer the first one in the visible range of the carousel. Four parameters are passed: The instance of the requesting carousel and the <li> object itself, the index which indicates the position of the item in the list and the state of the carousel action (prev, next or init). Alternatively, you can pass a hash of one or two functions which are triggered before and/or after animation:
itemFirstOutCallback: {
  onBeforeAnimation: callback1,
  onAfterAnimation: callback2
}
itemLastInCallbackfunctionnullJavaScript function that is called (after the scroll animation) when an item becomes the last one in the visible range of the carousel. Four parameters are passed: The instance of the requesting carousel and the <li> object itself, the index which indicates the position of the item in the list and the state of the carousel action (prev, next or init). Alternatively, you can pass a hash of one or two functions which are triggered before and/or after animation:
itemLastInCallback: {
  onBeforeAnimation: callback1,
  onAfterAnimation: callback2
}
itemLastOutCallbackfunctionnullJavaScript function that is called when an item isn't longer the last one in the visible range of the carousel. Four parameters are passed: The instance of the requesting carousel and the <li> object itself, the index which indicates the position of the item in the list and the state of the carousel action (prev, next or init). Alternatively, you can pass a hash of one or two functions which are triggered before and/or after animation:
itemLastOutCallback: {
  onBeforeAnimation: callback1,
  onAfterAnimation: callback2
}
itemVisibleInCallbackfunctionnullJavaScript function that is called (after the scroll animation) when an item is in the visible range of the carousel. Four parameters are passed: The instance of the requesting carousel and the <li> object itself, the index which indicates the position of the item in the list and the state of the carousel action (prev, next or init). Alternatively, you can pass a hash of one or two functions which are triggered before and/or after animation:
itemVisibleInCallback: {
  onBeforeAnimation: callback1,
  onAfterAnimation: callback2
}
itemVisibleOutCallbackfunctionnullJavaScript function that is called (after the scroll animation) when an item isn't longer in the visible range of the carousel. Four parameters are passed: The instance of the requesting carousel and the <li> object itself, the index which indicates the position of the item in the list and the state of the carousel action (prev, next or init). Alternatively, you can pass a hash of one or two functions which are triggered before and/or after animation:
itemVisibleOutCallback: {
  onBeforeAnimation: callback1,
  onAfterAnimation: callback2
}
buttonNextCallbackfunctionnullJavaScript function that is called when the state of the 'next' control is changing. The responsibility of this method is to enable or disable the 'next' control. Three parameters are passed: The instance of the requesting carousel, the control element and a flag indicating whether the button should be enabled or disabled.
buttonPrevCallbackfunctionnullJavaScript function that is called when the state of the 'previous' control is changing. The responsibility of this method is to enable or disable the 'previous' control. Three parameters are passed: The instance of the requesting carousel, the control element and a flag indicating whether the button should be enabled or disabled.
buttonNextHTMLstring<div></div>The HTML markup for the auto-generated next button. If set to null, no next-button is created.
buttonPrevHTMLstring<div></div>The HTML markup for the auto-generated prev button. If set to null, no prev-button is created.
buttonNextEventstring"click"Specifies the event which triggers the next scroll.
buttonPrevEventstring"click"Specifies the event which triggers the prev scroll.

Compatibility

jCarousel has been tested and works on the following browsers:

  • Internet Explorer 6 (PC)
  • Internet Explorer 7 (PC)
  • FireFox 1.5.0.6 (PC/Mac/Linux)
  • Opera 9.01 (PC/Mac)
  • Safari 2.0.4 (Mac)
  • Safari 3.1.0 (PC)
  • Konqueror 3.4.0 (Linux)

Credits

Thanks to John Resig for his fantastic jQuery library.
jCarousel is inspired by the Carousel Component written by Bill Scott.

转载于:https://www.cnblogs.com/toto/archive/2010/01/08/1642047.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值