实现聚焦效果

更新日期: 2022-05-11阅读: 635标签: 效果


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

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

来自:https://segmentfault.com/a/1190000041804137

链接: https://www.fly63.com/article/detial/11480

使用 JavaScript 实现分屏视觉效果

今天这篇文章就来讲讲使用JavaScript来实现这种分屏的视觉UI效果。现在在网站上这种分屏视觉效果应用的也非常广泛,比如 Corsair website。

CSS3的过渡效果,使用transition实现鼠标移入/移出效果

在css中使用伪类虽然实现了样式的改变,但由于没有过渡效果会显得很生硬。以前如果要实现过渡,就需要借助第三方的js框架来实现。现在只需要使用CSS3的过渡(transition)功能,就可以从一组样式平滑的切换到另一组样式。

js如何实现新手引导效果?

js最近有个小伙伴问到了怎么实现新手引导的效果,然后便去网上找了下实现方案。可以通过css的border来实现。

css3 斜切角/斜边的实现方式

设计图含有斜切角的效果时,我们一般想到的方法是切出四个角为背景,然后用border连起来,这样就能显示出该效果了,那么直接使用css呢?下面就整理css做斜边的效果。

JavaScript 实现打字机效果,跑马灯效果

这篇文章在不使用任何插件的情况,以最简洁的原生javascript来实现打字机效果和跑马灯效果。打字效果即把一段话一个字一个字的显示出来。

CSS遮罩效果(模糊效果,阴影效果,毛玻璃效果)

一般遮罩加上透明度opacity就是阴影效果。阴影效果和一般遮罩一样,唯一不同的是设置.mask遮罩的背景色用rgba()表示,当然hsla()也是可以的。模糊效果(毛玻璃效果) 通过 filter来实现

纯css实现气泡效果

主要运用的是1.border 组成的直角三角形。2,before 和 after 伪元素 。3,z-index属性;将元素的长宽设置为0,并且将border的3条边设置为透明的,就会出现border颜色的直角三角形

css文字选中效果

文字选中效果,这个可能很少有人注意过。在默认状态先一般选中的文本颜色是白字蓝底的,不过可以通过CSS进行设置。::selection定义元素上的伪选择器,以便在选定元素时设置其中文本的样式。

text-fill-color:仿苹果官网介绍效果 CSS设置文字渐变效果 文字背景图遮罩

发布iPhone XR的时候 各种心动 去官网看了一遍又一遍。闲着无聊发现 里面的介绍很用大篇幅的有背景文字来介绍。Like this:看着挺酷炫的还不错 就看了下实现方式。还挺简单的。

Vue 中多个元素、组件的过渡,及列表过渡

多元素之间如何实现过渡动画效果呢?这么写行不行呢?肯定是不行的,因为 Vue 在两个元素进行切换的时候,会尽量复用dom,就是因为这个原因,导致现在动画效果不会出现。如果不让 Vue 复用dom的话,应该怎么做呢?只需要给这两个div不同的key值就行了

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!