What is the best way to align an image with the position of a div?

Looking for assistance on positioning an image to a div's position after it has been cloned.

$(".raindrop1").clone().removeClass("raindrop1").appendTo("body");
$("img").css({"left": "div".x, "top": "div".y});
.shape{
border-radius: 50px;
width: 10px;
height: 10px;
background-color: white;
position: absolute;
left: 50%;
top: 50%;
}
<div class = "shape" onclick = "curse()"></div>
<img src = 'http://images.clipartpanda.com/raindrop-clipart-RTGdn5bTL.png' width = "15px" class = "raindrop1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>

Seeking guidance and solutions from the community. Any help is appreciated!

Answer №1

Example 1

If you wish to move the duplicated raindrop to the same position as the div, simply use .appendTo(".shape"), which will add the image inside the div.

$(".raindrop1").clone().removeClass("raindrop1").appendTo(".shape");
$("img").css({
  "left": "div".x,
  "top": "div".y
});
.shape {
  border-radius: 50px;
  width: 10px;
  height: 10px;
  background-color: white;
  position: absolute;
  left: 50%;
  top: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="shape" onclick="curse()"></div>
<img src='http://images.clipartpanda.com/raindrop-clipart-RTGdn5bTL.png' width="15px" class="raindrop1">

Example 2

If you do not want to attach it to the div, you can utilize the code below:

$("img:not(.raindrop1)").css({
  "left": $(".shape").position().left,
  "top": $(".shape").position().top,
  "position": "relative"
});

$(".raindrop1").clone().removeClass("raindrop1").appendTo("body");
$("img:not(.raindrop1)").css({
  "left": $(".shape").position().left,
  "top": $(".shape").position().top,
  "position": "relative"
});
.shape {
  border-radius: 50px;
  width: 10px;
  height: 10px;
  background-color: white;
  position: absolute;
  left: 50%;
  top: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="shape" onclick="curse()"></div>
<img src='http://images.clipartpanda.com/raindrop-clipart-RTGdn5bTL.png' width="15px" class="raindrop1">

Answer №2

If you want to find the location of the div, one way is to use the following code:

let position = $("div").css("top")
. After obtaining this value, you can then apply it to your image using $("img").css("top", position).

Answer №3

let clonedImg = selectedDiv.clone()
    .offset({
        top: selectedDiv.offset().top,
        left: selectedDiv.offset().left
    })

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

Is it possible to create a personalized serialize form when sending an AJAX POST request

How can I format form data on an AJAX POST request differently than the default $("#formid").serialze()? The current result is not suitable for my needs, as it looks like this: `poststring="csrfmiddlewaretoken=bb9SOkN756QSgTbdJYDTvIz7KYtAdZ4A&colname= ...

The Bootstrap toggler is failing to conceal

Currently, I am working on a website utilizing Bootstrap 5. The issue arises with the navbar - it successfully displays the navigation when in a responsive viewport and the toggler icon is clicked. However, upon clicking the toggler icon again, the navigat ...

Showing nested div elements in a single row

I'm currently dealing with a react component that generates nested divs. My goal is to make sure all the divs are displayed on the same line, but the catch is I can only apply styles to the outermost container div. Is there a way to achieve this witho ...

Using Vue's V-IF directive to compare dates

On my website, I have an object that contains a field named available_at with the date in the format of 2019-08-08 I have a working HTML table utilizing Vue bindings but I am unsure how to compare the timestamp above using the built-in Date.now() method ...

Steps for constructing an object containing an array of nested objects

I've been working on this problem for some time now, and it's starting to feel like a messy situation that just won't come together. I'm trying to recreate an object with 5 properties and a nested array of objects, but so far, it's ...

Activate function at timed intervals

I've created a function that moves to the next slideshow image when a user clicks a link: jQuery("a.next").click(function(e) { return ngg_ajax_navigation(e, this); }); Now, I want the image to advance automatically every 3 seconds as well. What& ...

What is the process for generating an alert box with protractor?

While conducting tests, I am attempting to trigger an alert pop-up box when transitioning my environment from testing to production while running scripts in Protractor. Can someone assist me with this? ...

What is the process for conducting linting with nodemon?

Is it possible to utilize nodemon for linting my javascript without the use of build tools like gulp or grunt, in order to fully leverage node and npm? Nodemon's output can be piped. I am interested in using this feature for linting the changed file ...

Is there a way to execute code right before the jQuery submit() function is run?

I'm currently facing an issue with executing codes before a validation function runs in my form. I have tried different methods including referring to How to order events bound with jQuery, but without success. This is the code snippet I am working w ...

Sending a JSON object containing quotes, URLs, and other data through a POST request in Javascript

I have been researching how to convert JavaScript to JSON and properly escape characters, but I am not having much luck. I need to pass a large JavaScript object in a POST call using Postman, following this structure: { "key":"/url/url/es", "value":"{myDa ...

React Scheduler by Bryntum

After successfully discovering some functions related to various actions, I find myself still in need of additional functions: Currently, I am utilizing these functions by passing them directly as props to the Scheduler React Component: - onBeforeEventSa ...

What is the reason for not receiving a JSON object in the response from my personal node.js/express server?

-- Exploring a New Challenge -- I am currently working on creating an e-form signup for a client as part of our business marketing strategy service. The form design is complete and looks excellent. Now, I need to connect it to the existing API that our bu ...

Exploring depths with nested ng-Repeat in Depth First Search

I am dealing with an object that has variable key names, each of which contains nested objects. My goal is to perform a depth-first search (DFS) over this object. For example, consider the following object: object = { "var1" : { "var2 ...

Changes made in React are not reflected in the DOM

import React, { Component } from "react"; import ReactDOM from "react-dom"; import "./index.css"; class App extends Component { constructor(props) { super(props); this.state = { text: "", listItem: [] } this.onChangeInpu ...

ajax always returns the same value, even when the input value is different

Each time I click on a different value, the console.log keeps giving me the same value. view image description I have checked the HTML code and found that even though each value attribute is different, it still returns the first value of the attribute. vi ...

Transferring PHP variables to Javascript variables

I'm working on a basic php script that updates specific elements of a game through JavaScript. I've encountered an issue where the variable data doesn't seem to transfer when passing them from the script to the game using forms. Currently, t ...

adjust variable increment in JavaScript

As I work on developing a Wordpress theme, I have encountered an issue with incrementing a load more button using Javascript. This is my first time facing this problem with a javascript variable, as I don't always use Wordpress. The variable pull_page ...

Styling Collapse Text in Bootstrap Framework

I have a question about the "Accordion example" in the Bootstrap Collapse Component. My query is regarding removing the underline from the text "Collapsible Group Item" 1-3 when they are activated. Normally, this text is not underlined, but upon hovering, ...

React-Query: executing a function after updating query data

After updating the cache in a form, triggered by a response from the server, I utilize setQueryData. However, following this cache update, my goal is to refocus on the form input field. Here are some details: Within my React application, I employ Recoil. ...

Is it possible to integrate Wavify with React for a seamless user experience?

For my website designs, I have been experimenting with a JavaScript library known as Wavify (https://github.com/peacepostman/wavify) to incorporate wave animations. Recently delving into the world of React, I pondered whether I could integrate Wavify into ...