zoukankan      html  css  js  c++  java
  • 面向对象轮播图

    function Init(){
    this.olist = document.getElementsByClassName("list")[0];
    this.ali = this.olist.getElementsByTagName("li");
    this.lw = this.ali[0].offsetWidth;
    this.btn = document.getElementsByClassName("btn")[0].getElementsByTagName("a");
    this.dot = document.getElementsByClassName("dot")[0].getElementsByTagName("a");
    this.ban = document.getElementsByClassName("banner")[0];
    this.btn1=document.getElementsByClassName("btn")[0];
    this.now = 0;
    this.timer = null;
    var _this=this;
    this.olist.style.width=this.lw*this.ali.length+'px';


    //轮播的停止和播放
    this.ban.onmouseover=function(){
    clearInterval(_this.timer);
    _this.btn1.style.display = "block";
    }
    this.ban.onmouseout=function(){
    _this.play();
    _this.btn1.style.display = "none";
    }
    //左右切换
    this.btn[0].onclick=function(){
    if(_this.now==0){
    _this.now=_this.ali.length-2;
    _this.olist.style.left=-_this.lw*(_this.ali.length-1)+'px'
    }else{
    _this.now--;
    }
    _this.toimg()
    }
    this.btn[1].onclick=function(){
    if(_this.now==_this.ali.length-1){
    _this.now=1;
    _this.olist.style.left=0;
    }else{
    _this.now++;
    }
    _this.toimg()
    }
    //点击圆点切换
    for(var i=0;i<this.dot.length;i++){
    this.dot[i].index = i;
    this.dot[i].onclick=function(){
    _this.click(this);
    }
    }
    }
    Init.prototype.click = function(that){
    for(var j=0;j<this.dot.length;j++){
    this.dot[j].className = '';
    }
    that.className = "active";
    move(this.olist,{"left":-that.index*this.lw})
    this.now=that.index;
    }
    //轮播原理
    Init.prototype.toimg = function(){
    move(this.olist,{"left":this.lw*-this.now});
    for(var i=0;i<this.dot.length;i++){
    this.dot[i].className='';
    }
    this.dot[this.now==this.ali.length-1?0:this.now].className="active";
    }
    //自动轮播
    Init.prototype.play = function(){
    var _this=this;
    this.timer=setInterval(function(){
    if(_this.now==_this.ali.length-1){
    _this.now=1;
    _this.olist.style.left=0;
    }else{
    _this.now++;
    }
    _this.toimg()
    },2000)
    }
    new Init().play();

  • 相关阅读:
    day13_迭代器,生成器
    作业礼包
    day12_装饰器进阶
    装饰器作业
    day11_装饰器
    函数作业
    day10-函数进阶
    数据类型-习题解答
    day09_函数
    PHP 完美分页
  • 原文地址:https://www.cnblogs.com/xiaocuncheng/p/8487942.html
Copyright © 2011-2022 走看看