Angularjs:滚动到div底部

2024-04-04

我无法滚动到最后一条消息。

var app=angular.module('myApp', ['ngMaterial'] );
app.controller('ChatCtrl', function($window, $anchorScroll){
  var self = this;
  self.messageWindowHeight = parseInt($window.innerHeight - 170) + 'px';
  self.messages = [];
  for(var i = 1 ; i<=200;i ++){
    self.messages.push(i);
  }
  
  self.user = { text : ""};
  self.send = function(){
    self.messages.push(angular.copy(self.user.text));
    self.user.text = "";
  }
});

app.directive('scrollToBottom', function($timeout, $window) {
    return {
        scope: {
            scrollToBottom: "="
        },
        restrict: 'A',
        link: function(scope, element, attr) {
            scope.$watchCollection('scrollToBottom', function(newVal) {
                if (newVal) {
                    $timeout(function() {
                        element[0].scrollTop =  element[0].scrollHeight;
                    }, 0);
                }

            });
        }
    };
});
/* Styles go here */
.chat-box {
    padding: 8px;
    border-radius: 8px;
}
.message-box {
    border-top: 1px solid #E0E0E0;
    position: fixed;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 120px;
    background: white;
}

.message {
    overflow: scroll;
    margin: 4px;
    border: 1px solid #E0E0E0;
    -webkit-border-radius: 8px;
    -moz-border-radius: 8px;
    border-radius: 8px;
    height: 110px;
}
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="style.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.0.9/angular-material.css">
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=RobotoDraft:300,400,500,700,400italic">
</head>

<body ng-app='myApp' layout="column" layout-fill ng-controller="ChatCtrl as ctrl">
  <!-- content starts here -->
  <md-toolbar class="md-whiteframe-z2">
    <div class="md-toolbar-tools">
      <h2>
          hello
        </h2>
    </div>
  </md-toolbar>
  <md-content layout="column" style="background-color: #F5F5F5;"
              ng-style="{'height':ctrl.messageWindowHeight}">
      <div class="chat-box">
        <ol class="discussion" scroll-to-bottom="ctrl.messages">
          <li ng-repeat="message in ctrl.messages track by $index" id="msg-{{$index}}">
            {{message}}
          </li>
        </ol>
      </div>
  </md-content>
  <form name="userForm" novalidate ng-submit="ctrl.send()" layout="row" layout-padding layout-align="center center" class="message-box">
    <div class="message" flex>
      <md-input-container class="md-block">
        <input type="text" name="text" ng-model="ctrl.user.text" />
      </md-input-container>
    </div>
    <md-button class="md-icon-button" type="submit">
      send
    </md-button>
  </form>

  <!-- content ends here -->


  <!-- Angular Material Dependencies -->
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-animate.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-aria.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.0.9/angular-material.js"></script>

  <script src="script.js"></script>
</body>

</html>

这是一个plunk http://plnkr.co/edit/rAMvaQDFaFqlRQ6tXH2O我的代码。


移动scroll-to-bottom指令给md-content element

 <!-- MOVE directive here -->
 <md-content ͟s͟c͟r͟o͟l͟l͟-͟t͟o͟-͟b͟o͟t͟t͟o͟m͟=͟"͟c͟t͟r͟l͟.͟m͟e͟s͟s͟a͟g͟e͟s͟"͟ layout="column"
             style="background-color: #F5F5F5;" 
             ng-style="{'height':ctrl.messageWindowHeight}">
      <div class="chat-box">
        <!-- NOT HERE -->
        <ol class="discussion" ̶s̶c̶r̶o̶l̶l̶-̶t̶o̶-̶b̶o̶t̶t̶o̶m̶=̶"̶c̶t̶r̶l̶.̶m̶e̶s̶s̶a̶g̶e̶s̶"̶ >
          <li ng-repeat="message in ctrl.messages track by $index" id="msg-{{$index}}">
            {{message}}
          </li>
        </ol>
      </div>
  </md-content>

The scroll-to-bottom指令需要对实际滚动的元素进行操作。正在滚动的元素是<md-content>元素,因为它的高度受到其创建的 CSS 的限制ng-style https://docs.angularjs.org/api/ng/directive/ngStyle指示。这<ol>元素不滚动并且它的scrollHeight https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight属性是恒定的。

The DEMO http://plnkr.co/edit/Z6tdvG9Rt8DhuiHGurRu?p=preview

