jQuery实现碎片轮播效果

 更新时间:2022年05月06日 10:15:26   作者:南初️  
这篇文章主要为大家详细介绍了jQuery实现碎片轮播效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了jQuery实现碎片轮播效果的具体代码,供大家参考,具体内容如下

一、效果图

二、核心代码

碎片轮播.html

<script src="js/suiBanner.js"></script> 
<script>
    //实例化整个对象
    var suiBanner=$('.box').initBanner({
        imgs: ['images/1.jpg', 'images/2.jpg', 'images/3.jpg', 'images/4.jpg', 'images/5.jpg'], //图片集合 必选
        size: {
            width: 1000,
            height: 560
        }, //容器的大小 可选
        //行数与列数 可选
        grid: {
            line: 15,
            list: 18
        },
        index: 0, //图片集合的索引位置 可选
        boxTime: 1500, //小方块来回运动的时长 可选
        fnTime: 1000 //banner切换的时长 可选
    }) 
</script> 

suiBanner.js

function(){
    var instance;
    $.fn.extend({
        initBanner:function(setting){
            if(!instance){
                instance=new SuiBanner();
                instance.option.container=$(this);
                instance.option= $.extend({},instance.option,setting);
                instance._init();
                return instance;
            }
        }
    });
    //碎片轮播的类
    function SuiBanner(){

    }
    //设置默认配置
    SuiBanner.prototype={
        constructor:SuiBanner,
        option:{
            container:null,//默认的容器
            imgs:[],//图片集合必选
            size:{
                width:800,
                height:600
            },//容器的大小,可选
            grid:{
                line:8,
                list:12
            },
            index:0,//图片集合的索引位置,可选
            boxTime:1000,//小方块来回运动的时长,可选
            fnTime:3000,//banner切换的时长,可选
            sui:[],//碎片的集合
            suiPos:[],//存储碎块的最终位置
            timer:null,//接收计时器
        },
        _init:function(){
            var that=this,opt=this.option;
            //初始化方法
            //设置容器的样式
            if(opt.container){
                opt.container.css({
                    width:opt.size.width,
                    height:opt.size.height
                });
            }
            //创建dom
            that.createDom();
            //开始动画
            that.start();

        },
        createDom:function(){
            var that=this,opt=this.option;
            //创建dom元素
            opt.itemImage=$("<div class='itemImage'></div>");
            opt.imgs.forEach(function(img,index,arr){
                var img=$("<img src='"+img+"'/>");
                var aparent=$("<a href='#' style='z-index:"+(arr.length-index)+";'></a>");
                aparent.append(img);
                opt.itemImage.append(aparent);
            });
            opt.container.append(opt.itemImage);
            //创建一个碎片的容器
            opt.suiItem=$("<div class='suiItem'></div>");
            opt.container.append(opt.suiItem);
            //创建所有的碎片
            var html="";
            for(var i=0;i<opt.grid.line;i++){
                for(var k=0;k<opt.grid.list;k++){
                    opt.suiPos.push([opt.size.width/opt.grid.list*k,opt.size.height/opt.grid.line*i]);
                    html+="<div style='background-size: "+opt.size.width+"px
"+opt.size.height+"px;width:"+opt.size.width/opt.grid.list+"px;height:"+opt.size.height/opt.grid.line+"px;'></div>";
                }
            }
            opt.sui[0]=html;
        },
        start:function(){
            var that=this,opt=this.option;
            //开始加载动画
            opt.suiItem.html("");
            opt.itemImage.children().eq(opt.index).show().siblings().hide();
            opt.timer=setTimeout(function(){
                opt.index++;
                if(opt.index>=opt.imgs.length-1){
                    opt.index=0;
                }
                that.animation();
            },opt.fnTime);
        },
        animation:function(){
            var that=this,opt=this.option;
            //设置小块的随机位置
            opt.suiItem.html(opt.sui[0]).children().each(function(index){
                //随机自身的left、top
                var left=that.random(opt.size.width*2,opt.size.width/2);
                var top=that.random(opt.size.height*2,opt.size.height/2);
                $(this).css({
                    left:left,
                    top:top,
                    backgroundImage:'url('+opt.imgs[opt.index]+')',
                    backgroundPosition:-opt.suiPos[index][0]+'px '+(-opt.suiPos[index][1])+'px'
                }).animate({
                    left:opt.suiPos[index][0],
                    top:opt.suiPos[index][1]
                },opt.boxTime);
            }).last().queue(function(){
                that.start();
                $(this).dequeue();
            });
        },
        random:function(s,e){
            return Math.random()*s-e;
        }
    } 
})(); 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • jQuery中 bind的用法简单介绍

    jQuery中 bind的用法简单介绍

    bind() 方法为被选元素添加一个或多个事件处理程序,并规定事件发生时运行的函数。这篇文章主要介绍了jQuery中的 bind用法,需要的朋友可以参考下
    2017-02-02
  • 基于JQUERY的两个ListBox子项互相调整的实现代码

    基于JQUERY的两个ListBox子项互相调整的实现代码

    基于JQUERY的两个ListBox子项互相调整的实现代码,需要的朋友可以参考下。
    2011-05-05
  • jquery json 实例代码

    jquery json 实例代码

    jquery json 实例代码,需要的朋友可以参考下。
    2010-12-12
  • jquery点击改变class并toggle的实现代码

    jquery点击改变class并toggle的实现代码

    下面小编就为大家带来一篇jquery点击改变class并toggle的实现代码。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-05-05
  • jQuery的事件委托实例分析

    jQuery的事件委托实例分析

    本文通过具体的实例向我们简单介绍了jQuery的事件委托的实现方式,十分的简单实用,有需要的小伙伴可以参考下。
    2015-07-07
  • jQuery仿写百度百科的目录树

    jQuery仿写百度百科的目录树

    这篇文章主要介绍了jQuery仿写百度百科的目录树医保词条样式,点击右侧的目录树,左侧跳转到指定的锚点位置,滚动鼠标,游标跟随一起滚动至响应链接位置,具体实现思路大家参考下本文
    2017-01-01
  • jquery插件实现扫雷游戏(1)

    jquery插件实现扫雷游戏(1)

    这篇文章主要为大家详细介绍了jquery插件实现扫雷游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04
  • jQuery多条件筛选如何实现

    jQuery多条件筛选如何实现

    这篇文章主要介绍了jquery实现多条件筛选特效,推荐给大家,有需要的小伙伴可以参考下。
    2015-11-11
  • jQuery使用hide方法隐藏元素自身用法实例

    jQuery使用hide方法隐藏元素自身用法实例

    这篇文章主要介绍了jQuery使用hide方法隐藏元素自身用法,实例分析了jQuery中hide方法的原理与使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • jQuery pjax 应用简单示例

    jQuery pjax 应用简单示例

    此篇文章给大家分享了jQuery pjax 应用的一些简单示例,希望可以帮助到大家
    2018-09-09

最新评论