gpt4 book ai didi

javascript - 如何使用 Angular 根据元素的高度显示/隐藏元素?

转载 作者:太空宇宙 更新时间:2023-11-03 20:12:38 28 4
gpt4 key购买 nike

如果我的段落高度达到 200 像素,我想显示一个“阅读更多”链接。使用 Angular 的优雅方式是什么?

我的元素-

<section class="mynotes" ng-if="MyController.mynotes">
<p ng-bind="MyController.mynotes" ng-class="{'showMore': more, 'showLess': !more}"></p>
<section ng-click="MyOtherController.togglearrow($event)">
<i class="down-arrow"></i>
<span ng-click="more = !more">{{more ? 'Less' : 'More'}}</span>
</section>

我想根据 p 高度隐藏箭头和 Less/More 文本。有什么想法吗?

最佳答案

这是我刚刚想出的一个简单且可配置的指令,用于显示较少的功能。

.directive('moreless', function($timeout){
//Default options, default height of 200
var defOption = {
height:200
};

return {
restrict:'EA',
transclude:true, //transclusion enabled
scope:{}, //scoped to isolate since this adds functions on scope
template:'<div class="showless"><div ng-transclude class="content" ng-style="style"></div><section class="trigger" ng-if="showSection"> \
<i class="down-arrow"></i> \
<span ng-click="toggleMore()">{{{true:"More", false:"Less"}[more]}}</span> \
</section></div>',
link:function(scope, elm, attrs){
var origHeight,
//Construct the options from default options and override by any thing specified
options = angular.extend({}, angular.copy(defOption), scope.$eval(attrs.options));

//Initialize
initialize();

function initialize(){
scope.toggleMore = toggleMore;
scope.toggleSection = toggleSection;
//Do not show showless section initially
scope.toggleSection(false);
//Wait for one digest cycle to complete and intialize showless componenet
$timeout(_initShowLess);
}

function _initShowLess(){
//Set initial height and get the height
origHeight = elm.find('.content').height();
scope.style = {height:origHeight};
//compare the height to allowed height and togglesection and set showmore accordingly
if(origHeight > options.height){
scope.toggleSection(true);
scope.style.height = options.height;
scope.toggleMore();
}
}

function toggleMore(){
expandCollapse(scope.more);
scope.more = !scope.more;
}

function toggleSection(show){
scope.showSection = show;
}

function expandCollapse(expand){
var height = expand ? origHeight : options.height;
scope.style.height = height;
}

}
}

});

默认设置为 200 高度,您可以通过 options 属性进行配置。所以用法类似于:

<moreless options="{height:300}">
<p ng-bind="mynotes2"></p>
</moreless>

Demo

内联演示'

var app = angular.module('plunker', []);

app.directive('moreless', function($timeout) {

var defOption = {
height: 200
};

var defOffset = 10;

return {
restrict: 'EA',
transclude: true,
scope: {},
template: '<div class="showless"><div ng-transclude class="content" ng-style="style"></div><section class="trigger" ng-if="showSection"> \
<i class="down-arrow"></i> \
<span ng-click="toggleMore()">{{{true:"More", false:"Less"}[more]}}</span> \
</section></div>',
link: function(scope, elm, attrs) {
var origHeight,
options = angular.extend({}, angular.copy(defOption), scope.$eval(attrs.options));

initialize();


function initialize() {
scope.toggleMore = toggleMore;
scope.toggleSection = toggleSection;
scope.toggleSection(false);
$timeout(_initShowLess);
}

function _initShowLess() {

origHeight = elm.find('.content').height();
scope.style = {
height: origHeight
};
if (origHeight > options.height) {
scope.toggleSection(true);
scope.style.height = options.height;
scope.toggleMore();
}
}

function toggleMore() {
expandCollapse(scope.more);
scope.more = !scope.more;
}

function toggleSection(show) {
scope.showSection = show;
}

function expandCollapse(expand) {
var height = expand ? origHeight : options.height;
scope.style.height = height;
}

}
}

}).controller('MainCtrl', function($scope) {
$scope.mynotes = 'Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.(The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.';

$scope.mynotes2 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
})
/* Put your css in here */

.showless {
position: relative;
}
.showless .content {
overflow: hidden;
transition: all linear 0.5s;
-webkit-transition: all linear 0.5s;
}
.showless .trigger {
background: #cdcdcd;
display: inline-block;
}
<!DOCTYPE html>
<html ng-app="plunker">

<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script data-require="jquery@*" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link data-require="bootstrap-css@*" data-semver="3.3.1" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.10/angular.js" data-semver="1.3.10"></script>

<script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
<div>
<moreless>
<p ng-bind="mynotes"></p>
</moreless>
</div>
<div>
<moreless options="{height:100}">
<p ng-bind="mynotes"></p>
</moreless>
</div>
<div>
<moreless options="{height:300}">
<p ng-bind="mynotes2"></p>
</moreless>
</div>
</body>

</html>

关于javascript - 如何使用 Angular 根据元素的高度显示/隐藏元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28054982/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com