Placing a GIF on top of an image through the use of Javascript

Welcome to my first post on this platform! I'm currently in the process of creating a portfolio website featuring long panoramic photographs displayed within scrollable divs. You can check it out here: www.edentan.co.uk/portfolio.html. My challenge right now is overlaying a GIF onto one of these photos, but positioning it precisely on the image seems tricky. It seems like using JavaScript may be necessary as simply using CSS for positioning restricts me to determining the position based on pixels from the screen edge.

var imageFive = document.getElementById("Img5").style.left = 500%

The code snippet above does work, but the issue lies in how the position varies with different screen resolutions. What I aim to accomplish is having .style.left operate similarly to how I've scrolled left using the script provided below.

var imageThree = document.getElementById('Img3')
var widthImg3 = imageThree.clientWidth;
const Img3El = document.querySelector('#Div3');
Img3El.scrollLeft = widthImg3/1.26; 

In essence, I want to style.left Img5 by a fraction relative to Img3, yet I'm struggling to formulate the script to achieve this goal. Any assistance would be greatly appreciated, and thank you for taking the time to read through my request!

Answer №1

One thing to note is that it's best practice to replace "var" with "let" in your code, as using var can lead to potential issues.

Another suggestion is to avoid using javascript if possible – instead, consider adding styles directly to your HTML file within the header using CSS. This approach tends to be simpler and more efficient. You can achieve the desired effect by applying position:absolute to positioning a gif over an image, utilizing VW or VH units for responsiveness. Here's a snippet of how you could do it:

#image2 {
// add styling for image 2 here
}

#image4 {
     position: absolute;
     left: specify vw value
     top: specify vh value
}

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

The strategic manipulation of numerous elements within a group

I am interested in finding out the safe limits for DOM manipulation using jQuery to avoid freezing the browser. What are the most efficient methods for mass manipulation of the DOM? Typically, I may need to handle lists containing up to 40k li elements. ...

Activate a change or response when the input value in Javascript is altered

Currently, I am working on a small project using JQuery and facing an issue when trying to remove error classes from HTML elements. To handle the removal of the error classes, I am utilizing $('selector').on('input') event to target th ...

Is there a way to remove a value from the search bar while updating the table at the same time?

Although I can successfully search the table based on the values in my search bar, I am having trouble with updating the state when deleting a value. To see my code in action, check out my sandbox here. ...

Can you explain the significance of the ColSpan property in the Material UI TablePagination?

Why is ColSpan used in this code snippet? Reference: https://material-ui.com/components/tables/#table Check for the arrow symbol <TableFooter> <TableRow> <TablePagination rowsPerPageOptions={[5, ...

JavaScript validation is failing, yet the form is still getting submitted

I'm facing an issue with my JS validation. It seems to be working fine, checking for a valid number that isn't zero, but the form is still submitting. I've searched for a solution to this problem, but none seem to work for me. Any suggestion ...

Guide on connecting various information to a jquery element through .data() within a custom plugin

I have come across a problem with my two plugins $.fn.expect = function (expectation) { return this.each(function () { $(this).data('expectation', expectation); }); } $.fn.isExpected = function () { return $(this).dat ...

Pagination in AngularJS that allows users to easily "jump to page"

Looking to enhance my pagination function by adding a textbox and button for users to input the desired page number. Any ideas or reference links on how to go about this? Thanks in advance! Here's my current HTML: <div class="pagingDiv"> <b ...

How can I create a hover effect animation in React?

I am looking to replicate the menu item underline animation demonstrated in this example. In jQuery, I would easily achieve this by obtaining the left position and width of a menu item, then using stop.animation on hover. Attempting this animation with R ...

A guide on accessing header response information in Vue.js

Currently, I am operating on my localhost and have submitted a form to a remote URL. The process unfolds in the following sequence: Submission of a form from localhost Being redirected to a remote URL Sending a response back from the Remote URL to localh ...

What is the best way to execute a function only once after retrieving a Firestore collection?

Here is my query: firestore() .collection('users') .doc(userId) .onSnapshot(doc => setUser(doc.data())) .once(() => { // Do some things just once here! }); The setUser function runs every time the collection's data chang ...

The elusive Ajax seems to be slipping through our grasp,

I am currently working on a website app to display the availability of PCs in my University using JSON data from the University website and AJAX. I am facing an issue where it shows all the MACs for the first room, but returns undefined for others. Since t ...

Create a container with three rows (divs) each taking up 33.333333% of the container's height, designed to be responsive to changes in

Seeking a method to organize 3 divs that will collectively fill the entire height of the container. Sample code: <div class="container"> <div class="item-1"></div> <div class="item-2"></div> <div class="item-3">& ...

While working in Next.js, I utilized an `<Image />` tag with a link to an image, only to encounter an unexpected error

I've attempted it numerous times, but the error persists. I even went ahead and created the next.config.js file. module.exports = { images: { domains: ['link.papareact.com', ], }, }; Error: The src prop (https://links.pap ...

Stop JavaScript Injection Attacks

Let's consider a scenario where a user inputs information into a form that is then submitted to the server using PHP. In the PHP code, we have: $data = $_POST['data']; // or $data = strip_tags(@$_POST['data']); I am curious t ...

Tips for displaying a div briefly before loading the second page

Incorporating a div named ADD, I aim to successfully load a second page within the current one using ajaxload. The challenge lies in displaying a div for 4 seconds before loading the second page. How can this be achieved? Following the wait period, the sec ...

Utilizing JavaScript to enable a Bootstrap 5 dropdown menu to open on hover for desktop users and be clickable for mobile users

I am currently using Bootstrap 5 to design a website and I'm facing an issue with creating a navbar dropdown. On desktop, I want the dropdown to open on hover and redirect the user to a new page when clicked. However, on mobile devices, I only want th ...

What folder layout is best suited for organizing Protractor end-to-end test cases?

Is it best practice to maintain the folder structure for e2e test cases in Protractor identical to that of the application? ...

Guide on adding information to details Table in mvc core with Jquery or utilize Jquery and Ajax for the task

Greetings, I'm an experienced c# developer with a background in old school technologies. Recently, the company I work for has decided to transition to using core technology for development. As a former winforms developer, this shift has left me strugg ...

What is the method to exhibit the outcome of a promise on a web page within a React component?

In my search for information about promises, I have noticed that most articles provide examples using console.log. However, I am faced with a different scenario. I am working with AWS Athena and I need to display the result on a webpage in my React export. ...

Unusual display of fonts on Chrome

Recently, I encountered a strange issue with font rendering on Chrome/Opera compared to Firefox. The problem can be seen in the preview below - pay attention to the pink text. It appears blurry initially but sharpens once text input is focused. This same i ...