纸头折飞机
欢迎大家加入KitJs官方高级QQ群88093625,讨论前端技术,上海携程招聘H5,iOS,android,产品,设计,交互,测试,有意者发简历到xueduanyang1985@163.com

Demo地址http://xueduany.github.com/KitJs/KitJs/index.html#lightbox

效果图如下

image

image

 

实现原理很简单,基本的html如下

<div id="gallery">
            <div class="item">
                <a class="kitLightBox" href="img/original/blue-green-nature.jpg" target="_blank"><img src="img/thumbs/blue-green-nature.jpg"></a>
                <div class="desc">
                    blue-green-nature
                </div>
            </div>
            <div class="item">
                <a class="kitLightBox" href="img/original/2-breast-stroke.jpg" target="_blank"><img src="img/thumbs/2-breast-stroke.jpg"></a>
                <div class="desc">
                    2-breast-stroke
                </div>
            </div>
            <div class="item">
                <a class="kitLightBox" href="img/original/farm.jpg" target="_blank"><img src="img/thumbs/farm.jpg"></a>
                <div class="desc">
                    farm
                </div>
            </div>
            <div class="item">
                <a class="kitLightBox" href="img/original/bahnhoff.jpg" target="_blank"><img src="img/thumbs/bahnhoff.jpg"></a>
                <div class="desc">
                    bahnhoff
                </div>
            </div>

            ……

 

先定义一块桌布,也就是div id=”gallery”

然后按照顺序,排列一竖列相册,div class=”item”,里面用一个a链接把图片包起来

 

接下来,我们要实现相片的分散效果,

$k(function() {
                    $k('.item', $k('#gallery')).each(function() {
                        $k(this).css({
                            top : $kit.math.rand($k('#gallery').innerHeight()) + 'px',
                            left : $kit.math.rand($k('#gallery').innerWidth()) + 'px',
                            '-webkit-transform' : 'rotate(' + $kit.math.rand(-40, 40) + 'deg)'
                        });
                    })

这里的是$k是kit的写法,类似jQuery的$,API完全一样,这段代码的意思是找到所有item的div,设置为绝对定位,用桌布高宽,生成随机数,排列,对于css3,使用css3特有的旋转效果'rotate属性,旋转一定角度

 

这个时候,相片就开始分散开了,达到了图1的效果,接下来我们要做一下LightBox的效果,

$k(function() {
                    $k('.item', $k('#gallery')).each(function() {
                        $k(this).css({
                            top : $kit.math.rand($k('#gallery').innerHeight()) + 'px',
                            left : $kit.math.rand($k('#gallery').innerWidth()) + 'px',
                            '-webkit-transform' : 'rotate(' + $kit.math.rand(-40, 40) + 'deg)'
                        });
                    }).pushStack('a.kitLightBox').each(function() {
                        new $kit.ui.LightBox({
                            el : this
                        }).init();
                    });
                });

完整代码如上,对于每个图片的a链接,使用kitjs的lightbox UI widget实例化,得到的效果就能点击,动画效果的打开图片大图了。^_^祝各位使用愉快!

LightBox源代码https://github.com/xueduany/KitJs/blob/master/KitJs/src/js/widget/LightBox/lightbox.js

相片分散效果源代码https://github.com/xueduany/KitJs/blob/master/KitJs/demo/Lightbox-Gallery/demo.html

posted on 2012-04-18 19:10  薛端阳  阅读(5524)  评论(5编辑  收藏  举报