【问题标题】:Angular 2 tooltip should auto adjust position itself in mobile or when window is resizedAngular 2 工具提示应在移动设备中或在调整窗口大小时自动调整位置
【发布时间】:2017-08-09 02:08:03
【问题描述】:

我正在寻找 Angular 2 工具提示,它应该在移动设备中或在调整窗口大小时自动调整位置本身(至少从左侧和右侧),以便它应该在屏幕上完全可见。我正在 Plunker 上分享我当前的代码,如果有人可以帮助我,我将不胜感激。谢谢。

Tooltip Plunk

Tooltip Image

我正在使用的这个位置函数:

 private positionElements(hostEl: HTMLElement, targetEl: HTMLElement, positionStr: string, appendToBody: boolean = false): { top: number, left: number } {
        let positionStrParts = positionStr.split("-");
        let pos0 = positionStrParts[0];
        let pos1 = positionStrParts[1] || "center";
        let hostElPos = appendToBody ? this.offset(hostEl) : this.position(hostEl);
        let targetElWidth = targetEl.offsetWidth;
        let targetElHeight = targetEl.offsetHeight;
        let shiftWidth: any = {
             center: function (): number {
                return hostElPos.left + hostElPos.width / 2 - targetElWidth / 2;
            },
            left: function (): number {
                return hostElPos.left;
            },
            right: function (): number {
                return hostElPos.left + hostElPos.width;
            }
        };

        let shiftHeight: any = {
            center: function (): number {
                return hostElPos.top + hostElPos.height / 2 - targetElHeight / 2;
            },
            top: function (): number {
                return hostElPos.top;
            },
            bottom: function (): number {
                return hostElPos.top + hostElPos.height;
            }
        };

        let targetElPos: { top: number, left: number };
        switch (pos0) {
            case "right":
                targetElPos = {
                    top: shiftHeight[pos1](),
                    left: shiftWidth[pos0]()
                };
                break;

            case "left":
                targetElPos = {
                    top: shiftHeight[pos1](),
                    left: hostElPos.left - targetElWidth
                };
                break;

            case "bottom":
                targetElPos = {
                    top: shiftHeight[pos0](),
                    left: shiftWidth[pos1](this.useAlternateMobileTooltip)
                };
                break;

            default:
                targetElPos = {
                    top: hostElPos.top - targetElHeight,
                    left: shiftWidth[pos1]()
                };
                break;
        }

        return targetElPos;
    }

    private position(nativeEl: HTMLElement): { width: number, height: number, top: number, left: number } {
        let offsetParentBCR = { top: 0, left: 0 };
        const elBCR = this.offset(nativeEl);
        const offsetParentEl = this.parentOffsetEl(nativeEl);
        if (offsetParentEl !== window.document) {
            offsetParentBCR = this.offset(offsetParentEl);
            offsetParentBCR.top += offsetParentEl.clientTop - offsetParentEl.scrollTop;
            offsetParentBCR.left += offsetParentEl.clientLeft - offsetParentEl.scrollLeft;
        }

        const boundingClientRect = nativeEl.getBoundingClientRect();
        return {
            width: boundingClientRect.width || nativeEl.offsetWidth,
            height: boundingClientRect.height || nativeEl.offsetHeight,
            top: elBCR.top - offsetParentBCR.top,
            left: elBCR.left - offsetParentBCR.left
        };
    }

    private offset(nativeEl: any): { width: number, height: number, top: number, left: number } {
        const boundingClientRect = nativeEl.getBoundingClientRect();
        return {
            width: boundingClientRect.width || nativeEl.offsetWidth,
            height: boundingClientRect.height || nativeEl.offsetHeight,
            top: boundingClientRect.top + (window.pageYOffset || window.document.documentElement.scrollTop),
            left: boundingClientRect.left + (window.pageXOffset || window.document.documentElement.scrollLeft)
        };
    }

    private getStyle(nativeEl: HTMLElement, cssProp: string): string {
        if ((nativeEl as any).currentStyle) // IE
            return (nativeEl as any).currentStyle[cssProp];

        if (window.getComputedStyle)
            return (window.getComputedStyle(nativeEl) as any)[cssProp];

        // finally try and get inline style
        return (nativeEl.style as any)[cssProp];
    }

    private isStaticPositioned(nativeEl: HTMLElement): boolean {
        return (this.getStyle(nativeEl, "position") || "static") === "static";
    }

    private parentOffsetEl(nativeEl: HTMLElement): any {
        let offsetParent: any = nativeEl.offsetParent || window.document;
        while (offsetParent && offsetParent !== window.document && this.isStaticPositioned(offsetParent)) {
            offsetParent = offsetParent.offsetParent;
        }
        return offsetParent || window.document;
    }

【问题讨论】:

    标签: javascript angular typescript tooltip angular2-directives


    【解决方案1】:

    试试这个:

    左侧的工具提示位置 = hostElPos.left

    从右边开始的工具提示位置 = 当前窗口宽度 - hostElPos.left - hostElPos.width;

    现在, 第一个工具提示 = 离左边缘最近 最后一个工具提示 = 离右边缘最近

    将此信息与中心功能一起使用,以在第一个/最后一个工具提示上应用样式。使用移动媒体查询。

    HTML:

    [class.alternate-left]="useLeft"
    [class.alternate-right]=“useRight"
    

    组件:

    useLeft = false;
    useRight = false;
    
    let useWindowWidth = window.screen.width;
            let targetElemetOnLeft = hostElPos.left;
            let targetElemetOnRight = (useWindowWidth - hostElPos.left) - hostElPos.width;
            this.useLeft = (hostElPos.width < 90 && targetElemetOnLeft < 30);
            this.useRight = (hostElPos.width < 90 && targetElemetOnRight < 50);
    
    center: function (isLeft: boolean, isRight: boolean): number {
                   if (isLeft) {
                        return hostElPos.left;
                    }
                    else if (isRight) {
                        return hostElPos.left - targetElWidth;
                    }
                    else{
                    return hostElPos.left + hostElPos.width / 2 - targetElWidth / 2;
                    }
    

    Working Plunk

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-21
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      • 2012-08-02
      • 2017-03-12
      相关资源
      最近更新 更多