Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8649755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:38:23+00:00 2026-06-12T13:38:23+00:00

I have a list of div elements, which is dragable items. I need to

  • 0

I have a list of div elements, which is dragable items.

I need to do, that all elements, i will generate random top and left positions, which will be in range of my container, but i dont know how to do it 🙂

Container div is: width: 800px; height: 500px;

My js now is like:

var randomTopPos = Math.floor(Math.random() * (600 / 2));
var randomLeftPos = Math.floor(Math.random() * (5));

$(".t-shirt-design").css({
    'top' : randomTopPos,
    'left': randomLeftPos
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:38:24+00:00Added an answer on June 12, 2026 at 1:38 pm

    Given than your container id is container and your draggable id is dragid, you can do as follows:

    ctop = $('#container').offset().top;  // container top
    cleft = $('#container').offset().left;// container left
    cheight = $('#container').height();   // container height
    cwidth = $('#container').width();     // container width
    
    $(".t-shirt-design").each(function() {
       dragheight = $(this).height(); //your draggable height
       dragwidth  = $(this).width(); // your draggable width
       randomtop = ctop + Math.floor((Math.random()* (cheight - dragheight))+1);
       randomleft = cleft + Math.floor((Math.random()* (cwidth - dragwidth))+1);
       $(this).css({
         'top' : randomtop,
         'left': randomleft
       });
    });
    

    UPDATE:
    Updated the code to accomodate several .t-shirt-design elements

    UPDATE 2:
    Also you have an error in your html code, an html element can only have ONE id, you have two in your container html element, as follows:

    <div id="tshirts-designs homepage">
    

    replace it for just one, the correct would be:

    <div id="homepage">
    

    UPDATE 3:
    Looking at your page, I adjust my code to better fit your requirements (your draggable elements have different widths and heights), so try my update code instead, also better execute this code on the window.load event, instead of .ready , as we need the images to be loaded so the div heights and widths are the correct ones, so replace your following line:

    $(document).ready(function() {
    

    for this one:

    $(window).load(function() {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of li elements that I need to allow drag/drop into
I have a div element which consists of list elements eg: java, ror, .NET.
Within my code I have list items like this; <div id=content> <ul class=kwicks> <li
The problem: I have a div where I will have a list of selections
I have an unordered list that is a child in a smaller parent div
I have a nestedSortable list in which I'm disallowing the nesting of items with
I have a page where i have a sortable list of items (a div
I have a div within which elements are dynamically added and removed. I'd like
I have a simple function in which a user will click on a div,
I'm developing a List of four items. Each item needs to have a top

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.