Manipulate a 3D shape by transforming both the x and y axes using jQuery's mousemove

I am trying to create a 3D element that rotates on both the x and y axes based on the position of the mouse on the screen. While I have successfully achieved this effect using either the X or Y position, I am struggling to make it work simultaneously for both positions.

Here is my current code snippet:

var $document = $(document);

$document.mousemove(function(e) {
    var height = $document.height() / 60;
    var pageY = e.pageY / height;
    var valueY = 180 - pageY;

    $(".cube").css("transform", "rotateZ(" + valueY + "deg)");
});

$document.mousemove(function(e) {
    var width = $document.width() / 40;
    var pageX = e.pageX / width;
    var valueX = 125 - pageX;

    $(".cube").css("transform", "rotateY(" + valueX + "deg)");
});

You can view a working example on CodePen: https://codepen.io/asmundsol/pen/PRXZRa

Answer №1

let $doc = $(document);

$doc.mousemove(function(event) {
    let heightVal = $doc.height() / 60;
    let pageYVal = event.pageY / heightVal;
    let valueYVar = 180 - pageYVal;

    let widthVal = $doc.width() / 40;
    let pageXVal = event.pageX / widthVal;
    let valueXVar = 125 - pageXVal;

    $(".cube").css("transform", "rotateZ(" + valueYVar + "deg) rotateY(" + valueXVar + "deg)");
});

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

Tabulator - Enhancing Filter Results Using a Second Tabulator

I've implemented a Tabulator that displays search results, here is the code: var table = new Tabulator("#json-table", { layout:"fitDataFill", //initialFilter:[{field:"title", type:"!=", value:"ignoreList.title"}], ajaxURL:tabUrl, ajax ...

Can you display a popup on a leaflet map from beyond its boundaries?

Utilizing leaflet maps, I have implemented a functionality where clicking on the map opens a popup at a specified location and centers on that position. The code for this is as follows: var popup = L.popup(); function onMapClick(e) { popup . ...

Changes in bottom position when scrolling in landscape mode on iOS Safari

I'm currently using ReactJS and have implemented a mobile menu bar with CSS properties position: fixed and bottom: 0. The menu bar stays locked at the bottom of the page in both portrait and landscape orientations on all browsers except for Safari. I ...

"Unexpected issue with jQuery AJAX Post functionality on a shared server causing data not to

I'm currently facing an issue with my code that allows comments to be added to a php page without needing to refresh the page. While everything is functioning properly on localhost, I encountered a problem when testing it on my justhost account. The ...

"Eliminate any inline styling from a string element with the power of JavaScript/j

If I have the following string: let sentence = '<h1 style="lots of class"> </h1><h2> <p style="bunch of class"> </p> <p style="bunch of class"> </p></h2>'; This string contains multiple elements a ...

Override CSS properties / prevent inheritance

I'm currently facing a challenge in Wordpress where I need to insert a link quickly, but there are intricate styles applied to all the anchor links within a specific section. Here is a snippet of the CSS selector and styles being used: div.content-ro ...

Is there a way to conceal the loading screen until the website has completely loaded?

I've been working on my personal portfolio/website and encountered a bug that I can't seem to fix on my own. The issue is with the logo (aa) and text under it showing even after the content page has fully loaded, taking around 3 seconds to hide. ...

Simultaneously sending jQuery.ajax data while submitting a form

I'm facing a bit of a dilemma here. I have a form with multiple fields, including one for entering links. When you input a link and click the add button, the link is added to a link_array using jQuery. My goal is to send this array through the jQuery. ...

Styling does not affect an Angular component that is injected into an iframe

I am facing an issue with styling in an iframe when trying to append my own angular component. Despite various attempts, I have been unsuccessful in getting the styling to apply to the component within the iframe. Upon loading the iframe, I use JavaScript ...

Troubleshooting a problem with jQuery waypoints in a static HTML/JS

Utilizing waypoints and windows to display the panels similar to the fiddle example I have created: http://jsfiddle.net/6bMMa/1/ Everything is functioning correctly, however, I have only managed to make it work by using id numbers on the panel divs. The ...

Comparing inline-block and block display options for displaying images

In this HTML snippet, there is a main div with the id of "main_div" that contains two sub-divs. The first sub-div has an id of "logo" and includes a link to a picture. The second sub-div has an id of "intro" and contains text. For reference, here is the o ...

Two divs with floated attributes and equal heights

My unique HTML is below: <div class="container"> <div class="box"> <div class="float"> <img src='http://media-cdn.tripadvisor.com/media/photo-s/03/9b/2f/db/miami-beach.jpg' /> </div> ...

Adjust text to fit within a specified height container automatically

Is there a way to make text resize automatically based on the height and width of its container? I've looked at similar questions but none seem to provide the right solution for me. I tried using fitText, but it doesn't take the container's ...

Learn how to implement media queries using CSS3 with an HTML5 form

Struggling to implement Media Query in my HTML5 form that was styled using CSS. Any assistance would be greatly appreciated. <div class="form"> <form action="Login.jsp" method="post" > <input type="text" name="username" placeholder="usernam ...

Issue with Flex property not being supported in Safari browser

.rq-search-container { position: relative; width: 100%; background: white; display: flex; flex-direction: row; align-items: flex-start; border-radius: 2px; z-index: 30; -webkit-flex: 1 0 15em; } While this code functi ...

The controller method in Laravel is not being triggered by AJAX

In my blade layout file, I have some code that is included in my view multiple times using the @include.layout("blade file link") syntax. When a button is pressed, I want to call a controller function. This works fine without AJAX, but when AJAX is added, ...

Creating colorful containers using Bootstrap

Hey there, I am interested in creating small colored boxes to represent different meanings for each color. I came across this code snippet but I am unsure how to adjust the size: <div class="p-3 mb-2 bg-primary text-white">.bg-primary</d ...

Guide to creating a toggle button

Can someone help me create a button that toggles between displaying block and display none when clicked? I've been trying to use Classlist.toggle with JavaScript, but I'm not sure if I have the correct classes targeted. let showCart = documen ...

Instructions for selecting all checkboxes in an HTML table with just one click

Developing an aspx page with 3 HTML tables, I am dynamically adding checkboxes to each cell. Additionally, I have a checkbox outside each table. When I check this checkbox, I want all checkboxes in the corresponding HTML table to be checked. However, curre ...

Can jQuery Autocomplete function without stopping at white spaces?

Is there a way to modify jQuery UI autocomplete so that it can handle multiple words instead of terminating with a space? For example, if you input "New York City", it should still filter results based on each word. $.ajax({ type: "GET" ...