Guide on achieving mirror effect with cursor on a webpage

Is it feasible to achieve a cursor mirroring effect on a website using Javascript, or similar technology? Imagine a scenario where the line dividing the black and white sections of the page serves as the "reflection line." In this setup, moving the cursor in the top black segment would produce an inverse cursor image and action in the bottom white segment, and vice versa.

I would appreciate any guidance or starting points—no pun intended. Thank you!

Edit: I attempted to implement .mousemove(), event.pageX, event.pageY based on suggestions provided (as shown below). However, being new to Javascript, I encountered difficulties getting it to function properly.

cursor.html

<doctype! html>
    <head>
        <link rel="stylesheet" href="cursor.css">
        <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
        <script src="cursor.js"></script>
        <title> Cursor Mirror </title>
    </head>
    <body>
        <div class="top-half-black"></div>
        <div class="bottom-half-white">     
                <img id="mirror-image" src="upside-down.png">
        </div>

    <script>

    $(".top-half-black").mousemove(function(event){
        var xPosition = event.pageX;
        var yPosition = event.pageY;
    })


    function placeCursor() {
        var d = document.getElementById('mirror-image');
        d.style.position = "absolute";
        d.style.left = xPosition+'px';
        d.style.top = -yPosition+'px';
    }

    $( ".top-half-black").mouseover(function( event ){
        placeCursor () ;
    })



    </script>
    </body>

</html> 

cursor.css

body{
    margin:0px 0px 0px 0px;
}

.top-half-black{
    background-color:black;
    width:100%;
    height:50%;
}

Desired Outcome:

https://i.sstatic.net/cDE50.png

Answer №1

You might be interested in implementing something like this.

HTML:

<div class="section top-half-black"></div>
<div class="section bottom-half-white">
  <img id="mirror-image" src="http://i.imgur.com/cjjNbk1.png"></img>
</div>

CSS:

body{
    margin:0px 0px 0px 0px;
}

.top-half-black{
    background-color:black;
    width:100%;
    height:50%;
}

.bottom-half-white{
  position: relative;
}

#mirror-image{
  left: 0;
  top: 0;
  position: absolute;
  width: 17px;
  height: 25px;
}

.section{
  display: block;
  min-height: 10em;
}

JS:

var $img = $('#mirror-image');
var imgHeight = $img.height() / 2;
function moveImage(x, y){
  $img.css({top: y + 'px', left: x+ 'px', position:'absolute'});
}

$(".top-half-black").mousemove(function(event){
  var newY = $(this).height() - event.pageY - imgHeight;
  moveImage(event.pageX, newY);
});

To see a live demonstration, visit: http://codepen.io/alirezanoori/pen/YyagKP

Answer №2

As for altering the color scheme, I'm not entirely sure. However, if you manage to locate specific visuals of the cursor colors you desire, it's feasible to incorporate them into your CSS classes.

 cursor: url(images/my-cursor.png), auto;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to execute xmlHttp.responseXML on a server that is offline

Currently, I am diving into the world of AJAX and XML. However, I recently encountered a frustrating issue. I attempted to create a basic program that would display everything I input into an input box within a <div>. Strangely enough, my program fa ...

Struggling to figure out the method for obtaining css selectors for scraping in scrapy

Struggling with scraping a website and figuring out how css selectors work in Scrapy. The css in question: https://ibb.co/eJeZpb Here are some standard css selectors: .css-truncate-target .message .js-navigation-open time-ago For scrapy, you would need ...

Error encountered in ngtsc(2345) where an argument of type 'Event' is being used instead of an expected parameter type of 'SortEvent'

I recently started using angular and encountered an issue while trying to sort columns. The error message I received was: Argument of type 'Event' is not assignable to parameter of type 'SortEvent'. Type 'Event' is missing t ...

Information is only displayed within the Node Request function and is not accessible to

I am currently developing a small web scraping tool using Node (Express) to search URLs from a list. However, I'm encountering an issue with accessing the results of the search outside of the request callback function in a forEach loop. Can anyone hel ...

create a nested promise with a delay in a node.js environment

