angular点击查看更多(简单demo)
今天来跟大家分享一个小的demo,一般网页浏览到底部的时候会有一个点击加载更多的按钮,之前一直纠结怎么写这个,今天学习angular时发现可以用组件来实现这么一个小的效果,大家有兴趣的话可以看一下。
点击加载更多,代码如下:
<!DOCTYPE html>
<html ng-app="indexApp">//绑定模块
<head>
<meta charset="UTF-8">
<title></title>
<style>
#fixed{
height: 500px;
overflow: auto;
}
</style>
<script src="../js/angular.1.5.6.js"></script>//引入angular库
<script>
var app = angular.module('indexApp',[]);//定义模块
app.controller('indexCtrl',['$scope',function($scope){//定义控制器
$scope.items = ['a','b'];
for(var i=0;i<=30;i++){
$scope.items.push(Math.random()*10);
}
$scope.loca = function(){
for(var j=0;j<=10;j++){
$scope.items.push(Math.random()*10);
}
} }]);
app.directive('ngScroll',function(){//组件
return {
template:'<ul><li ng-repeat="item in items">{{item}}</li></ul>',
}
});
</script>
</head>
<body>
<div ng-controller="indexCtrl">
<div id="fixed" ng-scroll=""></div><!--指令的方式-->
<button ng-click="loca()">查看更多</button>
</div>
</body>
</html> 滑动到底部自动加载:
<!DOCTYPE html>
<html ng-app="loadMoreApp">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<style>
#fixed {
height: 500px;
overflow: auto;/*溢出显示滚动条*/
}
</style>
<body>
<div ng-controller="indexCtrl">
<div id="fixed" ng-scroll="loadMore()"><!--指令的方式-->
</div>
</div>
</body>
<script src="js/angular.js"></script>
<script>
var app = angular.module('loadMoreApp', []);//定义一个模块
app.controller('indexCtrl', ['$scope', function($scope) {//定义一个控制器
$scope.items = ['a', 'b'];
var i = 0;
for(; i <= 30; i++) {
$scope.items.push(Math.random() * 10);//生成随机数,加到items这个数组后
}
$scope.loadMore = function() {
var j = 0;
for(; j <= 10; j++) {
$scope.items.push(Math.random() * 10);
}
}
}]);
//定义一个组件
app.directive('ngScroll', [function() {
return {
template: '<ul><li ng-repeat="item in items">{{item}}</li></ul>',
link: function(scope, ele, attr) {
// console.log(ele);
ele.bind('scroll', function(e) {
// console.log("offsetHeight:" + e.target.offsetHeight)
// console.log("scrollTop:" + e.target.scrollTop) //滚动的距离
// console.log("scrollHeight" + e.target.scrollHeight)
if(e.target.offsetHeight + e.target.scrollTop >= e.target.scrollHeight) {
console.log("你已经到底了")
scope.$apply(attr.ngScroll);
}
})
}
}
}])
</script>
</html>
angular点击查看更多(简单demo)的更多相关文章
- 利用ScrollView滑动属性实现点击查看更多
利用ScrollView的滚动实现点击查看更多 效果图 更新内容布局 <ScrollView android:id="@+id/sv_des" android:layout_ ...
- JS点击查看更多内容 控制段落文字展开折叠
JavaScript+jQuery实现的文字展开折叠效果,点击文字后文字内容会完整的显示出来,控制段落来显示文字,不需要的时候,可以再次点击后将内容折叠起来,也就是隐藏了一部分内容.点击查看更多的功能 ...
- jquery 点击查看更多箭头变化,文字变化,超出带滚动条。
从网上好了好久,没找到自己要的,自己写了一下. <!DOCTYPE html> <html> <head> <meta charset="utf-8 ...
- jQ-点击查看更多
<style type="text/css"> .hi { width: 200px; height: 18vw; background-color: pink; fo ...
- angular实现了一个简单demo,angular-weibo-favorites
前面必须说一段 帮客户做了一个过渡期的项目,唯一的要求就是速度,我只是会点儿基础的php,于是就用tp帮客户做了这个项目.最近和客户架构沟通,后期想把项目重新做一下,就用现在最流行的技术,暂时想的使用 ...
- Deferred在jQuery和Angular中的使用与简单实现
Deferred在jQuery和Angular中的使用与简单实现 Deferred是在jQuery1.5版本中加入的,并且jQuery使用它完全重写了AJax,以前也只是偶尔使用.但是上次在使用Ang ...
- iOS中"查看更多/收起"功能实现
实现效果如图: 查看更多功能在很多app种都有应用,在这里简单的实现,介绍实现流程: 一个tableViewCell中包含一个collectionView,"查看更多"按钮是tab ...
- Spring的简单demo
---------------------------------------- 开发一个Spring的简单Demo,具体的步骤如下: 1.构造一个maven项目 2.在maven项目的pom.xml ...
- jquery 点击显示更多
点击显示更多 html <div class="servicepicture banxin"> <div class="imgcontent" ...
随机推荐
- 10Linux_firewalld-Linux就该这么学
firewalld: runtime:当前生效,重启后失效(默认) permanent:当前不生效,重启后永久生效. A:重启,B:执行firewall-cmd --reload 数据链路层:ipta ...
- css样式基础详解
一.字体属性:(font) 1.大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 2.样式 {font-styl ...
- 【UiPath 中文教程】02 - 创建自定义 Activity
在 UiPath Studio 中,活动 (Activity) 是流程自动化的基石,是构成自动化程序的最小模块.它们被包含在一个个 NuGet 包中. UiPath Studio 中有 3 类包: 官 ...
- [Docker] 写 Dockerfile 的最佳实践理论
指导方针 创建短暂的容器 意思是 container 可以停止和销毁,接着以最小化启动和配置进行重新构建和替换. 理解构建的上下文 使用 docker build ,当前工作环境称 ...
- vscode垂直选中列选中
VSCode列选择快捷键:Alt+Shift+左键
- 使用pip安装包提示TLS证书错误解决办法
最近有在使用pip安装python包的时候,总会出现以下类似的错误: Could not fetch URL https://pypi.python.org/simple/pytest-xdist/: ...
- dbForge Studio for MySQL 中文乱码问题
设置一下编码格式就好了 第一步:右键点击连接的数据库,选择第二个 第二步:选择第二个选项卡,设置编码格式,点击OK 第三步:确认保存并从新连接 最后你就发现能正常显示中文啦
- netcore 下加密遇到的问题
KeyedHashAlgorithm algorithm = KeyedHashAlgorithm.Create(algorithmName.ToString().ToUpper(CultureInf ...
- 转载——JavaScript学习笔记:取数组中最大值和最小值
转载自:http://www.w3cplus.com/javascript/calculate-the-max-min-value-from-an-array.html. 取数组中最大值 可以先把思路 ...
- 第一周pta作业2
7-2 求最大值及其下标 (20 分) 本题要求编写程序,找出给定的n个数中的最大值及其对应的最小下标(下标从0开始). 输入格式: 输入在第一行中给出一个正整数n(1<n≤10).第二行输入n ...