如何在JavaScript中将事件“onclick”添加到对象的原型中

时间:2017-05-12 12:35:59

标签: javascript

我使构造函数创建函数轮播,我想要

将事件侦听器“click”添加到原型以继承子级。当我创建一些实例继承这个属性;

function Carousel(element, interval){
        this.element = document.querySelector(element);
        this.item = this.element.querySelectorAll('.item');
        this.toNext = this.element.querySelector('.right');
        this.toPrev = this.element.querySelector('.left');
        this.interval = interval;
        this.active =0;

        // Display First Element
        this.item[0].style.left = 0; 

    }

    Carousel.prototype.next = function(){
        var next;
        if (this.active === (this.item.length - 1)) {
            next = 0;
        }else{
            next = this.active + 1;
        }

        TweenMax.to(this.item[this.active], 0.8, {left:'-300px',ease:Power1.easeOut});
        TweenMax.to(this.item[next], 0.8, {left: 0, ease:Power1.easeOut});
        TweenMax.set(this.item[next], {left:'300px'})

        // Update  this.Active Position 
        this.active = next;
    }

    Carousel.prototype.prev = function() {
        var prev;
        if (this.active === 0) {
            prev = this.item.length - 1 ;
        }else{
            prev = this.active - 1;
        }

        TweenMax.set(this.item[prev], {left:'-300px'})
        TweenMax.to(this.item[this.active], 0.8, {left:'300px',ease:Power1.easeOut});
        TweenMax.to(this.item[prev], 0.8, {left: 0, ease:Power1.easeOut});

        // Update  this.active Position 
        this.active = prev;
    }

0 个答案:

没有答案