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

  • SEARCH
  • Home
  • 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 8692821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:24:00+00:00 2026-06-13T00:24:00+00:00

So I want to achieve this: http://superdit.com/2010/12/25/moving-element-to-random-position-inside-a-container/ I think my .js code should be

  • 0

So I want to achieve this:
http://superdit.com/2010/12/25/moving-element-to-random-position-inside-a-container/

I think my .js code should be alright, still the “heart” animates outside the #pageWrap (my container). What is wrong?

Link to my site: http://carlpapworth.com/htmlove/fumbling.html

EDIT: Actually I have 2 problems with this, the second being that the hover-action only takes place once, after the first “hover-animation-effect” it stops working. PLZ help 🙂

HTML:

<body>
<header>

<div id="headerTitle"><a href="index.html">&lt;html<span class="heart">&hearts;</span>ve&gt;</a>
            </div>
            <div id="help">
                <h2>?</h2>
                <div id="helpInfo">
                    <p>The name of the puzzle should lead u to success!</p>
                </div>
            </div>
        </header>
    <div id="reward">
        <div id="rewardContainer">
            <div id="rewardBG" class="heart">&hearts;
            </div>
            <p>OMG, this must be luv<br><a href="index.html" class="exit">x</a></p>
        </div>
    </div>  
    <div id="pageWrap">
        <div id="goal">
            <a href="#reward" class="heart">&hearts;</a>
        </div>  
    </div> <!-- END Page Wrap -->
    <footer>
        <div class="heartCollection">
            <p>collect us if u need luv:<p>
            <ul>
                <li><a id="collection1">&hearts;</a></li>
                <li><a id="collection2">&hearts;</a></li>
                <li><a id="collection3">&hearts;</a></li>
                <li><a id="collection4">&hearts;</a></li>
                <li><a id="collection5">&hearts;</a></li>
                <li><a id="collection6">&hearts;</a></li>
            </ul>
        </div>
        <div class="credits">with love from Popm0uth ©2012</div>
    </footer>

</body>

Css

body{
    overflow: hidden;
    }


#pageWrap{
padding: 20px 20px 100px 20px;
position: relative;
width: 100%;
height: 700px;
text-align: center;
display: block;
}

header{
position: fixed;
width: 100%;
height: 60px;
margin: 0 auto;
left: 0px;
background: url(../images/bg.png) solid;
z-index: 2000;
display: block;
}

#goal{
position: relative;
width: 100px;
height: 100px;
left: 50%;
top: 25%;
margin-left: -100px;
padding: 30px;
display: block;
}

a.heart{
font-size: 80px;
text-align: center;
display: block;
}

footer{
position: fixed;
bottom: 0px;
padding: 10px; 
width: 100%;
height: 100px;
background: /*url(../images/bgFooter.png)*/ #dddddd;
z-index: 2000;
}

JS:

