jquery版相片墙(鼠标控制图片聚合和散开)

原文:jquery版相片墙(鼠标控制图片聚合和散开)

  照片墙,简单点说就是鼠标点击小图片时,聚合变成一张大图片;点击大图片时,散开变成小图片。这个是我一年前无意间看到的动画效果(现在已经忘记是哪位大神制作的了,引用了他的图片),刚看到这个很炫的动画超级激动,哇!怎么可以这么牛!我制作出来的没那边炫,但是还是制作出来了,算是对我的一种激励!希望能有碰到问题就要解决它的精神,即使不是现在但会是不久的将来!

一、演示效果

  散开状态:

  聚合状态:

二、html代码

<div class="box">
    <div><img src="images/thumbs/1.jpg"/></div>
    <div><img src="images/thumbs/2.jpg"/></div>
    <div><img src="images/thumbs/3.jpg"/></div>
    <div><img src="images/thumbs/4.jpg"/></div>
    <div><img src="images/thumbs/5.jpg"/></div>
    <div><img src="images/thumbs/6.jpg"/></div>
    <div><img src="images/thumbs/7.jpg"/></div>
    <div><img src="images/thumbs/8.jpg"/></div>
    <div><img src="images/thumbs/9.jpg"/></div>
    <div><img src="images/thumbs/10.jpg"/></div>
    <div><img src="images/thumbs/11.jpg"/></div>
    <div><img src="images/thumbs/12.jpg"/></div>
    <div><img src="images/thumbs/13.jpg"/></div>
    <div><img src="images/thumbs/14.jpg"/></div>
    <div><img src="images/thumbs/15.jpg"/></div>
    <div><img src="images/thumbs/16.jpg"/></div>
    <div><img src="images/thumbs/17.jpg"/></div>
    <div><img src="images/thumbs/18.jpg"/></div>
    <div><img src="images/thumbs/19.jpg"/></div>
    <div><img src="images/thumbs/20.jpg"/></div>
    <div><img src="images/thumbs/21.jpg"/></div>
    <div><img src="images/thumbs/22.jpg"/></div>
    <div><img src="images/thumbs/23.jpg"/></div>
    <div><img src="images/thumbs/24.jpg"/></div>
</div>
<div class="alter">
    <div class="prev"><img src="images/prev.png"/></div>
    <div class="next"><img src="images/next.png"/></div>
</div>

 

 

