gpt4 book ai didi

angularjs - 将 angularjs 指令转换为 angular 10

转载 作者:行者123 更新时间:2023-12-04 02:30:44 25 4
gpt4 key购买 nike

我想在我的 Angular 项目中使用这段代码:

'use strict';

/**
* Floating text animation (random)
*/
angular.module('g1b.text-animation', []).
directive('textAnimation', ['$document', '$interval', '$timeout', function ($document, $interval, $timeout) {
return {
restrict: 'A',
compile: function () {
return {
pre: function () {},
post: function (scope, element) {
var chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
$interval(function () {
for ( var i = 0; i < Math.floor(Math.random() * 5); i++ ) {
var character = chars[Math.floor(Math.random() * chars.length)];
var duration = Math.floor(Math.random() * 15);
var offset = Math.floor(Math.random() * (45 - duration * 3)) + 3;
var size = 12 + (15 - duration);
var span = angular.element('<span class="animated-text" style="right:'+offset+'vw; font-size: '+size+'px; animation-duration:'+duration+'s">'+character+'</span>');
element.append(span);
$timeout(function (span) {
span.remove();
}, duration * 1000, false, span);
}
}, 250);
}
};
}
};
}]);

它还有一个 CSS 文件。这段代码基本上是一个文字动画。我的问题是我不知道从哪里开始。

这就是我要实现的目标: https://rawgit.com/g1eb/angular-text-animation/master/

这是它的 npm: https://github.com/g1eb/angular-text-animation

更新:

我已经自己尝试过了,这就是我所拥有的:

@ViewChildren('styleDiv', {read: ElementRef}) children: QueryList<ElementRef>;

constructor(private renderer: Renderer2, private host: ElementRef) {
}

ngOnInit(): void {
}

ngAfterViewInit(): void {
this.animateBackground();
}

private animateBackground(): void {
const renderer = this.renderer;
const children = this.children;
const host = this.host;
const chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
setInterval(() => {
for (let i = 0; i < Math.floor(Math.random() * 5); i++) {
const character = chars[Math.floor(Math.random() * chars.length)];
const duration = Math.floor(Math.random() * 15);
const offset = Math.floor(Math.random() * (45 - duration * 3)) + 3;
const size = 12 + (15 - duration);
const span = '<span class="animated-text" style="right:' + offset + 'vw; font-size: ' +
+size + 'px; animation-duration:' + duration + 's">' + character + '</span>';
this.children.first.nativeElement.insertAdjacentHTML('beforeend', span);
setTimeout(() => {
// renderer.removeChild(children.first.nativeElement.parentNode, children.first.nativeElement);
}, duration * 1000, false, host, children, renderer);
}
}, 250, host, children, renderer);
}

它可以工作,但我在设置超时函数中确实遇到了问题。我可以将 span 添加到 dom,但无法将其删除。

最佳答案

在与编写此指令的人交谈后,我们决定我将为 angular 2+ 编写一个新指令,他应该会在接下来的几天内批准拉取请求。新代码应该很快就会在他的存储库中可用: https://github.com/g1eb/angular-text-animation

但现在,这是新指令:

import {AfterViewInit, Directive, ElementRef, Input, Renderer2} from '@angular/core';

@Directive({
selector: '[sbzTextAnimation]'
})
export class NgxSbzTextAnimationDirective implements AfterViewInit {
@Input() maxFontSize = 20;
@Input() colorSchemeArray: string[];
@Input() position: 'left' | 'right' = 'right';

constructor(private elementRef: ElementRef, private renderer: Renderer2) {
}

ngAfterViewInit(): void {
this.init();
this.animateBackground();
}

private init(): void {
this.colorSchemeArray = this.colorSchemeArray
? this.colorSchemeArray
: [
'#63b598', '#ce7d78', '#ea9e70', '#a48a9e', '#c6e1e8', '#648177', '#0d5ac1',
'#f205e6', '#1c0365', '#14a9ad', '#4ca2f9', '#a4e43f', '#d298e2', '#6119d0',
'#d2737d', '#c0a43c', '#f2510e', '#651be6', '#79806e', '#61da5e', '#cd2f00',
'#9348af', '#01ac53', '#c5a4fb', '#996635', '#b11573', '#4bb473', '#75d89e',
'#2f3f94', '#2f7b99', '#da967d', '#34891f', '#b0d87b', '#ca4751', '#7e50a8',
];
}

private animateBackground(): void {
// need to access them in the setTimeout function
const renderer = this.renderer;
const elementRef = this.elementRef;

const chars = [...Array(26)].map((e, i) => (i + 10).toString(36));

setInterval(() => {
for (let i = 0; i < Math.floor(Math.random() * 5); i++) {
const duration = Math.floor(Math.random() * 15);
const offset = Math.floor(Math.random() * (45 - duration * 3)) + 3;
const size = 12 + (this.maxFontSize - duration);
const color = this.colorSchemeArray[Math.floor(Math.random() * this.colorSchemeArray.length)];

const span = renderer.createElement('span');
span.innerText = chars[Math.floor(Math.random() * chars.length)];
renderer.addClass(span, 'animated-text');

renderer.setStyle(span, 'color', color);
renderer.setStyle(span, this.position, `${offset}vw`);
renderer.setStyle(span, 'font-size', `${size}px`);
renderer.setStyle(span, 'animation-duration', `${duration}s`);
renderer.setStyle(span, 'color', color);

renderer.appendChild(elementRef.nativeElement, span);
setTimeout(() => {
renderer.removeChild(elementRef.nativeElement, elementRef.nativeElement.firstChild);
}, duration * 1000, false, elementRef, renderer);
}
}, 250);
}
}

我也做了一些改动:

  1. 文本现在是彩虹色。
  2. 添加了最大字体大小的输入以自定义用例:[maxFontSize]="30"
  3. 添加了彩虹色的输入。如果用户只想要一种颜色,他可以传递一种颜色或不同的方案是这样的:[colorSchemeArray]="['#000"]
  4. 为 float 文本在背景中的位置添加了一个输入。

这是指令的 CSS(应该在使用指令的组件的 CSS 文件中):

/**
* Floating text animation styles
*/
.animated-text {
cursor: default;
color: #68C2A3;
font-family: 'VT323', monospace, sans-serif;
text-shadow: 0 0 1px #ffffff;
font-size: 24px;
position: fixed;
top: -50px;
user-select: none;
-ms-user-select: none;
-moz-user-select: none;
-khtml-user-select: none;
-webkit-touch-callout: none;
-webkit-user-select: none;
animation-name: float-animation;
animation-timing-function: ease-out;
}

@keyframes float-animation {
0% {
top: 100vh;
opacity: 0;
}
25% {
opacity: 1;
}
50% {
opacity: 1;
}
75% {
opacity: 0;
}
100% {
top: -25px;
opacity: 0;
}
}

这是一个完整的工作演示: https://stackblitz.com/edit/floating-text-animation?file=src/app/app.component.html

更新:这是 npm 上最终指令的链接: https://www.npmjs.com/package/ngx-sbz-text-animation

关于angularjs - 将 angularjs 指令转换为 angular 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64367357/

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