zoukankan      html  css  js  c++  java
  • 轮播图

    /JavaScript代码/
    var timer = null,
    index = 0,
    pics = byId("banner").getElementsByTagName("div"),
    dots = byId("dots").getElementsByTagName("span"),
    size = pics.length,
    prev = byId("prev"),
    next = byId("next"),
    menuItems = byId("menu-content").getElementsByTagName("div"),
    subMenu = byId("sub-menu"),
    subItems = subMenu.getElementsByClassName("inner-box");

    function byId(id){
    return typeof(id)==="string"?document.getElementById(id):id;
    }

    // 清除定时器,停止自动播放
    function stopAutoPlay(){
    if(timer){
    clearInterval(timer);
    }
    }

    // 图片自动轮播
    function startAutoPlay(){
    timer = setInterval(function(){
    index++;
    if(index >= size){
    index = 0;
    }
    changeImg();
    },3000)
    }

    function changeImg(){
    for(var i=0,len=dots.length;i<len;i++){
    dots[i].className = "";
    pics[i].style.display = "none";
    }
    dots[index].className = "active";
    pics[index].style.display = "block";
    }

    function slideImg(){
    var main = byId("main");
    var banner = byId("banner");
    var menuContent = byId("menu-content");
    main.onmouseover = function(){
    stopAutoPlay();
    }
    main.onmouseout = function(){
    startAutoPlay();
    }
    main.onmouseout();

    // 点击导航切换
    for(var i=0,len=dots.length;i<len;i++){
       dots[i].id = i;
       dots[i].onclick = function(){
           index = this.id;
           changeImg();
       }
    }
    
    // 下一张
    next.onclick = function(){
       index++;
       if(index>=size) index=0;
       changeImg();
    }
    
    // 上一张
    prev.onclick = function(){
       index--;
       if(index<0) index=size-1;
       changeImg();
    }
    
    // 菜单
    for(var m=0,mlen=menuItems.length;m<mlen;m++){
        menuItems[m].setAttribute("data-index",m);
        menuItems[m].onmouseover = function(){
            subMenu.className = "sub-menu";
            var idx = this.getAttribute("data-index");
            for(var j=0,jlen=subItems.length;j<jlen;j++){
               subItems[j].style.display = 'none';
               menuItems[j].style.background = "none";
            }
            subItems[idx].style.display = "block";
            menuItems[idx].style.background = "rgba(0,0,0,0.1)";
        }
    }
    
    subMenu.onmouseover = function(){
        this.className = "sub-menu";
    }
    
    subMenu.onmouseout = function(){
        this.className = "sub-menu hide";
    }
    
    menuContent.onmouseout = function(){
        subMenu.className = "sub-menu hide";
    }
    

    }

    slideImg();

  • 相关阅读:
    Live2d网页看板娘
    阿里云服务器(云主机)搭建网站攻略 最新9.5一个月
    Cookie小案例
    Node搭建多人聊天室
    JS鼠标点击爱心,文字特效
    JQ根据鼠标上下移动设置导航浮窗
    JS背景网页樱花特效
    Node中怎么保持MySql一直连接不断开
    Navicat for MySQL破解版
    Windows Server 2008 R2 安装MySql,PHP
  • 原文地址:https://www.cnblogs.com/ArielChen/p/7252748.html
Copyright © 2011-2022 走看看