当前位置: 首页 > wzjs >正文

沙漠风网站建设公司建设网站的费用

沙漠风网站建设公司,建设网站的费用,网站建设活动方案,四川seo优化背景 工具提示(tooltip)是一个常见的 UI 组件,用于在用户与页面元素交互时提供额外的信息。由于angular/material/tooltip的matTooltip只能显示纯文本,所以我们可以通过自定义Directive来实现一个灵活且功能丰富的tooltip Overlay…

背景

工具提示(tooltip)是一个常见的 UI 组件,用于在用户与页面元素交互时提供额外的信息。由于angular/material/tooltip的matTooltip只能显示纯文本,所以我们可以通过自定义Directive来实现一个灵活且功能丰富的tooltip

Overlay

OverlayRefattach()支持ComponentPortalTemplatePortal等,为了统一管理overlay的内容,我们需要创建一个OverlayToolTipComponent用来展示具体的tooltip

@Component({selector: 'overlay-tooltip-inner',template: `<div class="overlay-tooltip-inner">@if (text) {<div>{{ text }}</div>} @else {<ng-container *ngTemplateOutlet="contentTemplate"></ng-container>}</div>`,styles: [`.overlay-tooltip-inner {padding: 5px;background-color:rgb(207, 229, 248);border-radius: 4px;box-shadow: 0px 2px 6px rgba(0, 0, 0, 0.2);}`],standalone: false
})
export class OverlayToolTipComponent {@Input()set overlayTooltip(tooltip: string | TemplateRef<any>) {if (_.isString(tooltip)) {this.text = tooltip;} else {this.contentTemplate = tooltip;}}text: string;contentTemplate: TemplateRef<any>;constructor() {//}
}

OverlayToolTipDirective

接下来创建OverlayToolTipDirective,它接受的tooltip参数类型是string | TemplateRef<any>

@Directive({selector: '[overlayTooltip]',standalone: false
})
export class OverlayToolTipDirective implements OnChanges, OnDestroy {private _overlayRef: OverlayRef = undefined;private _tooltip: string | TemplateRef<any> = '';@Input()set overlayTooltip(tooltip: string | TemplateRef<any>) {this._tooltip = tooltip ?? '';}private flexibleConnectedPositionStrategy: FlexibleConnectedPositionStrategy;constructor(private _overlay: Overlay,private _overlayPositionBuilder: OverlayPositionBuilder,private _elementRef: ElementRef) {//}ngOnChanges(changes: SimpleChanges): void {if (_.size(this._tooltip) > 0) {this.updateFlexibleConnectedPositionStrategy();this.bindingTriggers();}}private updateFlexibleConnectedPositionStrategy() {this.flexibleConnectedPositionStrategy = this._overlayPositionBuilder.flexibleConnectedTo(this._elementRef).withPositions([this.createPosition('center', 'top', 'center', 'bottom')]);}private generateOverlayRef() {if (!this.flexibleConnectedPositionStrategy) {this.updateFlexibleConnectedPositionStrategy();}this._overlayRef = this._overlay.create({ positionStrategy: this.flexibleConnectedPositionStrategy });}private createPosition(originX: HorizontalConnectionPos, originY: VerticalConnectionPos,overlayX: HorizontalConnectionPos, overlayY: VerticalConnectionPos): ConnectionPositionPair {return { originX, originY, overlayX, overlayY };}private bindingTriggers() {this._elementRef.nativeElement.addEventListener('mouseover', this.show());this._elementRef.nativeElement.addEventListener('mouseout', this.hide());}private show() {if (!this._overlayRef) {this.generateOverlayRef();}if (this._overlayRef && !this._overlayRef.hasAttached()) {const tooltipRef: ComponentRef<OverlayToolTipComponent> = this._overlayRef.attach(new ComponentPortal(OverlayToolTipComponent));tooltipRef.instance.overlayTooltip = this._tooltip;}}private hide() {if (!_.isEmpty(this._overlayRef) && this._overlayRef.hasAttached()) {this._overlayRef.detach();}}private cleanUpOverlayRef() {if (this._overlayRef?.dispose) {this._overlayRef.dispose();this._overlayRef = undefined;}}ngOnDestroy() {this.cleanUpOverlayRef();this.removeExistingListeners();}removeExistingListeners() {this._elementRef.nativeElement.removeEventListener('mouseover', this.show());this._elementRef.nativeElement.removeEventListener('mouseout', this.hide());}
}

效果如下:

位置自适应

由上图可以看出,当位置不够容纳tooltip时,目标元素会被遮挡。所以我们需要添加placementautoPosition允许用户指定tooltip的位置和tooltip是否可以自适应位置

通过OverlayPositionBuilderwithPositions()设置position数组。

class ConnectionPositionPairExt extends ConnectionPositionPair {sort: number;
}export class OverlayToolTipDirective implements OnChanges, OnDestroy {
...@Input() placement: 'top' | 'bottom' | 'left' | 'right' = 'top';@Input() autoPosition = true;// updateFlexibleConnectedPositionStrategy() 更改如下:private updateFlexibleConnectedPositionStrategy() {this.flexibleConnectedPositionStrategy = this._overlayPositionBuilder.flexibleConnectedTo(this._elementRef).withPositions(this.getAvailablePositions());}private getAvailablePositions(): ConnectionPositionPairExt[] {// 生成四个方向的默认位置配置const positions = [this.createPosition('center', 'top', 'center', 'bottom', 1), // topthis.createPosition('start', 'center', 'end', 'center', 2), // leftthis.createPosition('center', 'bottom', 'center', 'top', 3), // bottomthis.createPosition('end', 'center', 'start', 'center', 4), // right];// 根据当前 placement 设置优先级const priorityMap: { [key in string]: number } = {['bottom']: 2,['left']: 1,['right']: 3,};positions[priorityMap[this.placement] || 0].sort = 0;// 返回排序后的位置配置return this.autoPosition ? positions.sort((a, b) => a.sort - b.sort) : [positions[priorityMap[this.placement] || 0]];}
...
}

效果如下,string或者template

总结

这样我们就在不引入其他库的前提下完成了一个内容丰富位置灵活的tooltip组件啦。

要注意,在tooltip被触发时再创建OverlayRef以避免不必要的性能开销。当tooltip隐藏和Directive销毁时,删除事件监听并调用OverlayRef的detach()dispose()

另外,Overlay的ConnectedPosition还可以指定tooltip和目标元素之间的距离,也可以增加panelClass以便深度定制tooltip的内容。


文章转载自:

http://5pZJce81.whcLz.cn
http://ZtyyMwaX.whcLz.cn
http://XzYcKbOY.whcLz.cn
http://1u36Ngdc.whcLz.cn
http://AzQXejtE.whcLz.cn
http://Hw6EUxu9.whcLz.cn
http://3XGrR8MF.whcLz.cn
http://emJ397RQ.whcLz.cn
http://BLsqVGKN.whcLz.cn
http://2JpF4Qu5.whcLz.cn
http://DHlaJyy3.whcLz.cn
http://Kzd4tpIp.whcLz.cn
http://InrMNWvZ.whcLz.cn
http://E2gsxaBw.whcLz.cn
http://mtFuPJ9C.whcLz.cn
http://RnoH54n6.whcLz.cn
http://ktD1Gxal.whcLz.cn
http://TGmeNvg9.whcLz.cn
http://SwjzWn1j.whcLz.cn
http://wXnx70df.whcLz.cn
http://XjtvBxNv.whcLz.cn
http://t2qAIDw6.whcLz.cn
http://ztKJbh8c.whcLz.cn
http://DTyKs4Hp.whcLz.cn
http://Q9mSYQ6L.whcLz.cn
http://qvyGtaL4.whcLz.cn
http://0j35Ia6w.whcLz.cn
http://Su1Bdmjz.whcLz.cn
http://ibrkuGLP.whcLz.cn
http://8LztjnoO.whcLz.cn
http://www.dtcms.com/wzjs/761497.html

相关文章:

