Combining jQuery within a single container/segment/div tag

Currently, I am experimenting with adding jQuery to my Cargo Collective website:

I have been working on a mouse-tracking jQuery effect that I really like, but I am facing an issue where the script does not stay contained in a specific section of the site. As you scroll down the page, the image continues to follow the mouse throughout the entire journey.

Here is the current code snippet:

My goal is to limit the effect to only the top VH/VW area, and disable it when the user scrolls past a certain point within the container. I have tried wrapping it in a div tag and adjusting the height, but so far without success…

Any assistance would be greatly appreciated as always.

Thank you and stay safe.

$(document).on("mousemove", function(event) {
  $("img.lisle").css("width", event.pageX)
  $("img.lisle").css("height", event.pageY)

  $("img.stroke").css("width", $(window).width() - event.pageX)
  $("img.stroke").css("height", event.pageY)
})
img {
  position: absolute;
  width: 100%;
  height: 100v%;
}

img. lisle {
  top: 0 !important;
  left: 0 !important;
}

img.stroke {
  top: 0 !important;
  right: 0 !important;
}
<img src="https://freight.cargo.site/t/original/i/52942d135a28c76f77ec1127ef9a8d14d2df813d5f16575bdd381101d8097937/LisleA_Stretch.png" class="lisle" style="width: 1432px; height: 846px;">

<img src="
    https://freight.cargo.site/t/original/i/0292f819549959a5b1390d6d3c07c7853f8e95782e81450d62250dfd0b912a3e/test-stroke.png" class="stroke" style="width: 88px; height: 846px;">

Answer №1

Wow, that really did a number on my monitor! Let's try a slightly different approach this time. Don't forget to include a unique identifier for the element.

$('.page_container').on("mousemove", function(event) {
  $("img.lisle").css("width", event.pageX)
  $("img.lisle").css("height", event.pageY)

  $("img.stroke").css("width", $(window).width() - event.pageX)
  $("img.stroke").css("height", event.pageY)
})

Answer №2

Within the HTML document:

<div id="mouseActions">....</div>

Next, in the JavaScript file:

$("#mouseActions").on("mousemove", function(event) {....}

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

Tips on updating primeng Panel Menu appearance with scss

I'm looking to customize the color of my panel menu to black. Below is the HTML code I am using. <p-panelMenu styleClass="font-bold text-xs m-0 w-11" [model]="items" [multiple]='false'></p-panelMenu> ...

Instructions on creating a number increment animation resembling Twitter's post engagement counter

I've been attempting to replicate the animation seen on Twitter's post like counter (the flipping numbers effect that happens when you like a post). Despite my best efforts, I can't seem to make it work. Here is what I have tried: $(fun ...

Is it possible to have a hidden div box within a WordPress post that is only visible to the author of the

How can I create a div box with "id=secret" inside a post that is only visible to the author of the post? I initially made it for the admin, but now I want the id to be visible exclusively to the post's author. For instance: If the author is curren ...

Unlock the secret: Using Javascript and Protractor to uncover the elusive "hidden" style attribute

My website has a search feature that displays a warning message when invalid data, such as special characters, is used in the search. Upon loading the page, the CSS initially loads like this: <div class="searchError" id="isearchError" style="display: ...

Having trouble vertically aligning text? Combining AngularJS, Bootstrap, and Flexbox can help!

I have spent a lot of time trying to understand why I am unable to vertically align the text inside these cells. I have tried multiple solutions: align-items: center; vertical-align: middle; justify-content: center; justify-items: center; Unfortunately, ...

Issue arises when CSS selectors do not function properly with absolutely positioned divs

I am experiencing an issue with my CSS code that is functional on CodePen but not on my actual website. I am attempting to target the .appraisals class for manipulating the background color of an opaque screen slider containing information. Despite using a ...

Close the space between the image and the Container

Looking at the image, you can view the demo http://jsfiddle.net/yJUH4/8/ Upon observing the picture, a slight gap appears between the image and the Container. The image already has the css property vertical-align:middle. When I increase the height of the ...

Bone Structure Styles - adaptable form

I'm currently working on a form using skeleton css and initially set it up with a three-column layout. However, I found that this created too much white space due to varying content lengths in each column. I would like to switch to a horizontal layout ...

I'm struggling to figure out why the CSS isn't working properly

i found this markup like shown here i am curious why it appears that lines 12 & 13 .notes:link span, .notes:visited span { ... seems to be functioning .comments span, background-position: -16px -16px; } .permalink:link span, .permalink:visited sp ...

Moving a blank block to the top using CSS transformation

After trying to implement the translate3d(150px,0px,0px); on my "content" div, I encountered an issue where an empty block appears at the top of the page. Unfortunately, I am unsure about how to resolve this issue. <!DOCTYPE html> <html> <h ...

Using AJAX and Spring MVC to render JSON objects in bracket notation within a POST request

Why is a JSON rendered in bracket notation when bound to a Web Request? I am working on an application that involves making 2 consecutive REST Controller calls. The first call reads an address from a form, serializes it, sends it via AJAX to a validation ...

Create a cylindrical HTML5 canvas and place a camera view inside the cylinder

I have a unique project in mind that involves creating an HTML5 canvas cylinder with the camera view at its center. The walls of the cylinder will display images, and the entire structure should be able to rotate and zoom. If anyone has any advice on how ...

Keep the first column of an HTML table fixed as you scroll horizontally

Below is an HTML table that I have created: <table id="customers" class="table table-bordered" width="100%"> <thead> <tr> <th colspan="2">Activities</th> <th>Mon 17</th> <th>Tue 18</th> </thead> ...

Updating a Pandas Column in Python Using a for Loop

My lack of experience with web scraping is definitely showing as I am relatively new to this. The goal of my code is to scrape all the data from a webpage, which it does successfully. However, before the loop continues, I want pandas to write the current p ...

When the icon is clicked using `ngClick`, we are triggering the `refresh` event, but unfortunately, it is not functioning as expected

Currently, I am utilizing Angular and jQuery float to create a canvas graph. The graph itself is composed of canvas elements. The issue at hand involves the desire to redraw the canvas upon clicking on a specific icon. The click event handler in question ...

Is it necessary to reset the toggle, or should it be reset upon clicking another element?

I am in the process of setting up a click-to-show menu with two separate options for men and women. When users click on the "men" menu, it should display one set of options, while clicking on the "women" menu should display another. If one menu is clicke ...

Why isn't JQuery's .prependTo('body') function functioning correctly?

It's getting late and I should probably call it a night, but I am completely puzzled. I hate to admit it, but why isn't this working?? var informationBox = ''; informationBox += '<div class="informationBox">'; informati ...

What is the best way to interpret a line break within a string variable in TypeScript?

Realtime Data base contains data with \n to indicate a new paragraph. However, when this data is retrieved and stored in a String variable, the website fails to interpret the \n as a paragraph break: https://i.stack.imgur.com/tKcjf.png This is ...

Conceal the div upon clicking anywhere on the page, except if a particular div is clicked

Whenever the user interacts with topic_tag_sug or any of its child elements, the div should remain visible. However, if the user clicks on any other element, topic_tag_sug should be hidden. HTML <input id='topic_tag' onblur="$('#topic_t ...

Automatically updating quantity with the power of jQuery

I have created a spreadsheet where users can input their expenses and the total will update automatically. Initially, I have set some default numbers in my HTML which are editable for users to modify as needed. However, I am facing an issue with my JQuer ...