Home > Mobile >  Moving transparent div that follows mouse - how can I darken the background of a page but not darken
Moving transparent div that follows mouse - how can I darken the background of a page but not darken

Time:07-18

I have an 'Accessibility Toolbar' of my own that I've created - similar to UserWay but free for anyone to use.

I'm trying to create a 'readability bar' that adds a 100% width div to the page and moves with a users mouse. In essence, this allows a user to more easily focus and read a specific area of a page. I have this working well. However, I'd like to darken the page outside of the div and NOT darken the interior portion of the div. Something along the lines of like throwing a transparent darkened layer over the page, BUT remove that entirely on the inside of this readability bar - which is constantly moving up and down the page to follow the cursor.

Any ideas on how to approach this?

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>

CodePudding user response:

There are many ways to do this. One way could be to use a box shadow with a large spread.

let cursor = document.getElementById('cursor');

const onm ouseMove = (e) =>{
  cursor.style.left = e.pageX - 50   'px';
  cursor.style.top = e.pageY - 50   'px';
}
document.addEventListener('mousemove', onm ouseMove);
#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>

  • Related