Jquery和CSS pin-unpin侧边栏的问题

时间:2013-09-10 10:43:39

标签: javascript jquery html css sidebar

我创建了一个侧面板。 这允许我打开侧栏的 pin 取消固定。当我们悬停在侧边栏上时,侧边栏会打开。然后单击 pin 图像将其固定。现在我尝试打开侧栏 onClick ,并想要删除 onHover 。我该怎么做呢而且我还想添加一个功能,即当我点击打开侧边栏的框时,默认情况下会打开它。所以我不需要钉它。因此,打开侧边栏后,每当我点击侧边栏时都不会关闭。对于侧边栏,我必须点击我们用于打开的框。

这是我的代码

HTML

<ul id="dock">
            <li id="files">
                <ul class="free">
                    <li class="header"><a href="javascript:void(0);" class="dock"><IMG SRC="https://cdn2.iconfinder.com/data/icons/snipicons/500/pin-128.png" WIDTH="16" HEIGHT="16" BORDER="0" ALT="Dock"  style = "padding-top: 12px;"></a><a href="#" class="undock"><IMG SRC="https://cdn2.iconfinder.com/data/icons/oxygen/48x48/actions/note2.png" WIDTH="16" HEIGHT="16" BORDER="0" ALT=""  style = "padding-top: 12px;"></a><H5 ID="colorgreen">DISCOVER </h4></li>
                    <div id="accordion">
                      <h3>Section 1</h3>
                      <div class = "accordionheight">
                        <p>
                        accordion 1 content
                        </p>
                      </div>
                      <h3>Section 2</h3>
                      <div class = "accordionheight">
                        <p>
                        accordion 2 content
                        </p>
                      </div>
                      <h3>Section 3</h3>
                      <div class = "accordionheight">
                        <p>
                        accordion 3 content
                        </p>
                      </div>
                    </div>
                </ul>
            </li>

            <li id="tools">
                <ul class="free">
                    <li class="header"><a href="#" class="dock"><IMG SRC="https://cdn2.iconfinder.com/data/icons/snipicons/500/pin-128.png" WIDTH="16" HEIGHT="16" BORDER="0" ALT="Dock"></a><a href="#" class="undock"><IMG SRC="https://cdn2.iconfinder.com/data/icons/oxygen/48x48/actions/note2.png" WIDTH="16" HEIGHT="16" BORDER="0" ALT="Undock"></a><H5 ID="colorgreen">FACT FILE</H5></li>
                    <li><a href="#">This is one item</a></li>
                    <li><a href="#">This is one item</a></li>
                    <li><a href="#">This is one item</a></li>
                    <li><a href="#">This is one item</a></li>
                    <li><a href="#">This is one item</a></li>
               </ul>
            </li>
        </ul>

JS

            $(document).ready(function(){
            var docked = 0;

            $("#dock li ul").height($(window).height());

            $("#dock .dock").click(function(){
                $(this).parent().parent().addClass("docked").removeClass("free");

                docked += 1;
                var dockH = ($(window).height()) / docked
                var dockT = 0;               

                $("#dock li ul.docked").each(function(){
                $(this).height(dockH).css("top", dockT + "px");
                dockT += dockH;
                });
                $(this).parent().find(".undock").show();
                $(this).hide();

                if (docked > 0)
                $("#content").css("margin-left","250px");
                else
                $("#content").css("margin-left", "60px");
            });

            $("#dock .undock").click(function(){
                $(this).parent().parent().addClass("free").removeClass("docked")
                .animate({right:"-80px"}, 200).height($(window).height()).css("top", "0px");

                docked = docked - 1;
                var dockH = ($(window).height()) / docked
                var dockT = 0;               

                $("#dock li ul.docked").each(function(){
                $(this).height(dockH).css("top", dockT + "px");
                dockT += dockH;
                });
                $(this).parent().find(".dock").show();
                $(this).hide();

                if (docked > 0)
                $("#content").css("margin-left", "40px");
                else
                $("#content").css("margin-left", "80px");
            });

            $("#dock li").hover(function(){
                $(this).find("ul").animate({right:"40px"}, 200);
                }, function(){
                    $(this).find("ul.free").animate({right:"-80px"}, 200);
                });
            }); 

CSS

                 #dock{margin:0px; padding:0px; list-style:none; position:fixed; top:0px; height:100%; 
          z-index:9999; background-color:#f0f0f0; right:0px;}
    #dock > li {width:40px; height:8.3%; margin: 0 0 1px 0; background-color:#dcdcdc;
                 background-repeat:no-repeat; background-position:left center;}

    #dock #files {background-image:url(../images/menu.png);}
    #dock #tools {background-image:url(../images/menu.png);}

    /*#dock > li:hover {background-position:-40px 0px;}*/

    /* panels */
    #dock ul li {padding:5px; border: solid 1px #F1F1F1;}


    #dock > li:hover ul {display:block;}
    #dock > li ul {position:absolute; top:0px; right: 40px;  z-index:-1;width:180px; display:none;
                   background-color:#F1F1F1; border:solid 1px #969696; padding:0px; margin:0px; list-style:none;}
    #dock > li ul.docked { display:block;z-index:-2;}

    .dock,.undock{float:left;}
   .undock {display:none;}
    #sidepanelcontent {margin: 10px 0 0 60px;}

    #colorgreen {color:green;}

这是 JsFiddle

1 个答案:

答案 0 :(得分:1)

你想要这样的东西http://jsfiddle.net/W7sNp/1/

我添加了click个事件,并将一些代码从hover移到了它。也有点修改CSS

在单击

框之前,不会显示选项卡

编辑:

我添加了一个快速示例http://jsfiddle.net/W7sNp/3/,它忽略了所有

的悬停

HTML 是相同的

CSS

  1. :移除了悬停选择器
  2. 已更改#dock > li ul {position:absolute; top:0px; right: -40px; z-index:-1;width:0px; display:block;现在可见但已移出视图且宽度为0px
  3. <强>脚本

    所有功能都移至$("#dock li").click(),并决定按宽度

    打开或关闭标签
    $(document).ready(function(){
                var docked = 0;
    
                $("#dock li ul").height($(window).height());
    
    
                $("#dock li").click(function(){
                        var test = $(this).find("ul").css('width');
                    if (test=="0px"){
                        $(this).find("ul").addClass("docked").removeClass("free").animate({right:"40px",width:'180px'}, 200);
                        docked += 1;
    
                    }else{
                        $(this).find("ul").addClass("free").removeClass("docked").animate({right:"-40px",width:'0px'}, 200);
                        docked = docked - 1;
                    }
                    console.log(docked);
    
    
    
                    var dockH = ($(window).height()) / docked;
                    var dockT = 0;               
    
                    $("#dock li ul.docked").each(function(){
                    $(this).height(dockH).css("top", dockT + "px");
                    dockT += dockH;
                    });
    
    
                    if (docked > 0)
                    $("#content").css("margin-left","250px");
                    else
                    $("#content").css("margin-left", "60px");
                    });
                });