$(document).ready(function() {
function randomFromTo(from, to){
            return Math.floor(Math.random() * (to - from + 1) + from);
}

            /* get container position and size
             * -- access method : cPos.top and cPos.left */
            var cPos = $('#pageWrap').offset();
            var cHeight = $(window).height() - $('header').height() - $('footer').height();
            var cWidth = $(window).width() - $('header').width() - $('footer').width(); 

            // get box padding (assume all padding have same value)
            var pad = parseInt($('#goal').css('padding-top').replace('px', ''));

            // get movable box size
            var bHeight = $('#goal').height();
            var bWidth = $('#goal').width();

            // set maximum position
            maxY = cPos.top + cHeight - bHeight - pad;
            maxX = cPos.left + cWidth - bWidth - pad;

            // set minimum position
            minY = cPos.top + pad;
            minX = cPos.left + pad;

            // set new position         
            newY = randomFromTo(minY, maxY);
            newX = randomFromTo(minX, maxX);


        $('#goal').mouseenter(function() {
            $(this).stop(true,true).animate({
                left: newY,
                top: newX
                });
        });
});








    /*
    function randomFromTo(from, to){
            return Math.floor(Math.random() * (to - from + 1) + from);
        }

    function moveRandom {
            /* get container position and size
             * -- access method : cPos.top and cPos.left
            var cPos = $('#pageWrap').offset();
            var cHeight = $('#pageWrap').height();
            var cWidth = $('#pageWrap').width();

            // get box padding (assume all padding have same value)
            var pad = parseInt($('#pageWrap').css('padding-top').replace('px', ''));

            // get movable box size
            var bHeight = $('#goal').height();
            var bWidth = $('#goal'+id).width();

            // set maximum position
            maxY = cPos.top + cHeight - bHeight - pad;
            maxX = cPos.left + cWidth - bWidth - pad;

            // set minimum position
            minY = cPos.top + pad;
            minX = cPos.left + pad;

            // set new position         
            newY = randomFromTo(minY, maxY);
            newX = randomFromTo(minX, maxX);

            $('#goal').animate({
                top: newY,
                left: newX
                }, 500, function() {
            });
        }   
*/
  • 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-13T00:24:01+00:00Added an answer on June 13, 2026 at 12:24 am

    You don’t have to specify the #pageWrap height and width. You have to calculate it depending on the window size.

    Replace this:

    var cHeight = $('#pageWrap').height();
    var cWidth = $('#pageWrap').width(); 
    

    with that:

    var cHeight = $(window).height() - $('header').height() - $('footer').height();
    var cWidth = $(window).width() - $('header').width() - $('footer').width(); 
    

    Don’t you see something strange here?

    left: newY, 
    top: newX 
    

    should be

    left: newX, 
    top: newY 
    

    and everything will come in place 🙂

    EDIT:

    The onmouseover event is not signaled once, it is handled each time when mouse overs the area, but you just initialize the final destination point once in the begining of your code. So the moving object stays at the same place.

    Finally you should modify your script as follow:

    $(document).ready(function() {
    
      function randomFromTo(from, to){
         return Math.floor(Math.random() * (to - from + 1) + from);
      }
    
      $('#goal').mouseenter(function() {
    
        var cPos = $('#pageWrap').offset();
        var cHeight = $(window).height() - $('header').height() - $('footer').height();
        var cWidth = $(window).width();
    
        // get box padding (assume all padding have same value)
        var pad = parseInt($('#goal').css('padding-top').replace('px', ''));
    
        // get movable box size
        var bHeight = $('#goal').height();
        var bWidth = $('#goal').width();
    
        // set maximum position
        maxY = cHeight - bHeight - pad;
        maxX = cWidth - bWidth - pad;
    
        // set minimum position
        minY = cPos.top + pad;
        minX = cPos.left + pad;
    
        // set new position
        newY = randomFromTo(minY, maxY);
        newX = randomFromTo(minX, maxX);
    
    
        $(this).animate({
                left: newX,
                top: newY
        });
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made something like this: http://static.cmslog.com/testapp/google/terrain What I want to achieve now is terrain
Again I have a question regarding this plugin: http://t.wits.sg/2008/06/20/jquery-progress-bar-11/ What I want to achieve
I want to achieve something like this inside a UIView: I have created a
I've implemented a singleton CoreLocation class (courtesy of this link: http://jinru.wordpress.com/2010/08/15/singletons-in-objective-c-an-example-of-cllocationmanager/ ) However, I
I have a picture url like this http://www.address.com/image.jpg I want do dynamically save this
Does anyone know of a good tutorial to achieve this? (as seen here: http://www.contrastrebellion.com/
I have this menu http://jsbin.com/useqa4/3 The hover I think works correct, but what I
this is my fiddle http://jsfiddle.net/ocinisme/vqAwJ/5/ Practically what I want to achieve is that all
I want a Layout like this: But my code doesn't work. I can't achieve
I need some help with aligning the text. I want to achieve this https://i.stack.imgur.com/3aKvl.jpg

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.