Switch up the keyframe animation for a unique custom cursor style when hovering

I successfully developed a custom cursor using a div with jQuery. Is it possible to change the cursor style, such as applying animation keyframes, when hovering over the div?

Below is the HTML code I used to create the custom cursor.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>        <style type="text/css" media="screen">
        .verline, .horline {
            border-radius: 20%;
            background-color: #121212;
            position :absolute;
            animation-duration: 400ms;
            animation-timing-function: ease-out;
            animation-iteration-count: 1;
            animation-direction: normal;
            animation-fill-mode: forwards;
        }
        .horline {
            width: 50px;
            height: 3px;
            top: -3px;
            left: -25px;
            animation-name: hormove;
        }
        .verline {
            width: 3px;
            height: 50px;
            top: -27px;
            left: -2px;
            animation-name: vermove;
        }
        @keyframes hormove {
            0% { transform: rotate(0deg); top: -3px; left: -25px; }
            100% { transform: rotate(220deg); top: -19px; left: -25px; }
        }
        @keyframes vermove {
            0% { transform: rotate(0deg); top: -27px; left: -2px; }
            100% { transform: rotate(50deg); top: -12px; left: -2px; }
        }
        #hoverit {
            width: 200px;
            height: 200px;
            background: red;
        }
    </style>
</head>
<body>
    <div id="cursor">
        <div class="horline"></div>
        <div class="verline"></div>
    </div>
    <div id="hoverit"></div>
</body>
<script>
    var ElementCursor = {
        cursorElement: "",
        setCursor: function (cursorId) {
            $('html').css({
                'cursor': 'none'
            });
            $('html').mousedown(function (e) {return false;});
            ElementCursor.cursorElement = cursorId;
            ElementCursor.updateCursor();
        },
        removeCursor: function () {
            $('html').css({
                'cursor': ''
            });
            ElementCursor.cursorElement = '';
        },
        updateCursor: function () {
            $(document).mousemove(function (e) {
                $('#' + ElementCursor.cursorElement).css({
                    'position': 'fixed',
                        'top': e.pageY + 'px',
                        'left': e.pageX + 'px'
                });
            });
        }
    };
    ElementCursor.setCursor("cursor");
</script>
</html>

Answer №1

Definitely! All you need to do is calculate the offset and size of the div to create a hover effect based on the cursor's position. This will determine whether the cursor is on the left or right side of the center of the div and apply the appropriate class.

    $(document).mousemove(function (e) {
        ElementCursor.cursorElement.css({
            'position': 'fixed',
            'top': e.pageY + 'px',
            'left': e.pageX + 'px'
        });
        var width = ElementCursor.hoverElement.outerWidth();
        var left = ElementCursor.hoverElement.offset().left;
        if (e.pageX > left + (width / 2)) {
          ElementCursor.cursorElement.addClass('right');
        } else {
          ElementCursor.cursorElement.removeClass('right');
        }
    });
    ElementCursor.hoverElement.mouseenter(function(e) {
      ElementCursor.cursorElement.addClass('in');
    });
    ElementCursor.hoverElement.mouseleave(function(e) {
      ElementCursor.cursorElement.removeClass('in');
    });

Check out the live demo here: http://plnkr.co/edit/oNks2ApCleP6TqZFNOSR?p=preview

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

Troubleshooting regex validation issues in a JSFiddle form

Link to JSFiddle I encountered an issue with JSFiddle and I am having trouble figuring out the root cause. My aim is to validate an input using a regex pattern for a person's name. $("document").ready(function() { function validateForm() { var ...

The data in my AngularJS model will only refresh when the button is clicked twice

I am encountering an issue with a select list box and a button in my code. The aim is to filter the model displayed in the select list box based on the selectId received from clicking the button. The problem arises when the model updates only after clicki ...

Determining the visibility of an element on a webpage using JavaScript and Puppeteer

I've created a framework that scans websites hosted on our servers to ensure they comply with our policies. If any prohibited content is found, we capture a screenshot along with other relevant details. However, taking a screenshot may not be possibl ...

Having trouble transferring data from JavaScript to PHP via POST method

