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

标签 javascript jquery html css sidebar

我创建了一个侧面板。 这让我可以固定取消固定打开侧边栏。当我们悬停在它上面时,侧边栏打开。并使用点击固定图像固定它。现在我尝试打开边栏 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

最佳答案

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

我添加了 click 事件并将一些代码从 hover 移动到它。还稍微修改了 CSS

在您单击框之前不会显示选项卡

编辑:

我添加了一个快速示例 http://jsfiddle.net/W7sNp/3/它完全忽略悬停

HTML 是一样的

CSS

  1. :悬停选择器被移除
  2. 更改了#dock > li ul {position:absolute;顶部:0px;右:-40px; z-index:-1;宽度:0px; display:block; 现在它是可见的但移出 View 并且宽度为 0px

脚本

所有功能都移到了 $("#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");
                });
            }); 

关于javascript - 侧边栏的 Jquery 和 CSS pin-unpin 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18716797/

相关文章:

JavaScript : How to check external javascript

javascript - 在 Javascript for 循环中弹出数组未获取所有数组项

javascript - jquery 的 $.ajax 成功中的 'this' 指的是什么?

html - 如何让不旋转的文本跟随旋转的 div?

html - 使 DIV 可点击

python - Beautiful Soup 4 CSS 选择器的工作方式与教程显示的不同

javascript - 使跟踪代码从 URL 栏中消失

javascript - ng-disabled 覆盖自定义指令行为

jquery - 将 animation.css 中的 zoomIn 和 zoomout 集成到 Jquery Mobile,并转换 lightSpeedIn 和 lightSpeedout

javascript - 在图标状态更改时更改图像 :Jquery