angluarjs监听浏览器变化,使用directive获取dom元素属性
来源:互联网 发布:java构造函数怎么写 编辑:程序博客网 时间:2024/05/05 22:09
以下是一个响应式界面:
<div ng-app="myApp"> <div id="pattern" class="pattern" ng-controller="MyController"> <div class="offcanvas-top" ng-class="{active:showMore}" ng-style="contentHeight"> <div class="o-content" get-height> <p>Here is more content. This could be related items, navigation or other content you feel is conducive to this type of treatment.</p> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean quis nisi et dui placerat ornare. Maecenas molestie lacus lobortis libero lacinia sed scelerisque lectus congue. Mauris dignissim nisi a ante laoreet et ullamcorper ligula ullamcorper. In hac habitasse platea dictumst. In nisi odio, tempor in viverra vitae, mollis ac tortor. Sed a rhoncus leo. Maecenas ac dui elit, tristique dapibus nisl. Suspendisse feugiat porta ligula, auctor posuere lorem vulputate et.</p> </div> <a href="#" id="trigger" ng-click="showMore=!showMore">{{herfText}}</a> </div> </div>
body{ margin: 0;}.offcanvas-top { position: relative; overflow: hidden; height: 3.2em; -webkit-transition: height 0.5s ease-out; -moz-transition: height 0.5s ease-out; -o-transition: height 0.5s ease-out; transition: height 0.5s ease-out;}.offcanvas-top.active { height: 13.6em;}.o-content { width: 100%; position: absolute; bottom: 0; padding: 1em 1em 2.5em;}.crumbs li a { display: block; padding: 1em; border-bottom: 1px solid #000;}.crumbs li:last-child a { border-bottom: 0;}#trigger { position: absolute; bottom: 0; right: 0; display: block; font-size: 1em; padding: 0 1em 1em;}
angular.module('myApp', []) .controller('MyController', function ($scope,$interval, $window) { $scope.herfText = 'More +'; $scope.showMore = 0;//判定是否点击了展开按钮,是为1,收起为0 //监听展开按钮的变化,每变化一次对class="offcanvas-top"的div容器高度进行重新设置。 $scope.$watch('showMore', function (data) { if (data) { $scope.herfText = 'Hide -'; $scope.contentHeight = {height: $scope.resetHeight + 'px'}; } else { $scope.herfText = 'More +'; $scope.contentHeight = {height: 3.2 + 'em'}; } }); }) .directive('getHeight', function ($interval, $window) { //用directive中的link,获取dom的高度 return { restrict: 'A', link: function (scope, element, attrs) { scope.resetHeight = element[0].offsetHeight; //监听浏览器宽度的变化。浏览器越宽,隐藏信息高度约小 $window.addEventListener('resize', function () { scope.resetHeight = element[0].offsetHeight; }, false); } } });
该响应时页面通过监听浏览器size的变化,改变下拉菜单的div的高度以实现,在任何宽度下的下拉菜单响应式。如图
在angularjs中,一般使用directive来获取dom元素。
.directive('tmp', function ( ) { return { restrict: 'A', link: function (scope, element, attrs) { } } });元素的信息会传递到link的element中,可以使用console.log将element打印出来,查看相关信息。
link的scope指向的是controller的作用域,在这里可以使用scope.X调用controller作用域中的变量。
在directive中应该使用:
$window.addEventListener('resize', function () { }, false);
来监听浏览器窗口大小的变化。
scope.resetHeight = element[0].offsetHeight;在这里,使用element中的offsetHeight属性来作为元素高度的参考,可以准确的获得元素显示所需的高度。
在窗口宽度缩小的过程中,带有文字的元素宽度会逐渐缩小,高度会逐渐增加,用元素的高度,为它的外层div的高度,就能将元素在div容器中完整的显示出来。效果就如上图所示。
0 0
- angluarjs监听浏览器变化,使用directive获取dom元素属性
- dom监听属性的变化
- javascriptj监听dom元素的变化
- DOM:获取元素和获取设置属性
- JavaScript DOM 中获取元素属性
- js获取Dom元素的属性样式
- [DOM元素处理] 使用jQuery获取iframe元素的value属性值 IE-firefox可以用
- 浏览器窗口变化监听
- 动态创建dom元素、获取dom元素的属性及遍历dom元素的方法
- DOM简介,DOM树简介,获取元素节点对象; DOM对象的常见的元素属性
- DOM之获取元素,获取和设置属性
- JavaScript学习笔记:使用DOM获取元素
- 不同浏览器获取DOM元素的各种高度
- 不同浏览器获取DOM元素的各种高度
- Angular中使用$watch监听object属性值的变化
- IPHONE 和 KVC/KVO 监听对象属性变化(例如:获取textView 的text的变化)
- IPHONE 和 KVC/KVO 监听对象属性变化(例如:获取textView 的text的变化)
- jQuery -> 获取/设置/删除DOM元素的属性
- Linux spi驱动分析(二)----SPI核心(bus、device_driver和device)
- java讲义经验(三)数组理解
- HTML+CSS基础课程之form表单中的label标签
- IPC机制
- text——默认系统字体
- angluarjs监听浏览器变化,使用directive获取dom元素属性
- Mybatis中的_parameter
- activity之间传集合遇到的bug
- Storm tuple发送机制
- Linux spi驱动分析(三)----spiddev分析
- 关于Input关闭
- 一些网络应用的工具类
- [SCOI2005]互不侵犯king
- 进制转换(栈)