实现聚焦效果

2022年05月12日 阅读数:9
这篇文章主要向大家介绍实现聚焦效果,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

image.png

这是以前朋友问个人一个功能:他以为看网页有时候注意力会被转移,但愿能够有个蒙层帮助他集中注意力app

反手我就用 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

在我测试中发现了一些比较阴间的效果测试

image.png

因此咱们要小改动一下。直接给父级 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 复原一下。spa

(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;
            }
        })
    })()

最佳实现?

通过咱们通常操做,终于功能能实现了。code

可是这种的功能真的是咱们想要的嘛? 咱们只是想实现一下聚焦功能,并不但愿页面布局遭到破坏orm

那咱们应该怎么作呢?从上面的例子能够看到,box-shadow 是最佳的实现,他能够给咱们一个视口,将视口外的内容所有盖住,那么咱们只要控制好视口的大小便可。blog

这样咱们还能够对视口作一些特殊的样式处理。seo

固然你会说若是上面盖一层东西,会致使元素没法选中,这里咱们可使用 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'
            
            
        })
    })()

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

image.png

(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; 让动画会有一个过渡效果