【问题标题】:How to scroll down to bottom of page when data added?添加数据时如何向下滚动到页面底部?
【发布时间】:2017-03-17 15:21:10
【问题描述】:

我在我的 Ionic 应用程序中进行了聊天,但是当用户收到新消息并在聊天中时,视图不会向下滚动以显示消息,用户必须手动向下滚动才能看到它.

出现新消息时如何自动滚动到页面底部?

这是我的代码的 HTML:

<ion-view  view-title="Chat Detail">


    <ion-content class="padding" start-y="430">
        <!-- *messages / content messages -->
        <ol  class="messages">

            <!-- *self / container self messages -->
            <li ng-repeat="message in messages" ng-class="{self: message.userId === myId, other: message.userId !== myId}">
                <!-- *avatar / avatar image -->
                <div class="avatar">
                    <img ng-src="{{ message.profilePic }}">
                </div>
                <!-- *msg / messages -->
                <div id="data" class="msg">
                    <p>{{ message.text }}</p>
                    <time>
                        <i class="icon ion-ios-clock-outline"></i>
                        {{message.time}}
                    </time>
                </div>
            </li>
        </ol>
    </ion-content>
    <!-- *bar-detail / color bar and input -->
    <ion-footer-bar keyboard-attach class="item-input-inset bar-detail">
        <label class="item-input-wrapper">
        <input onblur="this.focus()" autofocus type="text" placeholder="Type your message" on-return="sendMessage(); closeKeyboard()" ng-model="data.message" on-focus="inputUp()" on-blur="inputDown()" />
        </label>
        <a class="button button-icon icon ion-arrow-return-left" ng-click="sendMessage()" ></a>
    </ion-footer-bar>
</ion-view>

编辑:添加控制器:

  //Recieve messages
        $timeout(function(){
        var ref = firebase.database().ref('/messages/' + $scope.currentDiscussion);
        ref.orderByChild("timestamp").on("value", function(snapshot1) {

          $timeout(function(){
          $scope.messages = snapshot1.val();

            })
          })

        })
        window.scrollTo(0,document.body.scrollHeight);

        $scope.sendMessage = function() {

          if ($scope.blocked === 1){

            // An alert dialog

     var alertPopup = $ionicPopup.alert({
       title: 'BLOCKED',
       template: 'This discussion is blocked.'
     });

     alertPopup.then(function(res) {

     });
   } else {

          var d = new Date();
          var g = new Date();
          d = d.toLocaleTimeString().replace(/:\d+ /, ' ');
          g = g.toLocaleTimeString().replace(/:\d+ /, ' ') + new Date();



          $scope.sendMessages = firebase.database().ref('/messages/' + $scope.currentDiscussion).push({
            userId: myId,
            text: $scope.data.message,
            time: d,
            timestamp: g,
            profilePic: $scope.displayProfilePic,

          });

          firebase.database().ref('/accounts/' + myId + "/discussions/" + $scope.currentDiscussion).update({
            lastMessage: $scope.data.message,
            time: d,
            blocked: 0,
            blockedOther: 0,
            });

            if ($scope.newMessages === ""){
            firebase.database().ref('/accounts/' + $scope.savedUid + "/discussions/" + $scope.currentDiscussion).update({
              lastMessage: $scope.data.message,
              name: $scope.displayName,
              profilePic: $scope.displayProfilePic,
              time: d,
              discussionId: $scope.currentDiscussion,
              blocked: 0,
              newMessages: 1,
              userId: myId,
              });
            } else {
              firebase.database().ref('/accounts/' + $scope.savedUid + "/discussions/" + $scope.currentDiscussion).update({
                lastMessage: $scope.data.message,
                name: $scope.displayName,
                profilePic: $scope.displayProfilePic,
                time: d,
                discussionId: $scope.currentDiscussion,
                blocked: 0,
                newMessages: $scope.newMessages + 1,
                userId: myId,
                });
            }

              firebase.database().ref('/accounts/' + $scope.savedUid ).update({

                messagesTotal: $scope.messagesTotal + 1,
                });


          delete $scope.data.message;
          $ionicScrollDelegate.scrollBottom(true);
        }

        };

        $scope.inputUp = function() {
          if (isIOS) $scope.data.keyboardHeight = 216;
          $timeout(function() {
            $ionicScrollDelegate.scrollBottom(true);
          }, 300);

        };

        $scope.inputDown = function() {
          if (isIOS) $scope.data.keyboardHeight = 0;
          $ionicScrollDelegate.resize();
        };

        $scope.closeKeyboard = function() {
          // cordova.plugins.Keyboard.close();
        };

      })
      $scope.leaveChat = function() {
           var userId = firebase.auth().currentUser.uid;
           firebase.database().ref('accounts/' + userId).update({
             currentDiscussion: "",
             })



         };


      $scope.data = {};
      $scope.myId = myId;

      $scope.messages = [];

【问题讨论】:

    标签: javascript html css firebase ionic-framework


    【解决方案1】:

    您可以在这里查看$ionicScrollDelegate 服务:http://ionicframework.com/docs/api/service/$ionicScrollDelegate/

    使用方法

    $ionicScrollDelegate.scrollBottom([shouldAnimate])
    

    shouldAnimate 是一个布尔类型,用于指定是否需要动画滚动。

    【讨论】:

    • 你好,是的,这就是我发送消息时使用的,它会自动向下滚动,但是当我收到消息时,这不起作用,我不明白如何集成它方式。我添加了控制器的代码供您查看是否有想法?提前谢谢你
    • 您可以使用角度$watch 服务为$scope.messages 对象创建一个侦听器。对于监听器,每次$scope.messages 更改其值时,您都会调用$ionicScrollDelegate.scrollBottom(])。
    • 您介意为我更新初始代码以便将其集成到我的实际代码中吗?我在尝试这个解决方案时非常头疼(我对 Javascript 还是很陌生)。
    猜你喜欢
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多