移动端安卓和IOS开发框架Framework7教程-侧栏

让我们看看如何添加侧栏。我们的APP可能包含两种侧栏,一个在左边,另一个在右边。我们应该在body的开始处添加侧栏的htmlbody:

  1. <body>
  2.     <!-- First, we need to add Panel's overlay that will overlays app while panel is opened -->
  3.     <div class="panel-overlay"></div>
  4.  
  5.     <!-- Left panel -->
  6.     <div class="panel panel-left">
  7.         ... panel content goes here ...
  8.     </div>
  9.  
  10.     <!-- Right panel -->
  11.     <div class="panel panel-right">
  12.         ... panel content goes here ...
  13.     </div>
  14.  
  15.     ...
  16. </body>
复制

在我们添加侧栏后,我们需要为每个侧栏选择打开效果。可能有两种效果:"Reveal"(侧栏从整个App的内容中移出)和"Cover"(侧栏覆盖App的内容)。如果您想要使用"Reveal"效果应该向侧栏添加 "panel-reveal" 类, 如果要使用"Cover"效果则添加 "panel-cover" 类:

  1. <body>
  2.     <!-- First, we need to add Panel's overlay that will overlays app while panel is opened -->
  3.     <div class="panel-overlay"></div>
  4.  
  5.     <!-- Left panel, let it be with reveal effect -->
  6.     <div class="panel panel-left panel-reveal">
  7.         ... panel content goes here ...
  8.     </div>
  9.  
  10.     <!-- Right panel, with cover effect -->
  11.     <div class="panel panel-right panel-cover">
  12.         ... panel content goes here ...
  13.     </div>
  14.  
  15.     ...
  16. </body>
复制

打开和关闭侧栏

从 HTML

Ok, 现在当我们的App里有侧栏,我们需要知道如何打开/关闭它们:

  • 为了打开侧栏,我们需要添加"open-panel" 类到任意HTML元素上(最好加到链接节点上)

    为了关闭侧栏,我们需要添加"close-panel" 类到任意HTML元素上(最好加到链接节点上)

  • 如果你的App里有两个侧栏,链接默认将打开/关闭左侧栏

  • 如果你想要指定哪个侧栏被开启/关闭,那么它可以通过HTML元素上额外的属性data-panel="left"完成。

根据以上注意点:

  1. <body>
  2.   <!-- Panels Overlay -->
  3.   <div class="panel-overlay"></div>
  4.  
  5.   <!-- Left Panel with Reveal effect -->
  6.   <div class="panel panel-left panel-reveal">
  7.     <div class="content-block">
  8.       <p>Left Panel content here</p>
  9.       <!-- Click on link with "close-panel" class will close panel -->
  10.       <p><a href="#" class="close-panel">Close me</a></p>
  11.       <!-- Click on link with "open-panel" and data-panel="right" attribute will open Right panel -->
  12.       <p><a href="#" data-panel="right" class="open-panel">Open Right Panel</a></p>
  13.     </div>
  14.   </div>
  15.  
  16.   <!-- Right Panel with Cover effect -->
  17.   <div class="panel panel-right panel-cover">
  18.     <div class="content-block">
  19.       <p>Right Panel content here</p>
  20.       <!-- Click on link with "close-panel" class will close panel -->
  21.       <p><a href="#" class="close-panel">Close me</a></p>
  22.       <!-- Click on link with "open-panel" and data-panel="left" attribute will open Left panel -->
  23.       <p><a href="#" data-panel="left" class="open-panel">Open Left Panel</a></p>
  24.     </div>
  25.   </div>
  26.  
  27.   ...
  28.   <div class="page-content">
  29.     <div class="content-block">
  30.       <!-- If no data-panel attribute, Left panel will be opened by default -->
  31.       <p><a href="#" class="open-panel">Open Left Panel</a></p>
  32.       <!-- Click on link with "open-panel" and data-panel="right" attribute will open Right panel -->
  33.       <p><a href="#" data-panel="right" class="open-panel">Open Right Panel</a></p>
  34.     </div>
  35.   </div>
  36.   ...
  37. </body>
复制

实例预览

使用 JavaScript

我们也可以通过使用JavaScript打开和关闭侧栏,为此我们需要查看相关的App方法:

myApp.openPanel(position) - 打开侧栏.

  • position - string - 侧栏打开的位置: "left" 或 "right". 必需.

