CSS fixed dynamically with JavaScript and multiple div elements placed randomly

Is it possible to dynamically change the position of multiple div elements on a webpage without reloading?

I am looking for a way to modify the top and left positions of several divs, all with the same class, simultaneously. I want each div to have a different position without having to refresh the page.

For example:

.class{
  background-color:black;
  color: white;
  position:absolute;
  top: RANDOM;
  left: RANDOM;
}

<div1 class="class"></div> /// positioned at top:100px and left:200px
<div2 class="class"></div> /// positioned at top:245px and left:298px
<div3 class="class"></div> /// positioned at top:190px and left:490px
<div4 class="class"></div> /// positioned at top:89px and left:344px

Is there a way to achieve this on the same page? I have looked for solutions but only found scripts that change the position of one div upon clicking, not multiple divs simultaneously.

Answer №1

With the power of jQuery:

$('.element').each(function() {
  $(this).css('top', Math.floor(Math.random() * 101));
  $(this).css('left', Math.floor(Math.random() * 200));
});

For a visual demonstration, you can check out this jsfiddle link: http://jsfiddle.net/pZ7Kf/10/.

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

Ways to forward a parameter to a different web address?

Is there a way to properly redirect a parameter such as http://example.com/?link=https://google.com to its destination at ? ...

Exploring the power of D3's nested appends and intricate data flow

Currently diving into the world of D3, I've encountered a perplexing issue that has yet to be resolved. Unsure if my confusion stems from a lack of familiarity with the library or if there's a key procedure eluding me, I feel compelled to seek gu ...

ASP.NET "Data" Error: Trouble Parsing JSON Data on the Front-End

I am currently facing an issue with the configurations on my asmx page. The code is set up like this: using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; using System.Web.Script.Serialization; using Syst ...

unable to expand navbar in Angular 5 project with Bootstrap 4

I am encountering an issue with my navigation bar in a project that uses Angular5 and Bootstrap4. The navigation bar is supposed to expand when the screen size is small, as indicated by the navbar-expand-sm class in Bootstrap4. However, the navbar remains ...

Disregard keys with null values when making a post request using react and axios

As a beginner in JavaScript, I am facing an issue where I do not want to include keys with null values when sending metadata. For example, I should only send the filepath as it has a value, and omit tags and owner which are null. How can I achieve this? ...

Change the printer orientation to landscape when using the WebBrowser Control

When trying to print an HTML file using the WebBrowser Control, I encountered an issue in which I needed to force the print to be in landscape mode. I attempted a solution involving setting the printer settings to landscape mode, but it did not work as exp ...

Step-by-step guide on showcasing a quiz utilizing Magnific-popup and ajax

My goal is to create a functionality that allows users to easily download and view a quiz in a lightbox with just one click. I have successfully set up the ajax features and believe I am using Magnific-popup correctly, but for some reason, the lightbox is ...

Styles created using makeStyles in MUI are no longer working properly following an update to both MUI and React versions

Implementing the MUI Dropdown in a components library. When testing the library in Storybook, everything functions properly. However, when integrating the component into a separate React project, the layout becomes distorted. The Dropdown label is posit ...

Adding HTML content using jQuery's document.ready feature

As a jQuery novice, I am attempting to incorporate a Facebook like button using the jQuery document.ready function. In my external Javascript file (loaded after the jQuery script), you will find the following code snippet: $(document).ready(function(){ ...

JQuery hover effect causes mouse pointer to flicker

I'm currently working on creating a mega menu using Bootstrap 4, and I came across this code: While the code works well, I wanted to add a slide down/slide up effect to reveal the dropdown content when hovering over the dropdown link. So, I included ...

Adding Items to the Beginning of a Drop-down List using jQuery.prepend

I have created a select list for various types of restaurants. Users can choose a food type, click search, and receive a list of restaurants specializing in that particular cuisine. To add an initial option to the select dropdown, I used the following cod ...

Is there a method in Jquery to change the background color of a Div when it is clicked on?

Is there a way to use Jquery to highlight/select the text within a clicked-on div as if it were selected with the mouse? This would not be for a text box, but rather a standard div element. I am attempting to create a 'short URL' input box where ...

Retrieving the link to share for every video located in the Dropbox folder

My Dropbox folder contains a set of videos labeled "v1.avi, v2.avi, ....., vn.avi". I am looking to automate the process of extracting the share link for each video in the folder so that I can easily use it as a source value for HTML video. . . . Is ther ...

Trouble with the Bootstrap navbar loading correctly

Recently I started using Bootstrap and decided to implement one of their templates. Everything is working fine, except for my navbar which is not loading correctly. It should look like this: Desired Navbar However, in both Chrome and Firefox, it appears l ...

Updating dropdown menu selection after successful form submission validation

I'm currently working on a PHP sign up/registration form that retains and resubmits user input if validation fails. Although I have successfully implemented text boxes, password inputs, and radio buttons, I am encountering some challenges with the dro ...

How come Font Awesome icons aren't appearing in my link?

Is my question strange? I've noticed a peculiar behavior when using fontawesome. Normally, it displays correctly, and I have already referred to the documentation, but none of the solutions I found here seem to solve my issue. Sources I have tried: ...

The essence of responsiveness within personalized widgets

To create a vertical slider input, I had to find an alternative option since the built-in sliderInput function does not support it. After exploring this thread, I learned that there are two possible solutions: (I) rotating the sliderInput widget using CSS ...

What was the reason for Vue's decision to deprecate the ""is" attribute for HTML elements?

Vue's eslint rules mention that the is attribute on HTML elements is deprecated (since 3), and should be used on components instead: <template> <!-- ✓ GOOD --> <div /> <component is="foo"> <!-- ✗ BAD --&g ...

Implementing additional input with Jquery causes tooltips to malfunction

I am developing a form that allows users to add as many rows as needed. Each input is being treated as an array for easy data storage in the database, which is a new concept for me. $(document).ready(function() { var maxField = 10; //Limitati ...

Utilizing Node.js modules in a frontend HTML file with the help of browserify

I am encountering difficulty using a node.js module on my website with browserify. The example provided only demonstrates how to run a separate JavaScript file, not how to incorporate the node.js module within the .html file itself. This seemingly simple i ...