UNPKG

ng-cw-v12

Version:

Angular UI Component Library

170 lines (165 loc) 9.52 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; /** 渐变大小,单位px */ this.ncGradientSize = 200; /** 内部渐变颜色,可以是单色或颜色数组 */ this.ncGradientColor = '#D9D9D955'; /** 内部渐变透明度 */ this.ncGradientOpacity = 0.8; /** 边框颜色,可以是单色或颜色数组 */ this.ncBorderColor = ["#9E7AFF", "#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 computedStyle = getComputedStyle(element); // 设置主容器样式 const elementPosition = computedStyle.position; if (!elementPosition || elementPosition === 'static') { this.renderer.setStyle(element, 'position', 'relative'); } this.renderer.addClass(element, 'nc-magic-card'); // 获取容器的边框宽度 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) => { const childPosition = getComputedStyle(child).position; if (!childPosition || childPosition === 'static') { 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 borderColors = Array.isArray(this.ncBorderColor) ? this.ncBorderColor.join(',') : this.ncBorderColor; const borderGradient = `radial-gradient(${this.ncGradientSize}px circle at ${this.mouseX}px ${this.mouseY}px, ${borderColors}, transparent)`; this.renderer.setStyle(this.borderElement, 'background', borderGradient); // 更新内部渐变 const innerColors = Array.isArray(this.ncGradientColor) ? this.ncGradientColor.join(',') : this.ncGradientColor; const innerGradient = `radial-gradient(${this.ncGradientSize}px circle at ${this.mouseX}px ${this.mouseY}px, ${innerColors}, 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", ncBorderColor: "ncBorderColor", 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 }], ncBorderColor: [{ 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