myApp.closePanel() - 关闭最近打开的侧栏.

  1. <body>
  2.   <div class="panel-overlay"></div>
  3.   <div class="panel panel-left panel-reveal">
  4.     <div class="content-block">
  5.       <p>Left Panel content here</p>
  6.       <p><a href="#" class="panel-close">Close me</a></p>
  7.       <p><a href="#" class="open-right-panel">Open Right Panel</a></p>
  8.     </div>
  9.   </div>
  10.   <div class="panel panel-right panel-cover">
  11.     <div class="content-block">
  12.       <p>Right Panel content here</p>
  13.       <p><a href="#" class="panel-close">Close me</a></p>
  14.       <p><a href="#" class="open-left-panel">Open Left Panel</a></p>
  15.     </div>
  16.   </div>
  17.   ...
  18.     <div class="page-content">
  19.       <div class="content-block">
  20.         <p><a href="#" class="open-left-panel">Open Left Panel</a></p>
  21.         <p><a href="#" class="open-right-panel">Open Right Panel</a></p>
  22.       </div>
  23.     </div>
  24.   ...
  25.   <script>
  26.     var myApp = new Framework7();
  27.  
  28.     var $$ = Dom7;
  29.  
  30.     $$('.open-left-panel').on('click', function (e) {
  31.         // 'left' position to open Left panel
  32.         myApp.openPanel('left');
  33.     });
  34.  
  35.     $$('.open-right-panel').on('click', function (e) {
  36.         // 'right' position to open Right panel
  37.         myApp.openPanel('right');
  38.     });
  39.  
  40.     $$('.panel-close').on('click', function (e) {
  41.         myApp.closePanel();
  42.     });
  43.  
  44.   </script>
  45. </body>
复制

实例预览

Panel 事件

Panel事件对检测用户如何与你的侧栏交互,或者在侧栏打开/关闭时做一些JavaScript操作非常有用。

Event(事件) Target(目标) Description(描述)
open Panel Element<div class="panel"> 当侧栏的打开动画开始时,事件将被触发
opened Panel Element<div class="panel"> 当侧栏的打开动画结束时,事件将被触发
close Panel Element<div class="panel"> 当侧栏的关闭动画开始时,事件将被触发
closed Panel Element<div class="panel"> 当侧栏的关闭动画完成时,事件将被触发

这里有一个例子:

  1. var myApp = new Framework7();
  2.  
  3. var $$ = Dom7;
  4.  
  5. $$('.panel-left').on('opened', function () {
  6.     myApp.alert('Left panel opened!');
  7. });
  8. $$('.panel-left').on('close', function () {
  9.     myApp.alert('Left panel is closing!');
  10. });
  11. $$('.panel-right').on('open', function () {
  12.     myApp.alert('Right panel is opening!');
  13. });
  14. $$('.panel-right').on('closed', function () {
  15.     myApp.alert('Right panel closed!');
  16. });
复制

 实例预览

滑动屏幕打开侧栏

Framework7支持通过滑动手势打开侧栏。但有一个限制 —— 只有一个菜单能被设置成允许滑动屏幕打开 (左或右)。

要使用这个功能,我们需要在App初始化时设置相关参数:

  1. var myApp = new Framework7({
  2.     swipePanel: 'left'
  3. });
复制

 实例预览

There are also swipePanelCloseOppositeswipePanelOnlyCloseswipePanelActiveAreaswipePanelNoFollow,swipePanelThreshold parameters that gives you more control over swipe panels. You can learn more about them in Initialize App section.

 

侧栏已经被打开?

有时候检测我们的一些侧栏是否是打开的是很有用的。这很容易,当一些菜单被打开时<body>将由以下规则生成被添加的类:with-panel-[position]-[effect]:

  • 如果你有带Cover效果的左侧栏打开着,body会有“with-panel-left-cover”类 <body class="with-panel-left-cover">

  • 如果你有带Reveal效果的左侧栏打开着, body 会有“with-panel-left-reveal”类 <body class="with-panel-left-reveal">

  • 如果你有带Cover效果的右侧栏打开着,body会有"with-panel-right-cover" 类  <body class="with-panel-right-cover">

  • 如果你有带Reveal效果的右侧栏打开着, body 会有"with-panel-right-reveal" 类 <body class="with-panel-right-reveal">

You can use it in JavaScript to detect opened panel:

  1. if ($$('body').hasClass('with-panel-left-cover')) {
  2.     console.log('Left Panel is opened')
  3. }
复制

或者在 CSS 定义额外的样式:

  1. /* Change Status Bar background when panel is opened */        
  2. body.with-panel-left-cover .statusbar-overlay{
  3.     background-color: #333;
  4. }
复制
 

猜你喜欢

转载自zaixianshouce.iteye.com/blog/2296760