关于javascript:实现聚焦效果

46次阅读

共计 3816 个字符,预计需要花费 10 分钟才能阅读完成。

这是之前敌人问我的一个性能:他感觉看网页有时候注意力会被转移,心愿能够有个蒙层帮忙他集中注意力

反手我就用 box-shadow 把性能写了进去。

(function(){
    let lastEl = null;
    let styleEl = null;
    document.body.addEventListener('mouseover', (e)=>{e.stopPropagation();
        if(!styleEl){styleEl = document.createElement('style');
            document.body.appendChild(styleEl);
            styleEl.innerHTML = `
                .lilnong-focus{box-shadow: 0 0 0px 9999px rgba(0,0,0,.8);
                    z-index: 99999;
                    position: relative;
                }
            `
        }
        const el = e.target;
        lastEl?.classList?.remove('lilnong-focus');
        lastEl = el;
        el.classList.add('lilnong-focus');
    })
})()

因为 z-index 无奈超过非 static 层级导致的 bug

在我测试中 发现了一些比拟阳间的成果

所以咱们要小改变一下。间接给父级 ZIndex 全副晋升。

(function(){
        let lastEl = null;
        let styleEl = null;
        let ZIndex = 1;
        document.body.addEventListener('mouseover', (e)=>{e.stopPropagation();
            if(!styleEl){styleEl = document.createElement('style');
                document.body.appendChild(styleEl);
                styleEl.innerHTML = `
                    .lilnong-focus{box-shadow: 0 0 0px 9999px rgba(0,0,0,.8);
                        z-index: 99999;
                        position: relative;
                    }
                `
            }
            const el = e.target;
            lastEl?.classList?.remove('lilnong-focus');
            lastEl = el;
            el.classList.add('lilnong-focus');
            
            let parent = el;
            ZIndex++;
            while(parent){console.log(parent?.style)
                if(parent.style) parent.style.zIndex = 10000 +  ZIndex;
                parent = parent.parentNode;
            }
        })
    })()

因为 overflow 导致款式无奈超出盒子

那好了,咱们再把 overflow 还原一下。

(function(){
        let lastEl = null;
        let styleEl = null;
        let ZIndex = 1;
        document.body.addEventListener('mouseover', (e)=>{e.stopPropagation();
            if(!styleEl){styleEl = document.createElement('style');
                document.body.appendChild(styleEl);
                styleEl.innerHTML = `
                    .lilnong-focus{box-shadow: 0 0 0px 9999px rgba(0,0,0,.8);
                        z-index: 99999;
                        position: relative;
                    }
                    .lilnong
                `
            }
            const el = e.target;
            lastEl?.classList?.remove('lilnong-focus');
            lastEl = el;
            el.classList.add('lilnong-focus');
            
            let parent = el;
            ZIndex++;
            while(parent){// console.log(parent?.style)
                if(parent.style){
                    // parent.style.zIndex = 10000 +  ZIndex;
                    // overflow: visible !important;
                    // parent.style.overflow = 'visible !important'
                    parent.setAttribute('style', `${parent.getAttribute('style')};z-index: ${10000 +  ZIndex};overflow: visible !important;`)
                }
                parent = parent.parentNode;
            }
        })
    })()

最佳实现?

通过咱们个别操作,终于性能能实现了。

然而这种的性能真的是咱们想要的嘛? 咱们只是想实现一下 聚焦性能 ,并 不心愿页面布局受到毁坏

那咱们应该怎么做呢?从下面的例子能够看到,box-shadow 是最佳的实现,他能够给咱们一个视口,将视口外的内容全副盖住,那么咱们只有管制好视口的大小即可。

这样咱们还能够对视口做一些非凡的款式解决。

当然你会说如果下面盖一层货色,会导致元素无奈选中,这里咱们能够应用 pointer-events: none; 来阻止元素接管事件。(这个罕用在头像挂件显示,一般来说单击头像是弹出资料卡。挂件咱们阻止接管事件即可。)

(function(){let maskEl = document.querySelector('.lilnong-mask') || document.createElement('div');
        maskEl.className="lilnong-mask"
        document.body.appendChild(maskEl);
    
        let styleEl = document.createElement('style');
        document.body.appendChild(styleEl);
    
        styleEl.innerHTML = `
            .lilnong-mask{box-shadow: 0 0 0px 9999px rgba(0,0,0,.8);
                z-index: 99999;
                position: fixed;
                top: 0;
                left: 0;
                width: 40px;
                height: 40px;
                pointer-events: none;
            }
        `
    
        document.body.addEventListener('mousemove', (e)=>{e.stopPropagation();
            const el = e.target;
            const {x,y,width,height,top,left} = el.getBoundingClientRect();
            maskEl.style.left = left + 'px'
            maskEl.style.top = top + 'px'
            maskEl.style.width = width + 'px'
            maskEl.style.height = height + 'px'
            
            
        })
    })()

甚至说咱们还能够 批改一下聚焦视口的款式

(function(){let maskEl = document.querySelector('.lilnong-mask') || document.createElement('div');
        maskEl.className="lilnong-mask"
        document.body.appendChild(maskEl);
    
        let styleEl = document.createElement('style');
        document.body.appendChild(styleEl);
    
        styleEl.innerHTML = `
            .lilnong-mask{box-shadow: 0 0 0px 9999px rgba(0,0,0,.8);
                z-index: 99999;
                position: fixed;
                top: 0;
                left: 0;
                width: 40px;
                height: 40px;
                pointer-events: none;

                padding: 10px;
                box-sizing: content-box;
                transform: translate(-10px,-10px);
                border-radius: 10px
            }
            .lilnong-mask:before{
                content: '';
                position: absolute;
                top: -6px;right: -6px;bottom: -6px;left: -6px;
                border: 1px dashed #eee;
                border-radius: 10px
            }
        `
    
        document.body.addEventListener('mousemove', (e)=>{e.stopPropagation();
            const el = e.target;
            const {x,y,width,height,top,left} = el.getBoundingClientRect();
            maskEl.style.left = left + 'px'
            maskEl.style.top = top + 'px'
            maskEl.style.width = width + 'px'
            maskEl.style.height = height + 'px'
            
            
        })
    })()

因为是 left、top、width、height 的变动,所以咱们还能够 transition: .2s all; 让动画会有一个过渡成果

正文完
 0