实现聚焦效果

image.png

这是之前朋友问我的一个功能:他觉得看网页有时候注意力会被转移,希望可以有个蒙层帮助他集中注意力

反手我就用 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 复原一下。

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

甚至说我们还可以修改一下聚焦视口的样式

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


javascript-lNong
只此一生,何必从众

Read-Search-Ask

27.4k 声望
8.6k 粉丝
0 条评论
推荐阅读
疫情已过,2023 我的前端面试记录
顺利入职。把我最近找工作的心得记录下来。工作交接确定 lastday整理手头工作,相关对接人、交接人放文档中工作交接过渡阶段。做好被咨询者,该拉人拉人,该拉群拉群平时沟通顺畅的同事如果没有 WX 可以加一个属...

linong1阅读 12

从零搭建 Node.js 企业级 Web 服务器(零):静态服务
过去 5 年,我前后在菜鸟网络和蚂蚁金服做开发工作,一方面支撑业务团队开发各类业务系统,另一方面在自己的技术团队做基础技术建设。期间借着 Node.js 的锋芒做了不少 Web 系统,有的至今生气蓬勃、有的早已夭折...

乌柏木172阅读 13.9k评论 10

手把手教你写一份优质的前端技术简历
不知不觉一年一度的秋招又来了,你收获了哪些大厂的面试邀约,又拿了多少offer呢?你身边是不是有挺多人技术比你差,但是却拿到了很多大厂的offer呢?其实,要想面试拿offer,首先要过得了简历那一关。如果一份简...

tonychen152阅读 17.7k评论 5

封面图
正则表达式实例
收集在业务中经常使用的正则表达式实例,方便以后进行查找,减少工作量。常用正则表达式实例1. 校验基本日期格式 {代码...} {代码...} 2. 校验密码强度密码的强度必须是包含大小写字母和数字的组合,不能使用特殊...

寒青56阅读 8.4k评论 11

JavaScript有用的代码片段和trick
平时工作过程中可以用到的实用代码集棉。判断对象否为空 {代码...} 浮点数取整 {代码...} 注意:前三种方法只适用于32个位整数,对于负数的处理上和Math.floor是不同的。 {代码...} 生成6位数字验证码 {代码...} ...

jenemy48阅读 7k评论 12

从零搭建 Node.js 企业级 Web 服务器(十五):总结与展望
总结截止到本章 “从零搭建 Node.js 企业级 Web 服务器” 主题共计 16 章内容就更新完毕了,回顾第零章曾写道:搭建一个 Node.js 企业级 Web 服务器并非难事,只是必须做好几个关键事项这几件必须做好的关键事项就...

乌柏木75阅读 7.1k评论 16

再也不学AJAX了!(二)使用AJAX ① XMLHttpRequest
「再也不学 AJAX 了」是一个以 AJAX 为主题的系列文章,希望读者通过阅读本系列文章,能够对 AJAX 技术有更加深入的认识和理解,从此能够再也不用专门学习 AJAX。本篇文章为该系列的第二篇,最近更新于 2023 年 1...

libinfs42阅读 6.8k评论 12

封面图

Read-Search-Ask

27.4k 声望
8.6k 粉丝
宣传栏