【问题标题】:How to get sidebar that is resizeable and collapsible?如何获得可调整大小和可折叠的侧边栏?
【发布时间】:2018-07-12 17:45:58
【问题描述】:

我正在尝试实现侧导航栏,它可以调整大小并且在栏上有一个按钮,即单击按钮时侧导航应该打开,然后再次单击应该关闭的按钮。 我无法在栏上实现按钮部分。 你能帮帮我吗..

<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="utf-8" />
<title>AngularJS Navigation</title>
<script>document.write('<base href="' + document.location + '" />');
</script>
<link href="style.css" rel="stylesheet" />
<script src="http://code.jquery.com/jquery-2.0.3.min.js" data-semver="2.0.3" data-require="jquery"></script>
<script data-semver="1.2.13" src="http://code.angularjs.org/1.2.13/angular.js" data-require="angular.js@1.2.x"></script>
<script src="app.js"></script>
<script src="resizer.js"></script>

</head>

  <body ng-controller="MainCtrl">

  <div id="topnav">Top navbar</div>

  <div id="sidebar">
    <h3>Side Navbar</h3>
  </div>

  <div id="content">

    <div id="top-content">Top content <p>{{content}}</p></div>

    <div id="content-resizer" 
        resizer="horizontal" 
        resizer-height="6" 
        resizer-top="#top-content" >
    </div>

  </div>

  <div id="sidebar-resizer" 
    resizer="vertical" 
    resizer-width="6" 
    resizer-left="#sidebar" 
    resizer-right="#content"
    resizer-max="100%">
   </div>

   </body>

   </html>

“app.js”的代码

  var app = angular.module('myApp', ['mc.resizer']);

app.controller('MainCtrl', function($scope) {
  $scope.content = 'Hello World';
});

“resizer.js”的代码

 angular.module('mc.resizer', []).directive('resizer', function($document) {

    return function($scope, $element, $attrs) {

        $element.on('mousedown', function(event) {
            event.preventDefault();

            $document.on('mousemove', mousemove);
            $document.on('mouseup', mouseup);
        });

        function mousemove(event) {

            if ($attrs.resizer == 'vertical') {
                // Handle vertical resizer
                var x = event.pageX;

                if ($attrs.resizerMax && x > $attrs.resizerMax) {
                    x = parseInt($attrs.resizerMax);
                }

                $element.css({
                    left: x + 'px'
                });

                $($attrs.resizerLeft).css({
                    width: x + 'px'
                });
                $($attrs.resizerRight).css({
                    left: (x + parseInt($attrs.resizerWidth)) + 'px'
                });

            } else {
                // Handle horizontal resizer
                var y = window.innerHeight - event.pageY;

                }
        }

        function mouseup() {
            $document.unbind('mousemove', mousemove);
            $document.unbind('mouseup', mouseup);
        }
    };
});

“style.css”的代码

#topnav {
    background-color: #333333;
    display: block;
    height: 35px;
    left: 0;
    position: absolute;
    right: 0;
    top: 0;
    background-color: #DDD;
}

#sidebar {
    background-color: #AEE;
    position: absolute;
    top: 35px;
    bottom: 0;
    left: 0;
    width: 200px;
    overflow: auto;
}
#content {
    position: absolute;
    top: 35px;
    bottom: 0;
    left: 205px /* 200 + 6*/;
    right: 0;
    overflow: hidden;
    color: #FFF;

}
#top-content {
    position: absolute;
    top: 0;
    bottom: 0px; /* 130 + 6 */
    left: 0;
    right: 0;
    background-color: #444;
    overflow: auto;
}

#sidebar-resizer {
    background-color: #666;
    position: absolute;
    top: 35px;
    bottom: 0;
    left: 200px;
    width: 5px;
    cursor: e-resize;
}
#content-resizer {
    position: absolute;
    height: 6px;
    bottom: 0px;
    left: 0;
    right: 0;
    background-color: #666;
    cursor: n-resize;
}

#sidebar-resizer:hover, #preview-resizer:hover {
    background-color: #AAA;
}

【问题讨论】:

  • 请贴出一些你尝试过的JS代码。这只是一个 HTML 页面,不足以让你得到一些帮助!
  • @sjahan 我已经更新了这个问题。请看一看。感谢您帮助我。
  • @sjahan 这是一个侧导航栏
  • 首先,我设置了一个小提琴:jsfiddle.net/4dxjfxd9 第二点:你确定你的 angularJS 应用程序正在运行吗?在小提琴中,起初什么也没发生(content 范围变量未设置,我没有看到 Hello World)。我添加了ng-app 以便它启动,您确定您的应用程序正在启动吗?

标签: javascript jquery html css angularjs


【解决方案1】:

试试这个希望对用于初始化侧边栏状态的 ng-init 有所帮助

