Issue with animated cursor function not activating when used with anchor links

I've spent hours searching for a solution but I can't seem to find one. I'm attempting to modify the codepen found at https://codepen.io/Nharox/pen/akgEQm to incorporate images and links, however, two issues are arising. The first issue is that the cursor's vertical position doesn't align correctly after scrolling with either the browser or mouse wheel. The second problem is that clicking on the link has no effect. I'm baffled as to why this is happening.

<body>
<div class="cursor hidden">
    <div class="cross">
        <div class="b b1"></div>
        <div class="b b2"></div>
    </div>
    <svg class="circle" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid" width="52" height="52" viewBox="0 0 52 52">
        <path d="M1,26a25,25 0 1,0 50,0a25,25 0 1,0 -50,0"/>
    </svg>
</div>

Links

<div class="myrow">
    <div class="myrow__box">
        <a href="/link-to-page"><img alt="" src="/images/myimage.jpg" /></a>
    </div>
</div>

SCSS

.cursor {
position: absolute;
z-index: 5;
width: 50px;
height: 50px;
opacity: 0;
transform: translate3d(-50%, -50%, 0) scale(.9) rotate(135deg);
transition: opacity 0.5s, transform 0.5s;
pointer-events: none;
&:before, &:after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 3px;
    height: 3px;
    background-color: white;
    transition: width 0.5s;
}
&:before {
    transform: translate3d(-50%, -50%, 0);
}
&:after {
    transform: translate3d(-50%, -50%, 0) rotate(90deg);
    transform-origin: center;
}
svg {
    fill: transparent;
    stroke: white;
    stroke-width: 3;
    stroke-dasharray: 160;
    stroke-dashoffset: 160;
    overflow: visible;
    transition: stroke-dashoffset 0.5s;
}
&-is-visible {
    opacity: 1;
    transform: translate3d(-50%, -50%, 0) scale(1) rotate(0deg);
    &:before, &:after {
        width: 22px;
    }
    svg {
        stroke-dashoffset: 0;
    }
}
}
.myrow {
display: block;
max-width: 100%;
margin: 0 auto;
&__box {
    cursor: none; 
    position: relative;
    height: auto;
    transform: scale(1);
    &:active {
        &:before {
            background-color: rgba(black, 0.15);
        }
    }
    &:before {
        content: '';
        position: absolute;
        z-index: 1;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background-color: rgba(black, 0);
        transition: background-color 0.3s;
    }
}
}

JS

(function showCursor() {
'use strict';
// Variables
var boxes = document.querySelectorAll('.myrow__box'),
    cursor = document.querySelector('.cursor'),
    boxPos = [];
// Get coordinates for the current cursor position
function getPos(e, el) {
    var xPos = 0,
        yPos = 0;
    xPos = (el.offsetLeft - el.scrollLeft + el.clientLeft);
    yPos = (el.offsetTop - el.scrollTop + el.clientTop);
    var mouseX = e.clientX - xPos,
        mouseY = e.clientY - yPos; 
    cursor.style.top = '' + mouseY + 'px';
    cursor.style.left = '' + mouseX + 'px';
}
// Add event listeners and call fns for the corresponding box
for (var i = 0; i < boxes.length; i++) {
    boxes[i].addEventListener('mousemove', function(event) {
        var currentBox = this;
        boxPos = getPos(event, currentBox);
    }, false);
    boxes[i].addEventListener('mouseenter', function() {
        this.appendChild(cursor);
         setTimeout(function() {
             cursor.classList.add('cursor-is-visible')
         }, 10);
    }, false);
    boxes[i].addEventListener('mouseleave', function() {
        cursor.classList.remove('cursor-is-visible');
    }, false);
}
})();

The images need to adjust in size to be 100% of the browser window width regardless of its dimensions. The cursor itself is functioning properly and animating as expected per the codepen example. However, the link remains unclickable and the y-position is not correct.

Answer №1

