利用jQuery实现小盒子菜单效果



12 46 16



特效描述:利用jQuery实现 小盒子 菜单效果,利用jQuery实现小盒子菜单效果

代码结构

1. 引入CSS

<link rel="stylesheet" href="css/style.css" type="text/css" media="screen"/>

2. 引入JS

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.easing.1.3.js"></script>

3. HTML代码

        <div class="title">
            <h1>Little Boxes Menu with jQuery</h1>
        </div>
        <div id="content">
            <div class="reference">
                <p><a href="http://www.51qianduan.com//photos/adforce1/">Photos from williamcho's photostream on Flickr</a></p>
            </div>
            <a class="back" href="http://tympanus.net/codrops/2010/05/18/little-boxes-menu-with-jquery">Back to Codrops</a>
            <div id="littleBoxes" class="littleBoxes">
                <div class="boxlink bg1" style="top:0px;left:0px;">
                    <a href="">About</a>
                    <div class="boxcontent">
                        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, 
                            sed do eiusmod tempor incididunt ut labore et dolore magna
                            aliqua. Ut enim ad minim veniam, quis nostrud exercitation
                            ullamco laboris nisi ut aliquip ex ea commodo consequat.
                        </p>
                    </div>
                </div>
                <div class="bg5" style="background-position:-90px 0;top:0px;left:95px;"></div>
                <div class="bg5" style="background-position:-180px 0;top:0px;left:190px;"></div>
                <div class="bg5" style="background-position:-270px 0;top:0px;left:285px;"></div>
                <div class="bg5" style="background-position:0 -90px;top:95px;left:0px;"></div>
                <div class="boxlink bg2" style="top:95px;left:95px;">
                    <a href="">Menu</a>
                    <div class="boxcontent">
                        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
                            sed do eiusmod tempor incididunt ut labore et dolore magna
                            aliqua. Ut enim ad minim veniam, quis nostrud exercitation
                            ullamco laboris nisi ut aliquip ex ea commodo consequat.
                        </p>
                    </div>
                </div>
                <div class="bg5" style="background-position:-180px -90px;top:95px;left:190px;"></div>
                <div class="bg5" style="background-position:-270px -90px;top:95px;left:285px;"></div>
                <div class="bg5" style="background-position:0 -180px;top:190px;left:0px;"></div>
                <div class="bg5" style="background-position:-90px -180px;top:190px;left:95px;"></div>
                <div class="boxlink bg3" style="top:190px;left:190px;">
                    <a href="">Chef</a>
                    <div class="boxcontent">
                        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
                            sed do eiusmod tempor incididunt ut labore et dolore magna
                            aliqua. Ut enim ad minim veniam, quis nostrud exercitation
                            ullamco laboris nisi ut aliquip ex ea commodo consequat.
                        </p>
                    </div>
                </div>
                <div class="bg5" style="background-position:-270px -180px;top:190px;left:285px;"></div>
                <div class="bg5" style="background-position:0 -270px;top:285px;left:0px;"></div>
                <div class="bg5" style="background-position:-90px -270px;top:285px;left:95px;"></div>
                <div class="bg5" style="background-position:-180px -270px;top:285px;left:190px;"></div>
                <div class="boxlink bg4" style="top:285px;left:285px;">
                    <a href="">Contact</a>
                    <div class="boxcontent">
                        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
                            sed do eiusmod tempor incididunt ut labore et dolore magna
                            aliqua. Ut enim ad minim veniam, quis nostrud exercitation
                            ullamco laboris nisi ut aliquip ex ea commodo consequat.
                        </p>
                    </div>
                </div>
            </div>
        </div>
        <!-- The JavaScript -->
       <script type="text/javascript">
            $(function() {
                /* object to save the initial positions of each box */
                var divinfo = {"initial": []};
                /* index of the selected / clicked box */
                var current = -1;
                /* we save the index,top and left of each one of the boxes */
                $('#littleBoxes > div').each(function(){
                    var $this = $(this);
                    var initial = {
                                'index' : $this.index(),
                                'top'     : $this.css('top'),
                                'left'     : $this.css('left')
                    };
                    divinfo.initial.push(initial);
                });
                /* clcik event for the anchors inside of the boxes */
                $('#littleBoxes a').bind('click',function(e){
                        var $this         = $(this);
                        var $currentBox    = $this.parent();
                        /* set a z-index lower than all the other boxes,
                        to see the other boxes animation on the top*/
                        $currentBox.css('z-index','1');
                        /* if we are clicking on a expanded box : */
                        if(current == $currentBox.index()){
                            /* put it back (decrease width,height, and set the top and left like it was before).
                            the previous positions are saved in the divinfo obj*/
                            $currentBox.stop().animate({
                                    'top'         : divinfo.initial[$currentBox.index()].top,
                                    'left'        : divinfo.initial[$currentBox.index()].left,
                                    'width'     : '90px',
                                    'height'    : '90px'
                            },800,'easeOutBack').find('.boxcontent').fadeOut();
                            $('#littleBoxes > div').not($currentBox).each(function(){
                                var $ele         = $(this);
                                var elemTop     = divinfo.initial[$ele.index()].top;
                                var elemLeft     = divinfo.initial[$ele.index()].left;
                                $ele.stop().show().animate({
                                    'top'         : elemTop,
                                    'left'        : elemLeft,
                                    'opacity'    : 1
                                },800);
                            });
                            current = -1;
                        }
                        /* if we are clicking on a small box : */
                        else{
                            /* randomly animate all the other boxes.
                            Math.floor(Math.random()*601) - 150 gives a random number between -150 and 450.
                            This range is considering the initial lefts/tops of the elements. It's not the exact right
                            range, since we would have to calculate the range based on each one of the boxes. Anyway, it
                            fits our needs...
                            */
                            $('#littleBoxes > div').not($currentBox).each(function(){
                                var $ele = $(this);
                                $ele.stop().animate({
                                    'top' : (Math.floor(Math.random()*601) - 150) +'px',
                                    'left': (Math.floor(Math.random()*601) - 150) +'px',
                                    'opacity':0
                                },800,function(){
                                    $(this).hide();
                                });
                            });
                            /* expand the clicked one. Also, fadeIn the content (boxcontent)
                            if you want it to fill the space of the littleBoxes container,
                            then these are the right values */
                            var newwidth     = 379;
                            var newheight     = 379;
                            $currentBox.stop().animate({
                                'top'     : '0px',
                                'left'    : '0px',
                                'width' : newwidth +'px',
                                'height': newheight+'px'
                            },800,'easeOutBack',function(){
                                current = $currentBox.index();
                                $(this).find('.boxcontent').fadeIn();
                            });
                        }
                        e.preventDefault();
                });
            });
        </script>



用户评论
大牛,别默默的看了,快登录帮我点评一下吧!:)      登录 | 注册


热门标签: 滚动导航菜单 滚动菜单 滚动导航 滑动导航菜单 滑动导航 滑动菜单 浮动菜单 浮动导航 导航切换 菜单切换 右键菜单 收缩菜单 收缩导航 h5动画导航 h5动画菜单 html5动画导航 html5动画菜单 flash导航菜单 flash导航条 flash导航 flash菜单 flash下拉菜单 flash下拉导航菜单 导航菜单 导航条 菜单栏 导航栏 分类导航 响应式导航条 响应式导航 固定导航 固定菜单
×
×

注册

官方QQ群

扫描上面二维码加微信群

官方QQ群

jQuery/js讨论群
群号:642649996
Css3+Html5讨论群
群号:322131262

加群请备注:从官网了解到