var app=angular.module('myApp', ['ngMaterial'] );
app.controller('ChatCtrl', function($window, $anchorScroll){
  var self = this;
  self.messageWindowHeight = parseInt($window.innerHeight - 170) + 'px';
  self.messages = [];
  for(var i = 1 ; i<=200;i ++){
    self.messages.push(i);
  }
  
  self.user = { text : ""};
  self.send = function(){
    self.messages.push(angular.copy(self.user.text));
    self.user.text = "";
  }
});

app.directive('scrollToBottom', function($timeout, $window) {
    return {
        scope: {
            scrollToBottom: "="
        },
        restrict: 'A',
        link: function(scope, element, attr) {
            scope.$watchCollection('scrollToBottom', function(newVal) {
                if (newVal) {
                    $timeout(function() {
                        element[0].scrollTop =  element[0].scrollHeight;
                    }, 0);
                }

            });
        }
    };
});
/* Styles go here */
.chat-box {
    padding: 8px;
    border-radius: 8px;
}
.message-box {
    border-top: 1px solid #E0E0E0;
    position: fixed;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 120px;
    background: white;
}

.message {
    overflow: scroll;
    margin: 4px;
    border: 1px solid #E0E0E0;
    -webkit-border-radius: 8px;
    -moz-border-radius: 8px;
    border-radius: 8px;
    height: 110px;
}
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.0.9/angular-material.css">
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=RobotoDraft:300,400,500,700,400italic">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-animate.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-aria.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.0.9/angular-material.js"></script>
<body ng-app='myApp' layout="column" layout-fill ng-controller="ChatCtrl as ctrl">
  <!-- content starts here -->
  <md-toolbar class="md-whiteframe-z2">
    <div class="md-toolbar-tools">
      <h2>
          hello
        </h2>
    </div>
  </md-toolbar>
  <md-content scroll-to-bottom="ctrl.messages" layout="column" style="background-color: #F5F5F5;" ng-style="{'height':ctrl.messageWindowHeight}">
      <div class="chat-box">
        <ol class="discussion">
          <li ng-repeat="message in ctrl.messages track by $index" id="msg-{{$index}}">
            {{message}}
          </li>
        </ol>
      </div>
  </md-content>
  <form name="userForm" novalidate ng-submit="ctrl.send()" layout="row" layout-padding layout-align="center center" class="message-box">
    <div class="message" flex>
      <md-input-container class="md-block">
        <input type="text" name="text" ng-model="ctrl.user.text" />
      </md-input-container>
    </div>
    <md-button class="md-icon-button" type="submit">
      send
    </md-button>
  </form>
</body>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angularjs:滚动到div底部 的相关文章

