Click here to Skip to main content
13,189,811 members (57,984 online)
Rate this:
Please Sign up or sign in to vote.
I want to create a slider div which move random position on the html documents with Jquery (animate function).
My HTML code is :-

<script src="scripts/jquery-1.8.3.min.js">
var temp=parseInt(Math.random()* document.body.clientWidth);
<button id="bu1">Start Animation</button>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;">

when I run this its doesn't move that div ...
how to write that Math.Random value in CSS ????

thanks in advanced..
Posted 17-Apr-13 5:16am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Change this

with this
temp = temp+"px"


it's like you are writing
  left: '100px';

wich is not valid
Moykn 18-Apr-13 8:10am
Don't understand why my answer was downvoted, despite jQuery treating number as pixels otherwise specified if he needs to use "%" or "em" this
or this
will still be wrong. Removing the line temp="'"+temp+"px'"; will make the code works but it's not showing what is wrong.
Brian A Stephens 18-Apr-13 8:47am
You are right, sorry. I changed my vote. Your answer is valid and applies to some alternate situations too. When I first read your answer, I misunderstood what you were saying.
Moykn 18-Apr-13 8:54am
Thank you. But you are right too, i know jQuery treats numbers as "px" and i should have considered this in my answer, to make it more precise.
Jayanta Chatterjee 18-Apr-13 8:54am
Thanks a Lotttttttttttt its works....
thanks again Sir....
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Instead of complicating it by adding the "px" (and inadvisably mixing datatypes), just leave it as a bare number. The JQuery docs state that
Property values are treated as a number of pixels unless otherwise specified.

So just remove this line:
Jayanta Chatterjee 18-Apr-13 8:54am
Thank You Sir....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171016.2 | Last Updated 17 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100