Adjust the position of the background when hovering with the mouse to create a right

Currently, I am implementing a background perspective change with the use of mousemove.

However, an issue arises with the right margin of the image after moving it. Everything looks fine initially.

Any suggestions on how to prevent this problem?

https://i.sstatic.net/539Ny.jpg

HTML-

<div id="bg">
    <div class="container1" >
            <div class="row1">
                    <div class="col-md-12" data-aos="fade-left" data-aos-duration="2600" data-aos-once="true"> 
                        <h1 class="title">Keeping track of your health. I`m a:</h1>
                    </div>
                        <div class="col-md-12" align="center" data-aos="fade-right" data-aos-duration="2600" data-aos-once="true" >
                                <a class="btn-link" href="/doctor-register"><button class="btn" type="button" type="submit" value="doctor">DOCTOR</button></a>
                                <a class="btn-link" href="/patient-register"> <button class="btn" type="button" type="submit" value="patient">PATIENT</button></a>
                                <a class="btn-link" href="/donor-register"><button class="btn" type="button" type="submit" value="donor">DONOR</button></a>
                        </div>
            </div>
    </div> <!-- /container -->
</div>

CSS-

#bg { 
height: 100vh;
background-image: url('../../../images/3.png'); /*  Background Image Link */   
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}

Javascript-

$(document).ready(function() {
  $("#bg").mousemove(function(e){
    var x = -(e.pageX + this.offsetLeft) / 65;
    var y = -(e.pageY + this.offsetTop) / 65;
    $('#bg').css("background-position", x + "px " + y + "px");
  });
});

Answer №1

To create an effect, you can expand the image background and remove background-position:center, replace it with background-size:120%;

Check out the snippet below:

$(document).ready(function() {
  $("#bg").mousemove(function(e) {
    var x = -(e.pageX + this.offsetLeft) / 40;
    var y = -(e.pageY + this.offsetTop) / 40;
    $('#bg').css("background-position", x + "px " + y + "px");
  });
});
#bg {
  height: 100vh;
  background-image: url('https://i.pinimg.com/originals/01/66/28/0166280c7713de4cc466aa3ca7052d11.jpg');
  /*  Background Image Link */
  background-size: 120%;
  background-repeat: no-repeat;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div id="bg">
  <div class="container1">
    <div class="row1">
      <div class="col-md-12" data-aos="fade-left" data-aos-duration="2600" data-aos-once="true">
        <h1 class="title">Keeping track of your health. I`m a:</h1>
      </div>
      <div class="col-md-12" align="center" data-aos="fade-right" data-aos-duration="2600" data-aos-once="true">
        <a class="btn-link" href="/doctor-register"><button class="btn" type="button" type="submit" value="doctor">DOCTOR</button></a>
        <a class="btn-link" href="/patient-register"> <button class="btn" type="button" type="submit" value="patient">PATIENT</button></a>
        <a class="btn-link" href="/donor-register"><button class="btn" type="button" type="submit" value="donor">DONOR</button></a>
      </div>
    </div>
  </div>
  <!-- /container -->
</div>

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

Having trouble with Node.js multiparty upload functionality

I'm facing an issue with the functionality of multiparty.Form(). Specifically, I am attempting to print numbers like 2, 3, and 4. Below is the code snippet for uploading images: app.post('/gallery/add',function(req, res,next) { var input = ...

In Chrome, there is a single pixel missing below the <dl> element

While my website's basic list looks great on Firefox and IE, there seems to be one missing pixel line in Chrome. Check out this JsFiddle link shared by Jared in the comments for reference. If you're not seeing the missing line, try adjusting th ...

CSS grid challenges on a massive scale

My CSS grid timeline is currently generating around 1300 divs, causing performance issues. Is there a way to style or interact with the empty nodes without rendering all of them? I also want each cell to be clickable and change color on hover. Any suggest ...

Picture that perfectly matches the height of the window

I am looking to create a website design similar to this where an image fills the window until you scroll down. I am not sure how to accomplish this. Would using jQuery be the best option? I haven't been able to find much information on this. While my ...

Ways to prompt the user to upload a file and integrate it into my program