随机推荐

  • 我可以将 xmodem 协议与 PySerial 一起使用吗?

    我通过 PySerial 与串行设备建立了工作连接 但我也想通过 xmodem 协议传输文件作为我的程序的一部分 哪一种是最平台中立的方式来做到这一点 最坏的情况 我可以close my serial SerialPython中的对象及其使
  • 如何在Python中从.pb文件恢复Tensorflow模型?

    我有一个 tensorflow pb 文件 我想将其加载到 python DNN 中 恢复图形并获取预测 我这样做是为了测试创建的 pb 文件是否可以做出与正常 Saver save 模型类似的预测 我的基本问题是 当我使用上述 pb 文件
  • Laravel Web 和 API 控制器结构。分开与干燥

    我想构建一个同时使用 Web 和 API 部分的 Laravel 应用程序 常见的 也是我的 问题是是否使用单独的控制器 有 2 个选项 独立控制器Laravel API 控制器结构 https stackoverflow com ques
  • CryptoStream 和经过身份验证的加密模式

    我有兴趣提供一个在 Net 中使用的托管 dll 它提供经过身份验证的加密服务 DLL 可以在 WPF 程序或 ASP 应用程序中使用 我有几个与 Microsoft 的加密和流模型相关的问题 经过身份验证的加密模式 CCM CWC EAX
  • 构建 Yeoman 应用程序会破坏 CSS 背景图像

    我正在尝试将现有项目迁移到 Yeoman Grunt Bower 更改里面的路径后Gruntfile js我注意到图像路径没有正确构建 background transparent url Users lucian Projects PRO
  • om 组件应该返回什么才能不渲染任何内容?

    是否可以编写一个不呈现任何内容的组件 例如 如果其光标数据为空 我不能做 defn count or nothing list cursor owner reify om IRender render if not empty list c
  • 使用 Orchard 创建列表

    我正在尝试按照本教程创建内容列表 http docs orchardproject net Documentation Creating lists http docs orchardproject net Documentation Cr
  • 如何安装第三方库

    我对 C 有点陌生 我决定尝试使用odeint做一些模拟 因为 python 太慢了 无法满足我的需求 I found 这个包 http headmyshoulder github io odeint v2 index html 我想玩 我
  • 实体框架 4 中是否允许可为 null 的外键?

    我在更新实体框架实体中的外键时遇到问题 我正在使用自跟踪实体 并且有一个具有某些关系的实体 其中外键也作为属性存在 EF4 的新功能之一 键 整数 被标记为可空且并发模式固定 具体来说 我有一个与确认用户具有多对 0 1 关系的 Alarm
  • 在 Windows 上,使用 C# 编写的 COM 服务器,可以为早期绑定和后期绑定代码返回 SAFEARRAY 吗?

    问题很长 所以我将用要点格式化以便于讨论 介绍 我正在编写一个 C COM 服务器 COM 服务器可在 Excel VBA 中以早期绑定和后期绑定模式使用 我的绊脚石是如何返回在早期和后期绑定模式下都可以工作的实例化类的 SAFEARRAY
  • 创建一个新向量,其中没有元素与原始向量处于相同位置?

    假设我有一个向量V1 有两个或多个元素 V1 lt 1 10 我可以使用该函数对原始向量重新排序sample 但是 此函数无法确保新向量中没有元素与原始向量处于相同位置 例如 set seed 4 V2 lt sample V1 这将产生一
  • iOS 8 上的 UIActivityViewController 显示带有自定义活动的“更多”按钮

    现在带有 iOS 8 SDK 的 XCode 6 已经出来了 我们可以讨论一些东西了 我尝试在 iOS 7 上使用 UIActivityViewController 的自定义活动 一切正常 但在 iOS 8 上 当显示自定义活动时 它们旁边
  • 如何从输入字段将图像上传到firebase云存储?

    我有一个输入字段 用户可以在其中选择他们想要上传的图像 然后我需要发送到云存储 问题是 我不知道如何获取他们选择的文件 我看到很多问题 比如this https stackoverflow com questions 31353703 ho
  • ng-repeat 上的“中继器中不允许重复”

    我收到了从服务请求返回的以下 json 数据 entries id 2081 name BM niceName bodmas id 8029 name Mas niceName Masm count 2 我正在尝试在 html 中使用以下代
  • PowerBuilder 中的 .NET 互操作

    我正在寻找一种在 PB 对象之间进行双向通信的方法 和 NET C 对象 在查看 Brad 的 GUI 的 NET 版本时 控件 我了解如何为 NET 对象提供对 PB 的引用 目的 但在该示例中 它被强制转换为 PowerObject 基
  • Spring注解-@Configuration调用spring bean自动构建

    如果我使用 Bean 声明一个类 然后对该类进行组件扫描 spring 将通过调用它的构造函数并注入构造函数参数并注入任何标记有 Inject 的字段来实例化该类 为了简单起见 我们将这个 Spring 自动构建称为 Spring 自动构建
  • Chrome 扩展后台页面未显示 Chrome 通知

    使用新的chrome notificationsAPI 我无法从我的扩展程序中获取通知 即使是最基本的通知也无法出现 但我没有收到任何错误 并且回调函数已正确执行 清单 json name notify version 0 0 0 mani
  • Python 中的 Flesch-Kincaid 可读性测试

    我需要帮助解决我遇到的这个问题 我需要编写一个从文本返回 FRES Flesch 阅读轻松测试 的函数 给出公式 换句话说 我的任务就是把这个公式变成一个Python函数 这是来自的代码我之前的问题 https stackoverflow
  • 使用另一个 numpy 数组元素作为索引向量化更新 numpy 数组

    设 A C 和 B 是行数相同的 numpy 数组 我想更新 A 0 的第 0 个元素 A 1 的第 2 个元素等 即将 A i 的第 B i 个元素更新为 C i import numpy as np A np array 1 2 3 3
  • Angularjs:滚动到div底部

    我无法滚动到最后一条消息 var app angular module myApp ngMaterial app controller ChatCtrl function window anchorScroll var self this