tab组件

export default {
name: ‘SideMenu’,
data() {
return {
prev,
next,
oList: null,
oPrev: null,
oNext: null,
oListLi: null,
oListUl: null,
len2: 0,
index: 0,
num: 0,
num2: 0,
w2: 0
}
},
props: {},
mounted() {
this.init()
},
methods: {
init () {
const that = this
that.oList = that.getElement(“listBox”);
that.oPrev = that.getElement(“prev”);
that.oNext = that.getElement(“next”);
that.oListLi = that.oList.getElementsByTagName(“li”);
that.oListUl = that.oList.getElementsByTagName(“ul”)[0];

        that.len2 = that.oListLi.length;
        that.w2 = that.oListLi[0].offsetWidth;
        that.oListUl.style.width = that.w2 * that.len2 + "px";

        that.index = 0;
        that.num = 5;
        that.num2 = Math.ceil(that.num / 2);

        for (var i = 0; i < that.len2; i++) {
            that.oListLi[i].index = i;
            that.oListLi[i].onclick = function(){
                that.Change();
            }
        }
    },
    Animate(obj, json){
        const that = this
        if (obj.timer) {
            clearInterval(obj.timer);
        }
        obj.timer = setInterval(function () {
            for (var attr in json){
                var iCur = parseInt(that.getStyle(obj, attr));
                iCur = iCur ? iCur : 0;
                var iSpeed = (json[attr] - iCur) / 5;
                iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
                obj.style[attr] = iCur + iSpeed + 'px';
                if (iCur == json[attr]) {
                    clearInterval(obj.timer);
                }
            }
        }, 30);
    },
    getElement (s) {
        return document.getElementById(s);
    },
    getStyle (obj, attr) {
        if(obj.currentStyle) {
            return obj.currentStyle[attr];
        }else {
            return getComputedStyle(obj, false)[attr];
        }
    },
    Change () {
        const that = this
        if (that.index < that.num2){
            that.Animate(that.oListUl, { left: 0 });
        } else if(that.index + that.num2 <= that.len2){
            that.Animate(that.oListUl, { left: - (that.index - that.num2 + 1) * that.w2 });
        } else{
            that.Animate(that.oListUl, { left: - (that.len2 - that.num) * that.w2 });
        }

        for (var i = 0; i < that.len2; i++) {
           that.oListLi[i].className = "";
            if(i == that.index){
               that.oListLi[i].className = "on";
            }
        }
    },
    handlePrev () {
        const that = this
        that.index --;
        that.index = that.index == -1 ? that.len2 -1 : that.index;
        that.Change();
    },
    handleNext () {
        const that = this
        that.index ++;
        that.index = that.index == -1 ? that.len2 -1 : that.index;
        that.Change();
    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值