Hello everyone, I need some help figuring out how to replace a JSON file with another JSON file that a user uploads. Here is my JavaScript code: var app = angular.module('miApp', []); app.controller('mainController', function ($scope ...

JQuery: Issue with Passing Value in a Select Query

I am having trouble passing the selected value from a drop-down list to a select query on another page. Although the drop-down boxes are populated successfully, they do not filter the comments by topic ID as expected. My goal is to pass the chosen value fr ...

jQuery: Read the character string until the occurrence of symbols "{" and "}" (language variation on the website)

Trying to extract text enclosed in curly brackets { } While managing content in three different languages simultaneously. For example: {:en}Resources List{:}{:ru}Список Ресурсов{:}{:uk}Список Ресурсів{:} If the current languag ...

Utilizing setInterval in Vue 3 to efficiently eliminate elements from an array

I am looking to implement a straightforward Snackbar using VUE 3. While I had no trouble doing this in Vue 2, Vue 3 seems to have some nuances. Currently, I have stored the messages array in the Vuex store as shown below: import { createStore } from &apos ...

Error encountered: The use of 'import' and 'export' is limited to 'sourceType: module' when attempting to install Tweakpane

I am currently learning JavaScript and have been following a course on Domestika that requires me to install the Tweakpane package. I usually use Git Bash for installing packages, and I have successfully installed others this way before. Here is the proces ...

Creating a new ES-6 class to extend express-js: Issues with binding getter and setter properties

I am intrigued by the idea of utilizing Express within an extended class. My goal is to create getter and setter methods for a property, but I'm facing the issue of these methods not being bound to the instances as desired. One way to work around this ...

Sending Input to SQL DataBase Using C# and HTML

I need to execute a C# function when a button is clicked that captures a rating (1-7) and saves it to a database along with a text input. Here is the feedback section: <asp:PlaceHolder ID="AnswersPlaceholder" runat="server" Visible="false"> <asp ...

Converting an HTML page into a base 64 string: Step-by-step guide

Currently, I am utilizing Angular 6 for my project. On the index page, I am binding data and then submitting it to the database. My objective is to submit the entire HTML page with the bound data as a string in the database. Is there a way to convert an H ...

Issue encountered while attempting to save a value in localStorage

I am encountering an issue while trying to save and read the value of a button in the panel. The error message I receive is - Unable to set property 'adl' of undefined or null reference. This is my first time working with localStorage, so I' ...

"Alert box displaying error message is not appearing on the screen

In order to display an error message using a tooltip, I have hidden the tooltip by setting the style of a span with an id of demo to display:none. Then I use JavaScript's getElementById method to retrieve the value of the input field with an id of use ...

"Experience the Dynamic Animation of Bootstrap4 with jQuery in a

I had implemented a code for horizontal page left to right animation which was working perfectly fine. However, it suddenly stopped functioning without any recent changes being made. Now, the Page--3 link is no longer functional and clicking on either the ...

Adding Floating Point Numbers in TypeScript Objects

Recently, I've been encountering some strange results while trying to sum up my floating point values. The output seems off with 8 decimal places and other unexpected outcomes. All I really want to do is add up the protein values of various objects to ...

What is the method for incorporating jQuery UI effects into buttons that are imported from a basic HTML document?

I am continuously updating the content on my website in three distinct ways: 0) Utilizing jQuery's getJSON('bla.json') to read JSON files; 1) Fetching JSONized C#/Razor classes via jQuery's getJSON('bla.cshtml'); 2) Loading H ...

Including a Textbox completely disrupts the Menu design

I have been working on a project involving ASP and C#. Within the project, I am utilizing a masterpage for the navigation bar located at the top of my pages. It appears as follows: https://i.sstatic.net/Qb1P8.png The navigation bar includes standard nav ...

The JavaScript function String.split() is generating an array filled with 20 empty strings

Attempting to replicate the functionality of jQuery's string-based HTML element determination, I employed a split function. However, rather than returning a list of values as intended, it produced an array containing twenty empty strings. console.l ...

What is the best method for eliminating the border of an Angular mat-form-field when it is in a disabled state?

I've been attempting to eliminate borders from a mat-form-field when the field is disabled, but despite trying various solutions found online, I haven't been successful in removing the borders from the div. Below is the code snippet: <div ...