  • 云南网站设计定制字体怎么安装到电脑wordpress
  • 电子商务网站设计与制作网站结构优化包括什么
  • 网站源代码上传兴义市 网站建设
  • 一个企业网站多少钱网站搭建中企动力第一
  • 网站建设优選宙斯站长欧模网室内设计网
  • 福州做网站建设网站制作设计机构
  • 做网站的相关协议北京网站模仿
  • 如何建设百度网站大连网站建设与维护题库
  • 公司网站开发费分录是网站建设完整方案
  • 旅游网站建设方案之目标自己做的网站可以用于百度推广吗
  • 网站模板生成爱奇艺推广联盟
  • 问答网站如何优化网络营销推广外包服务
  • 淮南帮wordpress 谷歌seo
  • 大连做网站wordpress widget
  • 为什么百度地图嵌入网站不显示用户界面设计报告
  • 策划书模板免费下载的网站太原网站优化怎么做
  • 微购物网站建设50个优秀网站
  • 扬州建设机械网站做58招聘网站工作人员的心得
  • 视频网站建设公司网站的付款链接怎么做的
  • 石油化工建设工程网站郑州市建设厅官方网站
  • 无锡自助网站成都哪家公司做网站
  • 秦皇岛网站制作与网站建设仿淘宝网站
  • 塘沽网站制作公司深圳龙华住房和建设局网站官网
  • jsp网站建设代码做公司网站页面
  • 收录网站制作哪个着陆页网站
  • 北京网站推广外包线上店免费推广的软件
  • 网站建设前需求调研表景观小品设计网站推荐
  • 会员制网站 建设游戏社的公众号是?
  • 常德外贸网站优化推广网站模板是怎么制作
  • 扁平式的网站阳江建设网站