Utilizing Google geocoding in my coding tasks has presented a challenge. I have around 50 places to geocode, but Google restricts the number of places that can be geocoded simultaneously, resulting in an 'OVER_QUERY_LIMIT' error. To address this ...

Filter an object in Typescript and retrieve a single key

Managing a set of checkboxes is essential in assigning roles to new users. While it's possible to filter and retrieve only the checked checkboxes, extracting just the "name" key poses a challenge. The current method involves filtering with a for loop ...

I'm having trouble with my carousel. I suspect it's the "link-rel" in the head tag that's causing the issue

Having trouble with my carousel - it's not navigating properly. When I check the console, it shows: Failed to find a valid digest in the 'integrity' attribute for resource '' with computed SHA-256 integrity 'YLGeXaa ...

Is there a way to incorporate CSS or tables when converting HTML to PDF using JavaScript?

While working on a project, I successfully converted an HTML file into a PDF. However, the output did not display the CSS design. Can anyone provide suggestions on how to include CSS design in the PDF file? Below is the JavaScript function code: $(funct ...

Adaptable CSS triangle design

Hello I'm looking to create a responsive CSS triangle similar to the image linked below. I attempted using percentages with border width, but didn't achieve the desired outcome. Can anyone suggest a simple solution? https://i.stack.imgur.com/Yah ...

What happens to an array element when it becomes null within the realm of JavaScript?

As I work on a complex AJAX control class, I've encountered an interesting question. For instance, imagine I store all page elements in an array upon initialization. When a user triggers an AJAX-enabled link, specific parts of the content are replac ...

Ajax Call Fails to Populate Select Field

I am striving to implement a particular feature. When users who want to post ads choose a category, I aim for a JQUERY method .change() to trigger an AJAX request that will fetch subcategories from the SUB-CATEGORIES Table associated with the selected CATE ...

Setting default selections for mat-select component in Angular 6

I've been attempting to preselect multiple options in a mat-select element, but I haven't been successful so far. Below is the snippet of HTML code: <mat-dialog-content [formGroup]="form"> <mat-form-field> <mat-select pla ...

Craft an engaging and dynamic Image map with SVG technology to elevate responsiveness and interactivity

I'm currently working on a website and I need to create two clickable sections on the home page. Each section will lead to a different specialization of the company. To achieve this, I decided to use a square image split into two right-angled triangle ...

Tips for adjusting the size of grid tiles according to the dimensions of the window within a specific range

I am currently exploring how to replicate the effect found on this webpage: When the window size is adjusted, javascript dynamically resizes the grid tiles between 200 and 240px based on the optimal fit for the screen. Is there a ready-made JavaScript/jQ ...

Pass data from a JavaScript function to Objective-C in Xcode by returning an array

How can I retrieve an array of strings from Javascript in my iPhone app code? I am calling a Javascript function, and once it completes, I need to send the array of strings back to the Objective-C code. What is the best way to return the array and how can ...

The disappearing act of the Show/Hide Button in Sencha Touch 2.3.1: what's the

I'm running into an issue with my sencha touch app. Here is the container I have defined: { xtype: 'container', text: 'SOMETHING', height: '15%', width: '15%', ...

I possess a function that can retrieve the key of an Object, but now I am faced with the task of accessing the actual Object using this value in JavaScript

This is my first time seeking advice on a technical issue. I'm currently working with the following function: export function sendRequest<T>(req: RawRequest, options) { const start = Date.now(); const reqOptions: CoreOptions = { ...

Setting a default value in a dynamic dropdown using AngularJS

I could really use some assistance with my issue as I am fairly new to working with AngularJS. My dilemma is related to setting the default value in a select option when there is only one item available due to it being a dynamic select option. Though there ...

What is the reason for the num pad being classified as a character?

Everything is functioning correctly, but when I use the number pad on the right side of my keyboard, it registers as a character and gets deleted. However, the numbers on the left side are accepted without any issue. I want to be able to input numbers usin ...

What is the correct method for closing a style element in Nextjs JSX?

Having trouble adding some unique styling to a jsx component. The code snippet below is throwing an error when compiled. import Card from "./card"; export default function CardRow(){ return( <> <div> <Card> ...