Carousel.js

define('Carousel',function(){
    var Carousel = {
        viewport : null ,
        intervlTime : 3000 ,
        index : 0 ,

        init : function(config) {
            this.viewport = config.viewport;
    //<div class="viewport" style="position:relative;width:558px;height:240px;overflow:hidden;><ul><li></li></ul></div>"

            this.ul = this.viewport.find('ul');

            this.len = this.ul.find('li').length;

            if(this.len<=1){
                return ;
            }

            this.width = this.viewport.width();

            this.render();

            this.bindEvent();

            this.start();

        },

        render : function() {
            this.ul.css('width',this.width*this.len);

            this.page();

            return this;
        },

        page : function() {
            var html='';

            this.viewport.append('<div class="pager"></div>');

            for(var i=0; i<this.len; i++){
                html += ((i==0)?'<span class="active">':'<span>') + (i+1) +'</span>';
            }

            this.page = this.viewport.find('.pager');
            this.page.append(html);

            return this;
        },

        bindEvent : function() {
            this.viewport.on('mouseenter',jQuery.proxy(this,'mouseenter'));
            this.viewport.on('mouseleave',jQuery.proxy(this,'mouseleave'));
            this.page.on('click','span',jQuery.proxy(this,'click'));
        },

        mouseenter : function(event) {
            this.stop();
        },

        mouseleave : function(event) {
            this.start();
        },

        click : function(event) {
            this.remove();

            this.index = $(event.target).html()-1;

            this.move(false);
        },

        loop : function(){
            this.remove();

            (this.index === this.len-1)? this.index=0 : this.index++;

            this.move(true);
        },

        start : function(){
            this.interval = window.setInterval(jQuery.proxy(this,'loop'),this.intervlTime);
        },

        stop : function(){
            window.clearInterval(this.interval);
        },

        move : function(animate){
            this.page.find('span').eq(this.index).addClass('active');

            var _marginLeft = '-'+(this.index*this.width)+'px';

            if(animate){
                this.ul.animate({'margin-left': _marginLeft},'slow');
            }else{
                this.ul.css({'margin-left': _marginLeft});
            }
        },

        remove : function(){
            this.page.find('span').eq(this.index).removeClass('active');
        }
    };

    return Carousel;  
});


var carousel = require('');Carousel
carousel .init({"viewport":$('#Silder-TC')});

转载于:https://www.cnblogs.com/huengscut/p/3463510.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值