UNPKG

ng-cw-v12

Version:

Angular UI component library

163 lines (158 loc) 9.2 kB
import * as i0 from '@angular/core'; import { Directive, Input, NgModule } from '@angular/core'; class MagicCardDirective { constructor(el, renderer) { this.el = el; this.renderer = renderer; this.ncGradientSize = 200; // 渐变大小,单位px this.ncGradientColor = '#D9D9D955'; // 内部渐变颜色 this.ncGradientOpacity = 0.8; // 内部渐变透明度 this.ncGradientFrom = '#9E7AFF'; // 边框渐变开始颜色 this.ncGradientTo = '#FE8BBB'; // 边框渐变结束颜色 this.ncCardBackground = 'white'; // 卡片背景颜色 this.mouseX = -200; this.mouseY = -200; this.handleMouseEnter = () => { this.renderer.setStyle(this.borderElement, 'opacity', '1'); this.renderer.setStyle(this.gradientElement, 'opacity', this.ncGradientOpacity.toString()); }; this.handleMouseMove = (e) => { const rect = this.el.nativeElement.getBoundingClientRect(); this.mouseX = e.clientX - rect.left; this.mouseY = e.clientY - rect.top; this.updateGradients(); }; this.handleMouseLeave = () => { this.mouseX = -this.ncGradientSize; this.mouseY = -this.ncGradientSize; this.updateGradients(); this.renderer.setStyle(this.borderElement, 'opacity', '0'); this.renderer.setStyle(this.gradientElement, 'opacity', '0'); }; } ngOnInit() { this.setupMagicCard(); this.addEventListeners(); this.initializeMousePosition(); } setupMagicCard() { const element = this.el.nativeElement; // 设置主容器样式 const elementPosition = element.style.position; if (!elementPosition || elementPosition === 'static') { this.renderer.setStyle(element, 'position', 'relative'); } this.renderer.addClass(element, 'nc-magic-card'); // 获取容器的边框宽度 const computedStyle = getComputedStyle(element); const borderTopWidth = parseFloat(computedStyle.borderTopWidth) || 0; const borderRightWidth = parseFloat(computedStyle.borderRightWidth) || 0; const borderBottomWidth = parseFloat(computedStyle.borderBottomWidth) || 0; const borderLeftWidth = parseFloat(computedStyle.borderLeftWidth) || 0; // 如果没有边框,默认使用1px const maxBorderWidth = Math.max(borderTopWidth, borderRightWidth, borderBottomWidth, borderLeftWidth); const borderWidth = maxBorderWidth > 0 ? maxBorderWidth : 1; // 创建边框元素 - 根据实际边框宽度向外扩展 this.borderElement = this.renderer.createElement('div'); this.renderer.addClass(this.borderElement, 'nc-magic-card-border'); this.renderer.setStyle(this.borderElement, 'pointer-events', 'none'); this.renderer.setStyle(this.borderElement, 'position', 'absolute'); this.renderer.setStyle(this.borderElement, 'inset', `-${borderWidth}px`); // 根据边框宽度动态设置 this.renderer.setStyle(this.borderElement, 'border-radius', 'inherit'); this.renderer.setStyle(this.borderElement, 'transition', 'opacity 300ms ease'); this.renderer.setStyle(this.borderElement, 'opacity', '0'); this.renderer.setStyle(this.borderElement, 'z-index', '1'); // 创建背景元素 - 隐藏边框层的内部区域 this.backgroundElement = this.renderer.createElement('div'); this.renderer.addClass(this.backgroundElement, 'nc-magic-card-background'); this.renderer.setStyle(this.backgroundElement, 'pointer-events', 'none'); this.renderer.setStyle(this.backgroundElement, 'position', 'absolute'); this.renderer.setStyle(this.backgroundElement, 'inset', '0'); this.renderer.setStyle(this.backgroundElement, 'border-radius', 'inherit'); this.renderer.setStyle(this.backgroundElement, 'background-color', this.ncCardBackground); this.renderer.setStyle(this.backgroundElement, 'z-index', '2'); // 创建渐变元素 this.gradientElement = this.renderer.createElement('div'); this.renderer.addClass(this.gradientElement, 'nc-magic-card-gradient'); this.renderer.setStyle(this.gradientElement, 'pointer-events', 'none'); this.renderer.setStyle(this.gradientElement, 'position', 'absolute'); this.renderer.setStyle(this.gradientElement, 'inset', '0'); this.renderer.setStyle(this.gradientElement, 'border-radius', 'inherit'); this.renderer.setStyle(this.gradientElement, 'transition', 'opacity 300ms ease'); this.renderer.setStyle(this.gradientElement, 'opacity', '0'); this.renderer.setStyle(this.gradientElement, 'z-index', '4'); // 原始内容位于背景层之上,渐变层之下 const children = Array.from(element.children); children.forEach((child) => { this.renderer.setStyle(child, 'position', 'relative'); this.renderer.setStyle(child, 'z-index', '3'); }); // 添加效果层到原始元素 this.renderer.appendChild(element, this.borderElement); this.renderer.appendChild(element, this.backgroundElement); this.renderer.appendChild(element, this.gradientElement); } addEventListeners() { const element = this.el.nativeElement; this.renderer.listen(element, 'mousemove', this.handleMouseMove); this.renderer.listen(element, 'mouseleave', this.handleMouseLeave); this.renderer.listen(element, 'mouseenter', this.handleMouseEnter); } initializeMousePosition() { this.mouseX = -this.ncGradientSize; this.mouseY = -this.ncGradientSize; this.updateGradients(); } updateGradients() { if (this.borderElement && this.gradientElement) { // 更新边框渐变 const borderGradient = `radial-gradient(${this.ncGradientSize}px circle at ${this.mouseX}px ${this.mouseY}px, ${this.ncGradientFrom}, ${this.ncGradientTo}, transparent 100%)`; this.renderer.setStyle(this.borderElement, 'background', borderGradient); // 更新内部渐变 const innerGradient = `radial-gradient(${this.ncGradientSize}px circle at ${this.mouseX}px ${this.mouseY}px, ${this.ncGradientColor}, transparent 100%)`; this.renderer.setStyle(this.gradientElement, 'background', innerGradient); } } } MagicCardDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: MagicCardDirective, deps: [{ token: i0.ElementRef }, { token: i0.Renderer2 }], target: i0.ɵɵFactoryTarget.Directive }); MagicCardDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "12.1.5", type: MagicCardDirective, selector: "[ncMagicCard]", inputs: { ncGradientSize: "ncGradientSize", ncGradientColor: "ncGradientColor", ncGradientOpacity: "ncGradientOpacity", ncGradientFrom: "ncGradientFrom", ncGradientTo: "ncGradientTo", ncCardBackground: "ncCardBackground" }, ngImport: i0 }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: MagicCardDirective, decorators: [{ type: Directive, args: [{ selector: '[ncMagicCard]' }] }], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: i0.Renderer2 }]; }, propDecorators: { ncGradientSize: [{ type: Input }], ncGradientColor: [{ type: Input }], ncGradientOpacity: [{ type: Input }], ncGradientFrom: [{ type: Input }], ncGradientTo: [{ type: Input }], ncCardBackground: [{ type: Input }] } }); class NcMagicCardModule { } NcMagicCardModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: NcMagicCardModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); NcMagicCardModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: NcMagicCardModule, declarations: [MagicCardDirective], exports: [MagicCardDirective] }); NcMagicCardModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: NcMagicCardModule, imports: [[]] }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: NcMagicCardModule, decorators: [{ type: NgModule, args: [{ declarations: [ MagicCardDirective ], imports: [], exports: [ MagicCardDirective ] }] }] }); /** * Generated bundle index. Do not edit. */ export { MagicCardDirective, NcMagicCardModule }; //# sourceMappingURL=ng-cw-v12-magic-card.js.map