Primarily, the correct Y position is maintained as there was no event listener set to update it according to cursor movements. Currently, only mouse enter, move, and leave events are being tracked. To adjust for changes in cursor position due to scrolling (which isn't a cursor event), a scroll event must be registered to update the Y position value accordingly.

Moreover, the anchor element was not functioning properly as it was lost within the content. To rectify this issue, wrap your "box" element inside an anchor tag.

See below for the updated HTML:

<a href="www.google.com" target="_blank">
    <div class="myrow">
        <div class="myrow__box">
            <img alt="" src="https://www.easyjet.com/en/holidays/shared/images/guides/austria/vienna.jpg" />
        </div>
    </div>
</a>

And here is the modified JS code:

(function showCursor() {
'use strict';
// Variables
var boxes = document.querySelectorAll('.myrow__box'),
    cursor = document.querySelector('.cursor'),
    boxPos = [],
    scrollY = 0;
// Function to get cursor coordinates
function getPos(e, el) {
    var xPos = 0,
        yPos = 0;
        xPos = (el.offsetLeft - el.scrollLeft + el.clientLeft);
        yPos = (el.offsetTop - el.scrollTop + el.clientTop);
    var mouseX = e.clientX - xPos,
        mouseY = e.clientY - yPos + scrollY; 
    cursor.style.top = '' + mouseY + 'px';
    cursor.style.left = '' + mouseX + 'px';
}
// Adding event listeners and functions for each box
for (var i = 0; i < boxes.length; i++) {
boxes[i].addEventListener('mousemove', function(event) {
    var currentBox = this;
    boxPos = getPos(event, currentBox);
}, false);
boxes[i].addEventListener('mouseenter', function() {
    this.appendChild(cursor);
     setTimeout(function() {
         cursor.classList.add('cursor-is-visible')
     }, 10);
}, false);
boxes[i].addEventListener('mouseleave', function() {
    cursor.classList.remove('cursor-is-visible');
}, false);
}
window.addEventListener('scroll', function (event) {
    if(event.target.scrollingElement.localName == "body") {
        scrollY = event.target.scrollingElement.scrollTop;          
    }
})     
})();

Feel free to check out the codepen link I created to view the modifications that have been implemented.

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

Arrange DIV elements sequentially with HTML, JQuery, and CSS

Live Demo: Live Demo HTML: <div class="target"> <img src="bg-clock.png" alt="jQuery" /> </div> <div class="target2"> <img src="bg-clock.png" alt="jQuery" /> </div> CSS: .target, .target2 { ...

Tips for repairing buttons in the CSS container

The buttons in the CSS search-box are not staying fixed as intended. They should be within the search box, but instead, they are protruding out of the panel. I attempted to utilize z-index, but it did not produce the desired outcome. https://i.sstatic.n ...

What is the process for implementing pagination in vue-tables-2 with a Laravel REST API?

I'm looking to implement pagination on Vue server-table using a Laravel endpoint. How can I achieve this? Below is my component setup: <template> <div> <v-server-table :columns="columns" url="/object/find" :options="option ...

Utilize external cascading style sheets (CSS) to style XML data loaded into Flash

Trying to incorporate formatted xml into a flash game has been quite challenging for me. Despite my efforts, the css styling doesn't seem to be applied properly. Below is the content of my stylesheet woorden.css: .f {color:red;} This is the str ...

The 'file' property of undefined throws an error in ng-file-upload

I am currently exploring the functionality of ng-file-upload from this repository: https://github.com/danialfarid/ng-file-upload I have successfully implemented the basic setup as follows: HTML: <section ng-controller="MyController"> ...

Modifying the CSS class of an element does not produce the desired effect after altering its styles using JavaScript

html: <input id="myinput" class="cinput" type="image" src="http://www.foodwater.org.au/images/triple-spiral-3-small-button.jpg"/> <br><br><br><br> <button id="s">set to visible class</button> <button id="h"> ...

Ways to determine if a date matches today's date within a component template

I am currently displaying a list of news articles on the webpage and I want to show the word "Today" if the news article's date is equal to today's date. Otherwise, I want to display the full date on the page. Is there a way to compare the news.D ...

What are some ways we can enhance Map.get for react-router using ES6 Maps?

I recently implemented a map using new Map() to store my RR4 configuration within my application. My goal is to retrieve the values associated with /countries/:id when accessing /countries/1. routesMap.get('/countries/1') // should provide the ...

Looking for assistance with PHP if statement and troubleshooting iFrame issues

Using the PHP if statement below on my website: <?php if(empty($_GET['tid'])) echo '<iframe src="http://tracking.mydomain.com/aff_goal?a=33&goal_id=47" scrolling="no" frameborder="0" width="1" height="1"></iframe>'; ...

Utilizing jQuery to Sort Table Rows based on an Array of Class Names

I have a table containing values and a filter option where users can select multiple values to filter the table. I want to create a filter with numbers ranging from 1 to 10, and assign class names like filter_1, filter_2, filter_3, etc. to each table row ( ...

The files being requested by jQuery Slimbox are not being retrieved properly

I am currently utilizing jQuery slimbox along with its Application Programming Interface (API). Below is the JavaScript code snippet that retrieves image paths through JSON and then triggers the slimbox using its API. $('#main-container').appen ...

After the build process, Nextjs Sitemap is eliminating the /en/ from all newly generated web links

Utilizing Strapi to pull data in JSON format. For instance, a typical website link appears as follows: https:/ /www.some-site.com/some-link What happens to the links once the post build is completed on my Nextjs project: <url><loc>https://web ...

What is the easiest and most straightforward method for deploying HTML and JavaScript in Service Mix?

I am currently working on a small HTML/web page project and I am interested in deploying service mix. Although I have knowledge of the web page side and I am familiar with service mix, I haven't ventured into using a simple HTML/JavaScript setup withi ...

Using jQuery, JavaScript, and PHP, we can easily implement the functionality to disable the form and submit button

After submitting a form, I am trying to disable both the submit button and the form itself. However, every solution I have attempted either disables the form and button without submitting it or submits the form without disabling the button/form. This is t ...

Updating the logo image on mobile devices using responsive CSS styling

My goal is to show a different image to users on mobile devices using only CSS, as I am unable to access the HTML code. On mobile, I used display:none for the header-logo-image img { and then added a background-url to the div to successfully display my alt ...

Conflicting styles arise when using the makeStyles function from Material UI with imported

In working on a React component library using create-react-library, I have incorporated basic components that utilize Material UI components and the material UI hook-based styles pattern. For example (in a simplified form): // LibraryComponent.js const u ...

There is no 'Access-Control-Allow-Origin' header found on the requested resource in Heroku for Node.js

Here is the header setup in my Node.js API app: res.header("Access-Control-Allow-Origin", "*"); res.header( "Access-Control-Allow-Headers", "Origin, X-Requested, Content-Type, Accept Authorization" ); ...

Issue with Chrome not triggering onMouseEnter event when an element blocking the cursor disappears in React

Important Note: This issue seems to be specific to Chrome Currently, React does not trigger the onMouseEnter event when a blocking element disappears. This behavior is different from standard JavaScript events and even delegated events. Below is a simpli ...

Encountering a react error following the installation of FontAwesome via npm

I successfully installed fontawesome by visiting their official website and following the steps below: 1. npm i --save @fortawesome/fontawesome-svg-core 2. npm install --save @fortawesome/free-solid-svg-icons 3. npm install --save @fortawesome/react-fon ...

Using Phonegap alongside ons-scroller and ons-button

Recently, I have been using Phonegap with the Onsen UI system on iOS devices. I encountered an issue where buttons included within an ons-scroller were not clickable when running on an iPad or iPhone. Here is the code snippet that caused the problem: < ...