Hi there, this is my first time posting and I could really use some assistance. I have a bit of a roadblock with two Symfony forms on a page that don't seem to be working properly. The first form is for a contract, while the second one is used to pop ...

Is it possible to utilize the same module.css file across multiple components in a React project?

As a newcomer to React, I have discovered the benefits of using module CSS to address naming conflicts. Now, as I prepare to refactor my app, I have encountered a scenario where two components are utilizing styles from the same file. I am curious to lear ...

Utilize the existing Google Maps API instance within a single-page application

Imagine I have a Single Page Application (Angular JS application), and I create a Google Map instance on an element with the id googleMap - var mapInstance = new google.maps.Map(document.getElementById(`googleMap`), mapOption) Later on, as I navigate th ...

Is it possible to utilize v-html with message data in Vue without any limitations on the <title> element?

Currently utilizing Vue.js 2 and my SDK is IntelliJ: I am attempting to bring HTML content into a Vue.js file. The main objective is to include the <title></title> attribute, as it seems that Vue doesn't have direct support for this feat ...

Inkwell shifts bespoke quality

I am currently utilizing Quill (v.2.0.0) with quill-better-table (v.1.2.10) within my Angular 13 project. I am attempting to incorporate a custom attribute to the table tag, as shown below: <table class="quill-better-table" custom-attribute=& ...

Unlocking the secrets to obtaining a socket.io client

I encountered an error when trying to set up a socket.io server and client. The error I received on the client side was: Failed to load resource:http://localhost:3000/socket.io/?EIO=4&transport=polling&t=OK-egu3 the server responded with a status o ...

Combining several objects into a one-dimensional array

I am encountering a small issue with correctly passing the data. My form is coming in the format {comment:'this is my comment'} and the id is coming as a number. I need to send this data to the backend. let arr = []; let obj = {}; o ...

Guide on adding an item to the end of an array with an arrow function

const appendElement = (item) => { return (list) => { }; }; Add a new item to the end of the list. Parameters item (any): The item to add. Output (Array) => Array: Generates a closure that duplicates the original list with the added ite ...

Vue not displaying local images

I'm having trouble creating an image gallery with Vue.js because local images are not loading. I have the src attributes set in the data attribute and can only load images from external sources. For example: data() { return { imag ...

Substituting placeholders within a docx template remains incomplete

Utilizing this specific library, I am implementing a feature to substitute placeholders within a docx template and generate multiple documents. The technologies involved are neutralino and vue for the front-end, where I have devised a method that transmits ...

Combine an array of objects using the main key in each object

I have an array of objects with different years and details var worksSummaryDetailsArr = [ { year: 2020, worksSummaryDetailsObj: [ [Object], [Object], [Object], [Object] ] }, { year: 2021, worksSummaryDetailsObj: [ [Object], [Object], ...

Different results are being obtained when destructuring props in a component

Just diving into the world of React and trying to grasp destructuring. I've been doing some reading on it, but I'm currently stuck. When I try to destructure like this function MList({action}) { // const data = [action];}, I only get 'camera ...

In what way can an array be assigned to a new key within the same key along with additional objects?

My goal is to transform the existing key value into a new format within the same key. It may be difficult for me to explain clearly through words, but the following data will help clarify. The data is currently structured as follows: const sampelData = [{ ...

Encountering an error while trying to import GraphQL resolvers and schema

Upon attempting to start the server with the following setup, an error is encountered: Error: "createUser" defined in resolvers, but has an invalid value "function (userInput) {... The resolver's value must be of type object. index.ts const schema ...

What is the best way to assign an identifier to a variable in this scenario?

script.js $('a').click(function(){ var page = $(this).attr('href'); $("#content").load(page); return false; }); main.html <nav> <a href="home.html">Home</a> <a href="about.html">About</a> < ...

Is it possible for AngularJS components to alter their enclosing element?

I see Angular components as element directives. Take a component named hero, for example, I can include it in the parent template like so: <hero myparam="something"></hero> What I envision is using the hero element as a container managed by t ...

Tips for showcasing an image prior to uploading within a personalized design input file

I am facing an issue with displaying multiple images inside custom style input file labels before uploading them to the database. Currently, the script I am using only displays one image at a time and in random positions. My goal is to have each image ap ...