我的JS轮播图笔记(未完)

我的JS轮播图笔记(未完)

// //采用js面向对象的方式
// //1添加属性:通过this关键字
// //原型链
//
// //创建对象
// function Banner() {
//     // 这个里面写的代码相当于python中的__init__方法。加载。
//     console.log("hello");
//     //通过this关键字创建属性
//     this.person = 'zhang';
// }
//
// //通过原型链绑定方法
// Banner.prototype.greet = function (word) {
//     console.log('原型链绑定方法',word);
// };
//
// //实例化对象
// var banner = new Banner();
// console.log(banner.person);
// banner.greet('hi');


function Banner() {
    this.bannerGroup = $("#banner-group");
    this.leftArrow = $('.left-arrow');
    this.rightArrow = $('.right-arrow');
    this.listenBannerHover();
    this.bannerUl = $('#banner-ul');
    this.liList = this.bannerUl.children('li');
    this.bannerCount = this.liList.length;
    this.index = 0;
}

//绑定箭头隐藏显示事件
Banner.prototype.toggleArrow = function (isShow) {
    var self = this;
    if (isShow) {
        self.leftArrow.show();
        self.rightArrow.show();
    } else {
        self.leftArrow.hide();
        self.rightArrow.hide();
    }
};

//绑定箭头点击事件
Banner.prototype.listenArrowClick = function () {
    self = this;
    self.leftArrow.click(function () {
        if (self.index === 0) {
            self.index = self.bannerCount - 1;
        } else {
            self.index--;
        }
        self.animate();
    });
    self.rightArrow.click(function () {
        if (self.index === self.bannerCount - 1) {
            self.index = 0;
        } else {
            self.index++;
        }
        self.animate();
    })
};

//绑定监听鼠标滑过事件
Banner.prototype.listenBannerHover = function () {
    var self = this;
    this.bannerGroup.hover(function () {
        //hover有两个函数,第一个函数是鼠标移动上面的函数
        clearInterval(self.timer);
        self.toggleArrow(true);
    }, function () {
        //第二个函数是把鼠标从上面移走的函数
        self.loop();
        self.toggleArrow(false);
    })
};

//重复性的代码单独提出来
Banner.prototype.animate = function () {
    this.bannerUl.animate({"left": -798 * this.index}, 500);
};

//绑定轮播图逻辑事件
Banner.prototype.loop = function () {
    var self = this;
    //获取轮播图的ul元素

    //这种使用css的方法会一步到位,不适合轮播图
    // bannerUL.css({"left":-798});

    this.timer = setInterval(function () {
        if (self.index >= 3) {
            self.index = 0;
        } else {
            self.index++;
        }
        self.animate();
    }, 2000);
};


//绑定run事件
Banner.prototype.run = function () {
    this.loop();
    this.listenArrowClick();
};

//通过jquery的$方法确保html全部加载后再执行方法。
$(function () {
    var banner = new Banner();
    banner.run();
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值