实现京东轮播图实现(二)

续上jd-JavaScript全部代码

function Banner(){
this.img = document.querySelectorAll(".banner .box a");
this.left = document.getElementById(“left”);
this.right = document.getElementById(“right”);
this.span = document.querySelectorAll(".banner .num span");
this.Banner = document.querySelector(".ban");

this.index = 0;
this.iprev = NaN;
this.addEvent();

}
Banner.prototype = {
construcyor:Banner,
addEvent : function(){
var that = this;
this.left.onclick = function(){
that.changeIndex(-1);
that.active(that.index);
}
this.right.onclick = function(){
that.changeIndex(1);
that.active(that.index);
}
this.Banner.onmouseover = function(){
that.left.style.display = “block”;
that.right.style.display = “block”;
}
this.Banner.onmouseout = function(){
that.left.style.display = “none”;
that.right.style.display = “none”;
}
for(var i=0;i<this.span.length;i++){
this.span[i].xuhao = i;
this.span[i].onmouseover = function(){
that.active(this.xuhao);
that.isDirect(this);
}
}

},
active : function(m){
    for(var i=0;i<this.span.length;i++){
        this.span[i].className = ""
    }
    this.span[m].className = "active";
},
isDirect : function(iNow){
    if(iNow.xuhao > this.index){
        this.changeImg(iNow,1);
    }
    if(iNow.xuhao < this.index){
        this.changeImg(iNow,-1);
    }
    this.index = iNow.xuhao;
},
changeImg : function(iNow,d){
    this.img[this.index].style.left = 0;
            move(this.img[this.index],{left:-785*d});
            this.img[iNow.xuhao].style.left = 785*d + "px";
            move(this.img[iNow.xuhao],{left:0});
},
changeIndex : function(d){
    if(d === -1){
        if(this.index === 0){
            this.index = this.img.length-1;
            this.iprev = 0;
        }else{
            this.index --;
            this.iprev = this.index + 1;
        }
    }else{
        if(this.index === this.img.length-1){
            this.index = 0;
            this.iprev = this.img.length-1;
        }else{
            this.index ++;
            this.iprev = this.index - 1;
        }
        
    }
    this.showImg(d);
},
showImg : function(d){
    this.img[this.iprev].style.left = 0;
    move(this.img[this.iprev],{left:-785*d});
    this.img[this.index].style.left = 785*d + "px";
    move(this.img[this.index],{left:0});
},

}
new Banner();
function move(ele, data, cb){
clearInterval(ele.t);
ele.t = setInterval(()=>{
var onoff = true;
for(var i in data){
var iNow = i === “opacity” ? getStyle(ele, i) * 100 : parseInt(getStyle(ele, i));

        var speed = (data[i] - iNow) / 10;
        speed = speed<0 ? Math.floor(speed) : Math.ceil(speed);

        if(iNow != data[i]) onoff = false;

        ele.style[i] = i === "opacity" ? (iNow + speed) / 100 : iNow + speed + "px";
    }
    if(onoff){
        clearInterval(ele.t);
        cb && cb();
    }
},30);

}
function getStyle(ele,attr){
if(ele.currentStyle){
return ele.currentStyle[attr];
}else{
return getComputedStyle(ele, null)[attr];
}
}

var rightnav = document.querySelector(".rightnav");
var denglu = document.querySelector(".denglu");
var i = document.querySelector(".rightnav i")
var banner = document.querySelector(".banner")

var t = rightnav.offsetTop;
var tt = true;
onscroll = function(){
rightnav.style.top=document.documentElement.scrollTop + t + “px”;
}
denglu.onclick = function(eve){
var e = eve || window.event;
e.cancelBubble = true;
if(tt){
move(rightnav,{right:0});
tt = false;
}else{
move(rightnav,{right:-276});
tt = true;
}
}
i.onclick = function(eve){
var e = eve || window.event;
e.cancelBubble = true;
move(rightnav,{right:-276});
tt = true;
}
rightnav.onclick = function(eve){
var e = eve || window.event;
e.cancelBubble = true;
}
document.onclick = function(){
move(rightnav,{right:-276});
tt = true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值