【问题标题】:How to scroll with ionic and have fixed content above如何使用离子滚动并在上面固定内容
【发布时间】:2016-08-26 08:28:06
【问题描述】:

我有以下看法:

 <ion-view ng-controller="recentDetailCtrl as vm">
   <ion-content  class="has-header">
      <div  style="padding:0px;">
        <my-video  video-url='vm.videoUrl'
           player-width="'100%'" player-height="'180px'"></my-video> 
    </div>
    <ion-scroll>
      <ion-list>
        <ion-item ng-repeat="comment in vm.comments">
                {{comment.text}}

        </ion-item>
      </ion-list>
   </ion-scroll>
  </ion-content>
</ion-view>

我想要的是要滚动的 cmets 列表,但始终固定视频元素(因此它会在视频下方滚动 - 类似于移动 youtube 应用程序允许您滚动固定播放视频的 cmets 的方式)。

目前,当我滚动视频时,视频会向上移出视图。

【问题讨论】:

    标签: css angularjs ionic-framework


    【解决方案1】:
    1. 将视频指令移出 ion-content(将滚动 ion-content 中的所有元素)。
    2. 更改 ion-content 的 css,使其不占据屏幕的上半部分。
    3. 将 position:fixed 和其他 css 添加到您的视频指令中,使其占据屏幕的上半部分。

    HTML:

    <html ng-app="ionicApp">
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    
        <title>Ionic Fixed Element at Top</title>
    
        <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
        <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
    
      </head>
      <body ng-controller="MyCtrl">
    
        <ion-header-bar class="bar-positive">
          <h1 class="title">Ionic Fixed Element at Top</h1>
        </ion-header-bar>
    
        <div class="fixed-header my-video">
           My fixed video content
        </div>
        <ion-content class="ion-content-below-my-video">
          <ion-list>
            <ion-item>1</ion-item>
            <ion-item>2</ion-item>
            <ion-item>3</ion-item>
            <ion-item>4</ion-item>
            <ion-item>5</ion-item>
            <ion-item>6</ion-item>
            <ion-item>7</ion-item>
            <ion-item>8</ion-item>
            <ion-item>9</ion-item>
            <ion-item>0</ion-item>
            <ion-item>1</ion-item>
            <ion-item>2</ion-item>
            <ion-item>3</ion-item>
            <ion-item>4</ion-item>
            <ion-item>5</ion-item>
            <ion-item>6</ion-item>
            <ion-item>7</ion-item>
            <ion-item>8</ion-item>
            <ion-item>9</ion-item>
            <ion-item>0</ion-item>
          </ion-list> 
        </ion-content>
    
      </body>
    </html>
    

    CSS:

    body {
      cursor: url('http://ionicframework.com/img/finger.png'), auto;
    }
    
    .fixed-header{
      margin-top:43px;
      /* 
       The ionic header bar is 43px in height,
       put your content below the header bar.
      */
    }
    
    .platform-webview.platform-ios.platform-cordova .fixed-header{
      margin-top:63px;
      /* On iOS, moving a div out of ion-content,
      I think you need to compensation for the 20px system status bar.
      so it's 43px+20px=63px. but I'm not so sure, it hasn't been tested.
      Test it yourself and make modifications if needed.
      */
    }
    
    .my-video
    {
      height:200px;
      width:100%;
      position:absolute;
      background:grey;
      color:red;
      text-align:center;
      padding:20px;
    }
    
    .ion-content-below-my-video{
      margin-top:200px!important;
    }
    
    .platform-webview.platform-ios.platform-cordova .ion-content-below-my-video{
      margin-top:220px;
      /*
      Same as .fixed-header
      */
    }
    

    这里是代码笔:

    http://codepen.io/KevinWang15/pen/QNJEXX

    【讨论】:

    • 恐慌已经开始......仍然无法正常工作。我应该添加我的视频元素是 iframe(angular-youtube-video - 不确定这是否导致问题或我的兼职 CSS 技能应该受到责备)。如果你能提供一个 CodePen 那就太棒了。
    • 谢谢凯文,它工作得非常好。非常感谢您的帮助。
    • 如果将z-index设置为固定元素,则可以跳过2.
    【解决方案2】:

    你可以使用ion-fixed: (Ionic 3.5.0)

    <ion-content no-padding>
      <div ion-fixed class="map"></div>
      <div ion-fixed class="test_class">
        <h4>Bla Bla</h4>
      </div>
      <ion-list>
      <ion-item>
         <p>Test</p>
      </ion-item>
     </ion-list>
    </ion-content>
    

    然后在css上:

    .map {
    width: 100%;
    overflow: hidden;
    height: 30%;
    }
    
    .test_class {
    width: 100%;
    height: 30px;
    top: 30%;
    }
    

    Ionic 文档还没有这方面的参考。

    【讨论】:

    • 这对我有用 100% 谢谢...不知道为什么我花了这么长时间才找到这个!这应该被标记为批准的答案!
    【解决方案3】:

    Ionic 属性中现在有slot。你可以这样使用它:

    &lt;my-video slot="fixed"&gt;&lt;/my-video&gt;

    这里的 Ionic 文档中很快提到了这一点:
    https://ionicframework.com/docs/api/content#slots

    【讨论】:

      【解决方案4】:

      在 Ionic4 中,您可以为 &lt;ion-content&gt; 的子级使用 slot="fixed" 属性来实现此目的。

      查看docs

      【讨论】:

        【解决方案5】:

        我有同样的用例。将视频内容从 ion-content 中移出就可以了。

        【讨论】:

        • 我正在使用 Ionic5
        • 谢谢!这个简单的技巧实际上有效......只要确保你没有像我一样在&lt;ion-content&gt; 元素上设置[fullscreen]="true",因为我复制/粘贴了一个示例...... doh! :-/
        猜你喜欢
        • 2022-01-06
        • 2021-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-07
        • 1970-01-01
        相关资源
        最近更新 更多