0

0

js实现图片轮播的代码实例(详细代码)

不言

不言

发布时间:2018-08-09 17:57:15

|

6878人浏览过

|

来源于php中文网

原创

本篇文章给大家带来的内容是关于js实现图片轮播的代码实例(详细代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

broadcast.js

var CarouselFigure = new Object();
//轮播图初始化定义函数
CarouselFigure.init = function(tmpobj){
    //定义动画帧数(默认为:30)
    this.frameNum = tmpobj.frameNum !=undefined ?  tmpobj.frameNum : 30;
    //定义一次轮播的时间(默认为:0.2s)
    this.comsumeTime = tmpobj.comsumeTime !=undefined ?  tmpobj.comsumeTime : 200;
    //定义轮播图的宽高(默认宽高为:700px * 250px)
    this.CFWidth = tmpobj.CFWidth !=undefined ?  tmpobj.CFWidth : 700;
    this.CFHeight = tmpobj.CFHeight !=undefined ?  tmpobj.CFHeight : 250;
    //定义轮播的风格
    this.type = tmpobj.type == "small" ? 'small' : 'big';
    //轮播图的按钮大小的宽高
    this.AssowWidth = tmpobj.AssowWidth !=undefined ?  tmpobj.AssowWidth : 76;
    this.AssowHeight = tmpobj.AssowHeight !=undefined ?  tmpobj.AssowHeight : 112;
    //判断用户是否初始化
    this.initFlag = true;
    //是否允许轮播图运行
    this.applicationflag = true;
}
CarouselFigure.start = function(){
/**
    第一部分类容:初始化整个轮播图和其运行数据
*/
(function(){
    //1. 判断用户是否进行初始化
    if(CarouselFigure.initFlag == undefined){
        CarouselFigure.init({});
    }
    
    //2.1 初始化整个轮播图的div的基本大小
    $("#CarouselFigure").width(CarouselFigure.CFWidth).height(CarouselFigure.CFHeight).css("position","relative");
    
    //2.2 最中央的大图实际大小为:
    CarouselFigure.ImgWidth = CarouselFigure.CFWidth * 2/3;
    CarouselFigure.ImgHeight =  CarouselFigure.CFHeight - 6;
    
    //3. 初始化轮播图按钮数据
    CarouselFigure.setAssowdata = {
    prev:{
          top:(CarouselFigure.CFHeight - CarouselFigure.AssowHeight)/2 +"px",
          left:CarouselFigure.CFWidth/6 - CarouselFigure.AssowWidth + 6 + "px",
          originUrl:$("#CarouselFigure > img:eq(0)").attr("src"),
          hoverUrl:$("#CarouselFigure > img:eq(1)").attr("src"),
          },
    next:{
          top:(CarouselFigure.CFHeight - CarouselFigure.AssowHeight)/2 +"px",
          left:CarouselFigure.CFWidth*5/6 + "px",
          originUrl:$("#CarouselFigure > img:eq(2)").attr("src"),
          hoverUrl:$("#CarouselFigure > img:eq(3)").attr("src"),
          }
    };
    
    //4.1 初始化轮播图的url和src信息,存放到一个容器中
    CarouselFigure.imageContains = [];                        
    $("#CarouselFigure ul li img").each(function(){
        var tmpobj = {src:$(this).attr("src"),href:$(this).attr("href")}
        CarouselFigure.imageContains.push(tmpobj);
    });
    //4.2 对轮播图容器数据进行处理,当轮播图的个数3<= x  <= 5 只能使用small风格类型。当轮播图的个数 x < 3 时, 停止运行
    if(CarouselFigure.imageContains.length < 3){
        CarouselFigure.applicationflag = false;
    }else if(CarouselFigure.imageContains.length < 6){
        CarouselFigure.type = 'small';
    }
    //4.3 对轮播容器数据按顺序进行增倍,保证有足够数据进行轮播
    var objstr = JSON.stringify(CarouselFigure.imageContains);
    CarouselFigure.imageContains = CarouselFigure.imageContains.concat(JSON.parse(objstr));
    //5.1 轮播图使用big风格时,页面七张图在静态页面中的属性值
    CarouselFigure.setViewPosData = new Object;
     CarouselFigure.setViewPosData.big = [
        {
            width:CarouselFigure.ImgWidth*3/8,        height:CarouselFigure.ImgHeight*3/8,
            left:0,                                    top:0,
            zIndex:1,            opacity:0.2,        borderSize:0
        },{
            width:CarouselFigure.ImgWidth*3/8,        height:CarouselFigure.ImgHeight*3/8,
            left:0,                                    top:CarouselFigure.CFHeight*5/16,
            zIndex:2,            opacity:0.7,        borderSize:0
        },{
            width:CarouselFigure.ImgWidth*3/4,        height:CarouselFigure.ImgHeight*3/4,
            left:CarouselFigure.CFWidth/18,            top:CarouselFigure.CFHeight/8,
            zIndex:3,            opacity:0.9,        borderSize:0
        },{
            width:CarouselFigure.ImgWidth,            height:CarouselFigure.ImgHeight,
            left:CarouselFigure.CFWidth/6,            top:0,
            zIndex:4,            opacity:1,            borderSize:3
        },{
            width:CarouselFigure.ImgWidth*3/4,        height:CarouselFigure.ImgHeight*3/4,
            left:CarouselFigure.CFWidth * 4/9,        top:CarouselFigure.CFHeight/8,
            zIndex:3,            opacity:0.9,        borderSize:0
        },{
            width:CarouselFigure.ImgWidth*3/8,        height:CarouselFigure.ImgHeight*3/8,
            left:CarouselFigure.CFWidth * 3/4,        top:CarouselFigure.CFHeight*5/16,
            zIndex:2,            opacity:0.7,        borderSize:0
        },{
            width:CarouselFigure.ImgWidth*3/8,        height:CarouselFigure.ImgHeight*3/8,
            left:CarouselFigure.CFWidth * 3/4,        top:0,
            zIndex:1,            opacity:0.2,        borderSize:0
        },];
    //5.2 轮播图使用small风格时,页面5张图在静态页面中的属性值
     CarouselFigure.setViewPosData.small = [
        {
            width:CarouselFigure.ImgWidth*3/8,        height:CarouselFigure.ImgHeight*3/8,
            left:0,                                    top:0,
            zIndex:1,            opacity:0.2,        borderSize:0
        },{
            width:CarouselFigure.ImgWidth*3/4,        height:CarouselFigure.ImgHeight*3/4,
            left:0,                                    top:CarouselFigure.CFHeight/8,
            zIndex:2,            opacity:0.9,        borderSize:0
        },{
            width:CarouselFigure.ImgWidth,            height:CarouselFigure.ImgHeight,
            left:CarouselFigure.CFWidth/6,            top:0,
            zIndex:3,            opacity:1,            borderSize:3
        },{
            width:CarouselFigure.ImgWidth*3/4,        height:CarouselFigure.ImgHeight*3/4,
            left:CarouselFigure.CFWidth * 1/2,        top:CarouselFigure.CFHeight/8,
            zIndex:2,            opacity:0.9,        borderSize:0
        },{
            width:CarouselFigure.ImgWidth*3/8,        height:CarouselFigure.ImgHeight*3/8,
            left:CarouselFigure.CFWidth * 3/4,        top:0,
            zIndex:1,            opacity:0.2,        borderSize:0
        }];
}());
//验证初始化是否成功,否则全部隐藏,结束进程
if(!CarouselFigure.applicationflag){
    $("#CarouselFigure").css("display","none");
    return false;
}
/**
    第二部分:
        对轮播图的箭头进行初始化(包括页面中的静态布局,和点击事件和悬浮事件)
*/
    CarouselFigure.InitAssow = function (Assow,direction){
        //实现轮播图箭头的静态样式、悬浮事件和点击事件
        Assow.css({
            position:"absolute",
            left:this.setAssowdata[direction].left,
            top:this.setAssowdata[direction].top,
            "z-index":4
        })
        .mouseover(function(){            //鼠标悬浮切换图片
            $(this).attr("src",CarouselFigure.setAssowdata[direction].hoverUrl);
        }).mouseout(function(){
            $(this).attr("src",CarouselFigure.setAssowdata[direction].originUrl);
        }).click(function(){
            //记录点击事件的次数
            CarouselFigure.clickArrowfun(direction);    
        });
    }
    //调用初始化轮播图函数--实现左右箭头全部功能
    $("#CarouselFigure > img:odd").css("display","none");
    CarouselFigure.InitAssow($("#CarouselFigure > img:eq(0)"),"prev");
    CarouselFigure.InitAssow($("#CarouselFigure > img:eq(2)"),"next");
/**
    第三部分:
        对所有的轮播图进行页面静态布局
*/
    //初始化某张轮播图的方法
    CarouselFigure.InitImages =    function (i,setViewPosData,imageContains){
        $("#CarouselFigure ul img:eq("+i+")").css({
            position:"absolute",
            width:setViewPosData[i].width + "px",
            height:setViewPosData[i].height + "px",
            top:setViewPosData[i].top + "px",
            left:setViewPosData[i].left + "px",
            display:"block",
            "z-index":setViewPosData[i].zIndex,
            opacity:setViewPosData[i].opacity,    
        }).attr({
            src:imageContains[i].src,
            href:imageContains[i].href
        }).click(function(){
            location.href = $(this).attr("href");    //绑定图片点击跳转事件
        });
        if( (i == 0 || i == 6) && this.type == 'big'){        //第1张图片和第7张图片不可见
            $("#CarouselFigure ul img:eq("+i+")").css("display","none");
        }else if(i == 3 && this.type == 'big'){                //为正中央图片加边框
            $("#CarouselFigure ul img:eq("+i+")").css("border","3px solid #fff");
        }else if( (i == 0 || i == 4)&&this.type == 'small'){//第1张图片和第5张图片不可见
            $("#CarouselFigure ul img:eq("+i+")").css("display","none");
        }else if(i == 2 && this.type == 'small'){            //为正中央图片加边框
            $("#CarouselFigure ul img:eq("+i+")").css("border","3px solid #fff");
        }
    }
    /**
        实现七张图片的静态样式:
            清空原有的li标签,然后新建
  • js实现图片轮播的代码实例(详细代码)
  • 来存放每张轮播图。 */ $("#CarouselFigure ul").empty(); for(var i = 0; i < CarouselFigure.setViewPosData[CarouselFigure.type].length; i++){ $("#CarouselFigure ul").append('
  • js实现图片轮播的代码实例(详细代码)
  • '); CarouselFigure.InitImages(i,CarouselFigure.setViewPosData[CarouselFigure.type],CarouselFigure.imageContains); } /** 第四部分: 形成轮播动画效果 */ /* 设置clickArrow变量:作为动画的标示位,让其进行队列化,必须本次动画完成,才能进行下次动画轮播。 1. 当为负数时向前轮播,比如:-5向前轮播五次动画; 2. 当为正数时向后轮播,同理; 3. 当等于0时,此时没有进行轮播。 */ CarouselFigure.clickArrow = 0 ; // 处理动画队列方法: CarouselFigure.clickArrowfun = function (direction){ if(this.clickArrow == 0){ this.startAnimation(direction); } if(direction == "prev"){ this.clickArrow--; }else if(direction == "next"){ this.clickArrow++; } } /** 开始一次轮播动画 其中composerAnimateNum变量为: 在一次动画中,记录每张轮播图完成的标识符; 在big风格类型中:当其值为6时说明此次动画全部完成 在small风格类型中:当其值为4时说明此次动画全部完成 */ CarouselFigure.startAnimation = function(direction){ this.completeAnimationNum = 0; if(direction == "prev"){ //从最后一张图片到第一张轮播图片,分别向前移动,首先让最后一张图片可视 if(this.type == "big"){ $("#CarouselFigure ul img:eq(6)").css("display","block"); }else{ $("#CarouselFigure ul img:eq(4)").css("display","block"); } for(var i = this.setViewPosData[this.type].length - 1 ; i > 0; i --){ this.startMove(i, this.setViewPosData[this.type][i],this.setViewPosData[this.type][i-1],direction); } }else if(direction == "next"){//从第一张图片到倒数第二张图片,分别向后移动,首先让第一张轮播图片可视 $("#CarouselFigure ul img:eq(0)").css("display","block"); for(var i = 0 ; i < this.setViewPosData[this.type].length - 1; i ++){ this.startMove(i, this.setViewPosData[this.type][i],this.setViewPosData[this.type][i+1],direction); } } } /** 功能:让一张轮播图从某一个状态变换到另一个状态 (包括位子、透明度、边框等等属性) */ CarouselFigure.startMove = function (index,startObj,endObj,direction){ //所有属性的增量 var increaseLeft = endObj.left - startObj.left; var increaseTop = endObj.top - startObj.top; var increaseWidth = endObj.width - startObj.width; var increaseHeight = endObj.height - startObj.height; var increaseOpacity = endObj.opacity - startObj.opacity; var increaseBorderSize = endObj.borderSize - startObj.borderSize; var time = this.frameNum; //共总帧数 var i = 1; //帧数的记步标志索引 //定义函数:一帧到下一帧的变化过程 function moveStep(){ setTimeout(function(){ var tmpObj = $("#CarouselFigure ul img:eq("+index+")"); //每一帧时图片的属性改变量 tmpObj.css({ width:startObj.width + (increaseWidth/time) * i + "px", height:startObj.height + (increaseHeight/time) * i + "px", top:startObj.top + (increaseTop/time) * i + "px", left:startObj.left + (increaseLeft/time) * i + "px", border:startObj.borderSize + (increaseBorderSize/time) * i + "px solid #fff", opacity:startObj.opacity + (increaseOpacity/time) * i, }); //当小于30帧时,继续递归,直至调用30次完成动画效果 if( i++ 0){ this.startAnimation(direction); }else if(this.clickArrow < 0){ this.startAnimation(direction); } } } };

    broadcast.html

    
    
        
            
            轮播图实例
        
        
        
        
            
    js实现图片轮播的代码实例(详细代码) js实现图片轮播的代码实例(详细代码) js实现图片轮播的代码实例(详细代码) js实现图片轮播的代码实例(详细代码)
    • js实现图片轮播的代码实例(详细代码)
    • js实现图片轮播的代码实例(详细代码)
    • js实现图片轮播的代码实例(详细代码)
    • js实现图片轮播的代码实例(详细代码)
    • js实现图片轮播的代码实例(详细代码)
    • js实现图片轮播的代码实例(详细代码)
    • js实现图片轮播的代码实例(详细代码)
    • js实现图片轮播的代码实例(详细代码)

    实现效果

    相关推荐:

    jquery实现3D旋转轮播图片代码
    jquery实现3D旋转轮播图片代码

    jquery实现3D旋转轮播图片代码

    下载

    怎么用简单的js代码实现轮播图滚动效果?

    koa-router是什么?详解koa-router的大致结构与运行流程

    相关专题

    更多
    html版权符号
    html版权符号

    html版权符号是“©”,可以在html源文件中直接输入或者从word中复制粘贴过来,php中文网还为大家带来html的相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

    612

    2023.06.14

    html在线编辑器
    html在线编辑器

    html在线编辑器是用于在线编辑的工具,编辑的内容是基于HTML的文档。它经常被应用于留言板留言、论坛发贴、Blog编写日志或等需要用户输入普通HTML的地方,是Web应用的常用模块之一。php中文网为大家带来了html在线编辑器的相关教程、以及相关文章等内容,供大家免费下载使用。

    650

    2023.06.21

    html网页制作
    html网页制作

    html网页制作是指使用超文本标记语言来设计和创建网页的过程,html是一种标记语言,它使用标记来描述文档结构和语义,并定义了网页中的各种元素和内容的呈现方式。本专题为大家提供html网页制作的相关的文章、下载、课程内容,供大家免费下载体验。

    468

    2023.07.31

    html空格
    html空格

    html空格是一种用于在网页中添加间隔和对齐文本的特殊字符,被用于在网页中插入额外的空间,以改变元素之间的排列和对齐方式。本专题为大家提供html空格的相关的文章、下载、课程内容,供大家免费下载体验。

    245

    2023.08.01

    html是什么
    html是什么

    HTML是一种标准标记语言,用于创建和呈现网页的结构和内容,是互联网发展的基石,为网页开发提供了丰富的功能和灵活性。本专题为大家提供html相关的各种文章、以及下载和课程。

    2892

    2023.08.11

    html字体大小怎么设置
    html字体大小怎么设置

    在网页设计中,字体大小的选择是至关重要的。合理的字体大小不仅可以提升网页的可读性,还能够影响用户对网页整体布局的感知。php中文网将介绍一些常用的方法和技巧,帮助您在HTML中设置合适的字体大小。

    505

    2023.08.11

    html转txt
    html转txt

    html转txt的方法有使用文本编辑器、使用在线转换工具和使用Python编程。本专题为大家提供html转txt相关的文章、下载、课程内容,供大家免费下载体验。

    311

    2023.08.31

    html文本框代码怎么写
    html文本框代码怎么写

    html文本框代码:1、单行文本框【<input type="text" style="height:..;width:..;" />】;2、多行文本框【textarea style=";height:;"></textare】。

    424

    2023.09.01

    高德地图升级方法汇总
    高德地图升级方法汇总

    本专题整合了高德地图升级相关教程,阅读专题下面的文章了解更多详细内容。

    43

    2026.01.16

    热门下载

    更多
    网站特效
    /
    网站源码
    /
    网站素材
    /
    前端模板

    精品课程

    更多
    相关推荐
    /
    热门推荐
    /
    最新课程
    Bootstrap 5教程
    Bootstrap 5教程

    共46课时 | 2.9万人学习

    前端开发(基础+实战项目合集)
    前端开发(基础+实战项目合集)

    共60课时 | 3.8万人学习

    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送

    Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号