<button type="button" ng-click="toggleSideBar=!toggleSideBar">Click Me!</button>
<div id="sidebar" ng-show="toggleSideBar" ng-init="toggleSideBar=true">
    <h3>Side Navbar</h3>
</div>

【讨论】:

    【解决方案2】:

    /* Set the width of the side navigation to 250px */
    function openNav() {
        document.getElementById("mySidenav").style.width = "250px";
    }
    
    /* Set the width of the side navigation to 0 */
    function closeNav() {
        document.getElementById("mySidenav").style.width = "0";
    }
    /* The side navigation menu */
    .sidenav {
        height: 100%; /* 100% Full-height */
        width: 0; /* 0 width - change this with JavaScript */
        position: fixed; /* Stay in place */
        z-index: 1; /* Stay on top */
        top: 0; /* Stay at the top */
        left: 0;
        background-color: #111; /* Black*/
        overflow-x: hidden; /* Disable horizontal scroll */
        padding-top: 60px; /* Place content 60px from the top */
        transition: 0.5s; /* 0.5 second transition effect to slide in the sidenav */
    }
    
    /* The navigation menu links */
    .sidenav a {
        padding: 8px 8px 8px 32px;
        text-decoration: none;
        font-size: 25px;
        color: #818181;
        display: block;
        transition: 0.3s;
    }
    
    /* When you mouse over the navigation links, change their color */
    .sidenav a:hover {
        color: #f1f1f1;
    }
    
    /* Position and style the close button (top right corner) */
    .sidenav .closebtn {
        position: absolute;
        top: 0;
        right: 25px;
        font-size: 36px;
        margin-left: 50px;
    }
    
    /* Style page content - use this if you want to push the page content to the right when you open the side navigation */
    #main {
        transition: margin-left .5s;
        padding: 20px;
    }
    
    /* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
    @media screen and (max-height: 450px) {
        .sidenav {padding-top: 15px;}
        .sidenav a {font-size: 18px;}
    }
    <div id="mySidenav" class="sidenav">
      <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
      <a href="#">About</a>
      <a href="#">Services</a>
      <a href="#">Clients</a>
      <a href="#">Contact</a>
    </div>
    
    <!-- Use any element to open the sidenav -->
    <span onclick="openNav()">open</span>
    
    <!-- Add all page content inside this div if you want the side nav to push page content to the right (not used if you only want the sidenav to sit on top of the page -->
    <div id="main">
      ...
    </div>

    试试这个例子。希望这可行..如有任何问题,请在下面发表评论

    【讨论】:

    • 感谢您提供此代码,但我希望在垂直条上实现的按钮不应单独出现,即单击垂直条时应打开该条并再次单击应获得的垂直条关闭。
    【解决方案3】:

    /* When the user clicks on the button, 
    toggle between hiding and showing the dropdown content */
    function myFunction() {
        document.getElementById("myDropdown").classList.toggle("show");
    }
    
    // Close the dropdown menu if the user clicks outside of it
    window.onclick = function(event) {
      if (!event.target.matches('.dropbtn')) {
    
        var dropdowns = document.getElementsByClassName("dropdown-content");
        var i;
        for (i = 0; i < dropdowns.length; i++) {
          var openDropdown = dropdowns[i];
          if (openDropdown.classList.contains('show')) {
            openDropdown.classList.remove('show');
          }
        }
      }
    }
    /* Dropdown Button */
    .dropbtn {
        background-color: #3498DB;
        color: white;
        padding: 16px;
        font-size: 16px;
        border: none;
        cursor: pointer;
    }
    
    /* Dropdown button on hover & focus */
    .dropbtn:hover, .dropbtn:focus {
        background-color: #2980B9;
    }
    
    /* The container <div> - needed to position the dropdown content */
    .dropdown {
        position: relative;
        display: inline-block;
    }
    
    /* Dropdown Content (Hidden by Default) */
    .dropdown-content {
        display: none;
        position: absolute;
        background-color: #f1f1f1;
        min-width: 160px;
        box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
        z-index: 1;
    }
    
    /* Links inside the dropdown */
    .dropdown-content a {
        color: black;
        padding: 12px 16px;
        text-decoration: none;
        display: block;
    }
    
    /* Change color of dropdown links on hover */
    .dropdown-content a:hover {background-color: #ddd}
    
    /* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
    .show {display:block;}
    <div class="dropdown">
      <button onclick="myFunction()" class="dropbtn">Dropdown</button>
      <div id="myDropdown" class="dropdown-content">
        <a href="#">Link 1</a>
        <a href="#">Link 2</a>
        <a href="#">Link 3</a>
      </div>
    </div>

    根据您的响应式外观调整下拉按钮的高度和宽度。希望这能满足您的要求。对任何问题发表评论。

    【讨论】:

    • 不是下拉菜单,而是侧导航栏
    猜你喜欢
    • 2016-11-14
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 2019-11-13
    • 2018-03-10
    • 1970-01-01
    • 2013-06-19
    相关资源
    最近更新 更多