三、css代码

    <style type="text/css">
        body {background: url("images/bg.jpg") left top; overflow: hidden;}
        .box {position: relative;}
        .box div {position: absolute; width: 125px; height: 125px; border: 1px solid #AAAAAA; box-shadow: 0 0 6px rgba(0,0,0,0.6); border-radius: 3px; cursor: pointer;}
        .box div img {margin: 5px; width: 115px;}
        .box div.curr {box-shadow: 1px 1px 3px #000000; border: none; border-radius: 0; }
        .box div.curr img {margin: 0; width: 125px;}
        .alter div {position: absolute; top: 50%; margin-top: -25px; width: 50px; height: 50px; line-height: 50px; border: 1px solid #AAAAAA; box-shadow: 0 0 3px rgba(0,0,0,0.6); text-align: center; cursor: pointer; background: white; opacity: 0.9; filter: Alpha(opacity=90);}
        .alter div img {*display: block; *margin-top: 15px;}
        .alter .prev {left: -52px;}
        .alter .next {right: -52px;}
    </style>

 

四、js代码

图片散开事件:

        /* 图片散开 */
        scatter: function(){
            windowWidth = $(window).width();
            windowHeight = $(window).height();
            leftDistance = (windowWidth - itemWidth * colCount)/7; /* left左边间距 */
            topDistance = (windowHeight - itemHeight * rowCount) / 5; /* top上部间距 */
            $item.each(function(){
                var _index = $(this).index();
                col = _index % colCount; /* 第几列,从0开始 */
                row = Math.floor(_index / colCount); /* 第几行 */
                cssLeft = (col  + 1) * leftDistance + col  * itemWidth; /* css中left位置设置 */
                cssTop = (row + 1) * topDistance + row * itemHeight;/* css中top位置设置 */
                $(this).stop().animate({"left": cssLeft, "top": cssTop},1000);
                var cssRotate = itemRotate();
                $(this).css({"transform": "rotate("+cssRotate+"deg)"})
            })
            $prev.stop().animate({"left": "-52px"});
            $next.stop().animate({"right": "-52px"});
        },

分析:

  这个事件的作用是点击大图片时,图片散开变成多个小图片,是通过控制图片父级position的left和top值。

  散开状态,首先我想到的是,要显示多少行和多少列(也就是每行显示多少张图片)?行和列有什么关系?如果说我每行显示6张图片(也就是6列),那会有多少行?最后发现,原来可以用图片的总个数除以列数再用Math.floor()取值(从第0列开始)得到总行数。好,到这里,我就知道图片要显示多少行和多少列了。现在要判断每张图片属于第几行第几列,这个要靠自己找规律,0-5为第一行,6-11为第二行,……,可得出行数 row = Math.floor(_index / colCount)(第0行开始),0、6、12、6*n为第一列,1、7、13、6*n+1为第二列,……,可得出列数 col = _index % colCount(第0列开始)。再考虑,图片之前的距离要多少会合适一点。我这里采取一个笨方法,我就规定图片间的距离和最外围图片离窗口边沿的距离一样(硬性规定了哈),也就是下图所示:

这样位置排布就考虑好了。

  那么接下来就是考虑图片怎么定位了,我先考虑top值,也就是距离窗口顶部的距离,先求出topDistance = (windowHeight - itemHeight * rowCount) / 5; 的值(假如是4行),第一行距离顶部的位置是topDistance,第二行距离顶部的位置topDistance * 2 + 上面图片的高度,第三行距离顶部的位置是 topDistance * 3 + 上面图片的高度 *2,依次类推可得出:cssTop = (row + 1) * topDistance + row * itemHeight。现在考虑left值,方法和top定位类似,先得出eftDistance = (windowWidth - itemWidth * colCount)/7(6列),……再推出cssLeft = (col + 1) * leftDistance + col * itemWidth。到这里,图片散开事件就解决了,里面的细节我就不一一考虑了。

图片聚合事件:

        /* 图片聚合 */
        together: function(url){
            windowWidth = $(window).width();
            windowHeight = $(window).height();
            targetWidth = windowWidth / 2; /* 以中心宽度为基准 */
            targetHeight = windowHeight / 2; /* 以中心高度为基准 */
            $item.each(function(){
                var _index = $(this).index();
                col = _index % colCount; /* 第几列,从0开始 */
                row = Math.floor(_index / colCount); /* 第几行 */
                cssLeft = targetWidth - halfCol * itemWidth + col * itemWidth;
                cssTop = targetHeight - halfRow * itemHeight + row * itemHeight;
                imgLeft = -col * itemWidth; /* 取背景图片水平位置 */
                imgTop = -row * itemHeight; /* 取背景图片垂直位置 */
                $(this).stop().animate({"left": cssLeft, "top": cssTop},1000);
                $(this).css({"transform": "rotate(0deg)","background": "url("+url+") no-repeat "+imgLeft+"px "+imgTop+"px"})
            })
            $prev.stop().animate({"left": "0"});
            $next.stop().animate({"right": "0"});
        },

分析:

  这个事件的主要是点击小图片,图片聚拢变成一张大图片,并且大图片时可切换的。这里我要说明的是,图片聚拢后该怎么定位,大图是由n个小图片的背景图片组成。

  图片聚拢定位,这个其实不难,先想想聚拢后会呈现什么状态,大图是居中显示的!那就是说明以窗口中心为基准,左右两边一边三张图片。那第一张小图片是在哪个位置?第二张在哪个位置?……这个还不简单,第一张图片位置可以用窗口宽度的一半减去3张图片宽度,第二张是窗口宽度一半减去3张图片+第一张图片(第0列),……类推得出:cssLeft = targetWidth - halfCol * itemWidth + col * itemWidth,好,这里位置,图片定位就解决了,cssTop类似,这里就不再说明了。

  小图片背景定位,这个其实更简单,第一张图片定位是0,0, 第二张图片是-125px,0,第三张图片是-250px,0,……第7张图片是0,-125px,第八张图片是-125px,-125px……,推出结果:imgLeft = -col * itemWidth; imgTop = -row * itemHeight;

js总代码:

$(function(){
    var $item = $(".box div"),
            $prev = $(".prev"),
            $next = $(".next"),
            windowWidth, /* 窗口宽度 */
            windowHeight, /* 窗口高度 */
            itemWidth = $item.width(), /* 图片父级div的宽度 */
            itemHeight = $item.height(), /* 图片父级div的高度 */
            leftDistance, /* left图片左边间距 */
            topDistance,/* top图片上边间距 */
            targetWidth,/* 以中心宽度为基准 */
            targetHeight,/* 以中心高度为基准 */
            col,/* 第几列,从0开始 */
            row,/* 第几行 */
            cssLeft,/* css中left位置设置 */
            cssTop,/* css中top位置设置 */
            imgLeft,/* 取背景图片水平位置 */
            imgTop,/* 取背景图片垂直位置 */
            colCount = 6, /* 多少列 */
            rowCount = Math.ceil($item.length / colCount), /* 多少行 */
            halfCol = colCount / 2, /* 图片聚合时,以窗口中心位置为基准,左边排列的图片个数 */
            halfRow = rowCount / 2, /* 图片聚合时,以窗口中心位置为基准,上边排列的图片个数 */
            flag = false, /* 判断图片时聚合还是散开,false为聚合,true为散开 */
            page = 0; /* 按钮切换时为第几张图片 */

    /* 事件控制 */
    var eventFunc = {
        /* 图片散开 */
        scatter: function(){
            windowWidth = $(window).width();
            windowHeight = $(window).height();
            leftDistance = (windowWidth - itemWidth * colCount)/7; /* left左边间距 */
            topDistance = (windowHeight - itemHeight * rowCount) / 5; /* top上部间距 */
            $item.each(function(){
                var _index = $(this).index();
                col = _index % colCount; /* 第几列,从0开始 */
                row = Math.floor(_index / colCount); /* 第几行 */
                cssLeft = (col  + 1) * leftDistance + col  * itemWidth; /* css中left位置设置 */
                cssTop = (row + 1) * topDistance + row * itemHeight;/* css中top位置设置 */
                $(this).stop().animate({"left": cssLeft, "top": cssTop},1000);
                var cssRotate = itemRotate();
                $(this).css({"transform": "rotate("+cssRotate+"deg)"})
            })
            $prev.stop().animate({"left": "-52px"});
            $next.stop().animate({"right": "-52px"});
        },
        /* 图片聚合 */
        together: function(url){
            windowWidth = $(window).width();
            windowHeight = $(window).height();
            targetWidth = windowWidth / 2; /* 以中心宽度为基准 */
            targetHeight = windowHeight / 2; /* 以中心高度为基准 */
            $item.each(function(){
                var _index = $(this).index();
                col = _index % colCount; /* 第几列,从0开始 */
                row = Math.floor(_index / colCount); /* 第几行 */
                cssLeft = targetWidth - halfCol * itemWidth + col * itemWidth;
                cssTop = targetHeight - halfRow * itemHeight + row * itemHeight;
                imgLeft = -col * itemWidth; /* 取背景图片水平位置 */
                imgTop = -row * itemHeight; /* 取背景图片垂直位置 */
                $(this).stop().animate({"left": cssLeft, "top": cssTop},1000);
                $(this).css({"transform": "rotate(0deg)","background": "url("+url+") no-repeat "+imgLeft+"px "+imgTop+"px"})
            })
            $prev.stop().animate({"left": "0"});
            $next.stop().animate({"right": "0"});
        },
        /* 判断图片为散开 */
        judgeScatter: function(){
            $item.removeClass("curr").find("img").show();
            $item.css({"background":"none"});
            eventFunc.scatter();
        },
        /* 判断图片为聚合 */
        judgeTogether: function(url){
            $item.addClass("curr").find("img").hide();
            eventFunc.together(url);
        }
    }

    /* 图片偏转角度 */
    function itemRotate(){
        return 20 - Math.random() * 40;
    }

    eventFunc.scatter(); /* 初始化,图片为散开状态 */
    $(window).resize(function(){
        var _url = $item.eq(page).find("img").attr("src").replace("thumbs/","");
        if(!flag){
            eventFunc.judgeScatter();
        }else {
            /* 改变窗口大小,当为聚合状态时,还是保持聚合状态,和点点击图片判断相反的原因是,下面为聚合状态后给flag赋值为true,所以当为true时就是聚合状态,散开类似判断 */
            eventFunc.judgeTogether(_url);
        }
    })

    /* 点击图片 */
    $item.click(function(){
        var _url = $(this).find("img").attr("src").replace("thumbs/","");
        page = $(this).index();
        if(flag){
            eventFunc.judgeScatter();
            flag = false;
        }else {
            eventFunc.judgeTogether(_url);
            flag = true;
        }
    })

    /* 上一页 */
    $prev.click(function(){
        if(page > 0){
            page--;
            var _url = $item.eq(page).find("img").attr("src").replace("thumbs/","");
            eventFunc.together(_url);

        }
    })

    /* 下一页 */
    $next.click(function(){
        if(page < $item.length - 1){
            page++;
            var _url = $item.eq(page).find("img").attr("src").replace("thumbs/","");
            eventFunc.together(_url);

        }
    })

})

   还有很多小细节我就不一一讲解了,如果有哪里说的不好或者不对的地方往指正,谢谢~

时间: 2024-08-01 23:11:50

jquery版相片墙(鼠标控制图片聚合和散开)的相关文章

jquery插件实现鼠标经过图片右侧显示大图的效果(类似淘宝)_jquery

这个插件的名字elevatezoom,网址为http://www.elevateweb.co.uk/image-zoom,在github上的项目首页为https://github.com/elevateweb/elevatezoom,建议去github下载,这个网速比较快. 实现这个效果你需要准备两张图片,一张小的,一张大用于鼠标经过时候显示.然后我们只要为img标签添加data-zoom-image属性,其值为大图的地址,最后在javascript中选择该图片调用elevateZoom()就可

ImageFlow可鼠标控制图片滚动_图象特效

You are free to use this in any product, or on any web site. For more information about ImageFlow read the Documentation and check my Newsblog. For anything else simply drop me a line in my Shoutbox. ChangeLog Version 0.8  Added Safari 1.x compatibil

jquery版小型婚礼(可动态添加祝福语)

原文:jquery版小型婚礼(可动态添加祝福语) 前两天在网上不小心看到"js许愿墙"这几个字,我的神经就全部被调动了.然后就开始我的百度生涯,一直寻觅许愿墙背景图片和便利贴图片,觅了好久--一直没找到满意的--无意间看到祝福语和一些卡通婚礼图片.最终我决定用jquery制作一个小型婚礼,并且实现添加祝福语的功能. 音乐响起来,开始我的婚礼进行曲~ 一.婚礼演示图 场景一: 场景二: 场景三: 场景四: 场景五: 场景六: 场景七: 添加祝福语: 二.html代码 <div cl

JQuery悬停控制图片轮播——代码简单_jquery

jquery实现的鼠标悬停图片自动轮播效果,当把鼠标悬停到图片时,图像就会不断循环播放,速度非常快,效果非常逼真,就和在放武侠片一样,使用了jquery实现,下面小编给大家分析jq悬停控制图片轮播,请看小面的效果图. 在线预览             源码下载 具体实现的代码如下: <!-- 轮播广告 --> <div id="banner_tabs" class="flexslider"> <ul class="slides

javascript控制图片播放的实现代码_javascript技巧

一般实现用鼠标控制图片的滚动效果都比较麻烦,大段大段的代码让新手头疼无从下手,下面我来写个简单的javascript控制图片滚动的效果.代码简洁明了,兼容ie.firefox和google浏览器. 分享代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

jQuery实现鼠标滑过链接控制图片的滑动展开与隐藏效果_jquery

本文实例讲述了jQuery实现鼠标滑过链接控制图片的滑动展开与隐藏效果.分享给大家供大家参考,具体如下: 这里演示jQuery实现鼠标移动到链接上,滑动展开/隐藏图片效果,鼠标放在"上一页""下一页"上,立即浮现出所对应的图片,有点提前预览的味道,让用户知道下一页或下一页的大致内容,很好的提升了用户体验. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/jquery-mouse-over-link-pic-show-

JQuery控制图片由中心点逐渐放大效果_jquery

有的时候我们需要做一个当鼠标放置在图片上的时候,希望图片逐渐变大,即图片的width和height逐渐变大,但是此时,其left值与top值没有改变,故看似不是从中心点进行缩放的.如下图: 从中心点进行缩放 实现代码如下: <meta charset="utf-8"> <style type="text/css"> #div1{ width:600px; height:400px; margin:50px auto; position:rel

JQuery实现鼠标移动图片显示描述层的方法

  本文实例讲述了JQuery实现鼠标移动图片显示描述层的方法.分享给大家供大家参考.具体如下: 这里可结合 JQuery easing 的动画来配合使用. 主要代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 $(".item").hover( function() { //$(this).children("img").stop().animate({opacity: 0.8}, 700, "easeInSine")

jQuery实现鼠标经过图片变亮其他变暗效果

  jQuery实现的仿商城图片变亮变暗效果,鼠标悬停图片之后该图片变亮,其他图片变暗.移开鼠标所有图片变亮,兼容主流浏览器,适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗,有需要的小伙伴可以参考下. 以下是完整源代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39