It isn’t working like the demo because of the position:relative you added to the parent which messes up the positioning of the list element.
If you remove margin:auto and position:relative it will work like your original demo. The original demo does not work on a centred element either.
You would need to work out the position from the containing div rather than the edge of the viewport I think,
This seems to work but Paul (or someone who knows what they are doing) can tidy it up:)
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
html.body {
padding: 0;
margin: 0;
}
#wrapper {
width:1200px;
background:#ddd;
margin:0 auto;
}
#wrap {
width:1000px;
height: 300px;
border: 1px solid red;
margin:0 auto;
}
#wrap ul {
list-style-type:none;
width:1000px;
height: 300px;
}
#wrap ul li {
width:1000px;
position: absolute;
width: 60px;
height: 60px;
-webkit-border-radius: 30px;
-moz-border-radius: 30px;
border-radius: 30px;
background-color:orange;
line-height: 60px;
}
header {
width:100%;
height:300px;
}
.one {
width:70%;
pading:50px;
margin:20px auto;
}
.clear {
clear:both;
}
</style>
</head>
<body>
<p>test</p>
<p>test</p>
<div id="wrapper">
<header>
<div id="wrap">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
</ul>
</div>
</header>
<div class="one"> Duo Reges: constructio interrete. Nec vero sum nescius esse utilitatem in historia, non modo voluptatem. Non quam nostram quidem, inquit Pomponius iocans; Hic nihil fuit, quod quaereremus. Si autem id non concedatur, non continuo vita beata tollitur.
An hoc usque quaque, aliter in vita?
Primum in nostrane potestate est, quid meminerimus?
Quid ergo hoc loco intellegit honestum? Nunc ita separantur, ut disiuncta sint, quo nihil potest esse perversius. </div>
<div class="one"> Duo Reges: constructio interrete. Nec vero sum nescius esse utilitatem in historia, non modo voluptatem. Non quam nostram quidem, inquit Pomponius iocans; Hic nihil fuit, quod quaereremus. Si autem id non concedatur, non continuo vita beata tollitur.
An hoc usque quaque, aliter in vita?
Primum in nostrane potestate est, quid meminerimus?
Quid ergo hoc loco intellegit honestum? Nunc ita separantur, ut disiuncta sint, quo nihil potest esse perversius. </div>
<div class="clear"></div>
</div>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$.fn.bounce = function(options) {
var settings = $.extend({
speed: 5
}, options);
return $(this).each(function() {
var $this = $(this),
$parent = $this.parent(),
height = $parent.height(),
width = $parent.width(),
top = Math.floor(Math.random() * (height / 2)) + height / 4,
left = Math.floor(Math.random() * (width / 2)) + width / 4 ,
vectorX = settings.speed * (Math.random() > 0.5 ? 1 : -1),
vectorY = settings.speed * (Math.random() > 0.5 ? 1 : -1);
// place initialy in a random location
$this.css({
'top': top,
'left': left
}).data('vector', {
'x': vectorX,
'y': vectorY
});
var move = function($e) {
var offset = $e.offset() ,
width = $e.width(),
height = $e.height(),
vector = $e.data('vector'),
$parent = $e.parent();
if (offset.left <= $parent.offset().left && vector.x < 0) {
vector.x = -1 * vector.x;
}
if ((offset.left + width - $parent.offset().left) >= $parent.width()) {
vector.x = -1 * vector.x;
}
if (offset.top <= $parent.offset().top -15 && vector.y < 0) {
vector.y = -1 * vector.y;
}
if ((offset.top + height + 15 - $parent.offset().top ) >= $parent.height()) {
vector.y = -1 * vector.y;
}
$e.css({
'top': offset.top + vector.y + 'px',
'left': offset.left + vector.x + 'px'
}).data('vector', {
'x': vector.x,
'y': vector.y
});
setTimeout(function() {
move($e);
}, 50);
};
move($this);
});
};
$(function() {
$('#wrap ul li').bounce({
'speed': 5
});
});
</script>
</body>
</html>