繁体   English   中英

跟随鼠标移动透明 div - 如何使页面背景变暗但不使 div 内部变暗?

[英]Moving transparent div that follows mouse - how can I darken the background of a page but not darken the inside of the div?

我有一个我自己创建的“辅助工具栏”——类似于 UserWay,但任何人都可以免费使用。

我正在尝试创建一个“可读性栏”,将 100% 宽度的 div 添加到页面并使用用户鼠标移动。 本质上,这允许用户更轻松地关注和阅读页面的特定区域。 我有这个运作良好。 但是,我想使 div 外部的页面变暗,而不是使 div 的内部变暗。 类似于在页面上扔一个透明的变暗层,但在这个可读性栏的内部完全删除它 - 它会不断地在页面上上下移动以跟随光标。

关于如何解决这个问题的任何想法?

 let readabilityDiv = document.getElementById('readability'); let body = document.querySelector('body'); function isTouchDevice(){ try{ document.createEvent("TouchEvent"); return true; } catch(e){ return false; } } const move = (e) =>{ //Try, catch to avoid errors for touch screens - Error thrown when user doesn't move their finger try{ //PageX and PageY return the position of clients cursor from top left of screen var y = !isTouchDevice() ? e.pageY: e.touches[0].pageY; } catch(e){} //set let and top based on mouse pointer readabilityDiv.style.top = y -50 + "px"; } //for mouse document.addEventListener("mousemove", (e) => { move(e); }); //forTouch document.addEventListener("touchmove", (e) => { move(e); })
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> html{ font-size: 10px; } body{ padding: 10px; margin: 0; background: white; font-size:1.6rem; box-shadow: inset 1px 1px 1px #ccc; } #readability{ width: 100%; height: 9.6rem; position: absolute; left: 0; border-top: 5px solid #000; border-bottom: 5px solid #000; } </style> </head> <body> <div id="readability"></div> <h1>Welcome to my page</h1> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Earum tempore maxime officia nobis debitis, dolorum consectetur ducimus temporibus repellendus voluptates quaerat praesentium, amet aperiam quis a ea, facere eum dignissimos id numquam consequatur non excepturi molestiae. Nisi temporibus, quia, tenetur cumque nulla corrupti, autem nesciunt fugiat veritatis voluptate mollitia nostrum.</p> <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Necessitatibus natus possimus ex tempore? Iste pariatur fuga officia dolorum soluta, ratione temporibus assumenda distinctio, nostrum voluptates mollitia, quo modi fugiat nihil veniam ullam ipsam suscipit voluptatem accusamus incidunt deleniti aperiam commodi reiciendis. Laboriosam quis, repellendus ducimus vero consequatur cupiditate eius quae excepturi reprehenderit cum obcaecati? Iste dolor, voluptas libero laborum dolores magni ex voluptatum aliquam, sequi, atque asperiores iure iusto enim facilis optio nemo quos? Corrupti iste necessitatibus autem eaque commodi? Cumque sed numquam assumenda iste. Corporis optio, modi aut a cumque ullam quo repellendus vero provident adipisci similique aperiam qui.</p> <script src="script.js" type="text/javascript"></script> </body> </html>

有很多方法可以做到这一点。 一种方法是使用散布较大的盒子阴影。

 let cursor = document.getElementById('cursor'); const onMouseMove = (e) =>{ cursor.style.left = e.pageX - 50 + 'px'; cursor.style.top = e.pageY - 50 + 'px'; } document.addEventListener('mousemove', onMouseMove);
 #cursor { position: fixed; width: 100px; height: 100px; border-radius: 100%; box-shadow: 0 0 0 200vw rgba(0,0,0,0.6); left: 0; top: 0; pointer-events: none; }
 <div id="cursor"></div> <h2>What is Lorem Ipsum?</h2> <p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> </div><div> <h2>Why do we use it?</h2> <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p> </div><br><div> <h2>Where does it come from?</h2> <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p><p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p> </div><div> <h2>Where can I get some?</h2> <p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</p> </div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM